From 7a8a2e30071b2614817b24472db4afcfafa8a792 Mon Sep 17 00:00:00 2001 From: Craig Laparo Date: Sun, 7 Jul 2024 22:59:11 -0500 Subject: [PATCH] Add option `inactive-interval` --- USAGE.md | 1 + bot.js | 2 +- commands/reminder.js | 14 ++------- commands/tak.js | 18 ++++++++++- util.js | 72 ++++++++++++++++++++++++++++++++------------ 5 files changed, 74 insertions(+), 33 deletions(-) diff --git a/USAGE.md b/USAGE.md index 5d70cdf..46a96d4 100644 --- a/USAGE.md +++ b/USAGE.md @@ -9,6 +9,7 @@ Use `/tak @opponent` to start a new game. You can specify any of the following: - `opening` _(default `swap`):_ Specify an opening variation. Valid values are `swap` and `no-swap`. - `caps`: Override the number of cap stones per player. - `flats`: Override the number of flat stones per player. +- `inactive-interval` Specifies the time interval between inactivity reminders. - `theme` _(default `discord`):_ Uses the specified theme. - `flat-counts` _(default `true`)_: Show flat counts - `stack-counts` _(default `true`)_: Show stack counts diff --git a/bot.js b/bot.js index 070c2d0..2f309fb 100644 --- a/bot.js +++ b/bot.js @@ -61,7 +61,7 @@ client.on(Discord.Events.ClientReady, () => { const timerPath = path.join(timersDir, timerFilename); const timer = require(timerPath); if (timer && timer.timestamp && timer.type) { - setTimer(timer.type, timer.timestamp, channelId, timer.playerId); + setTimer(timer, channelId); } else { console.log("Invalid timer:", timerPath); } diff --git a/commands/reminder.js b/commands/reminder.js index d58e56c..52c8377 100644 --- a/commands/reminder.js +++ b/commands/reminder.js @@ -1,6 +1,6 @@ const { SlashCommandBuilder } = require("discord.js"); const timestring = require("timestring"); -const { sendMessage, saveTimer } = require("../util"); +const { sendMessage, setReminder } = require("../util"); module.exports = { data: new SlashCommandBuilder() @@ -15,17 +15,7 @@ module.exports = { async execute(interaction) { try { const delay = timestring(interaction.options.getString("delay")) * 1e3; - const timestamp = Math.round((new Date().getTime() + delay) / 1e3); - sendMessage( - interaction, - `OK, I will ping you in this channel .` - ); - saveTimer( - "reminder", - timestamp, - interaction.channelId, - interaction.member.id - ); + setReminder(interaction, delay); } catch (err) { console.error(err); sendMessage( diff --git a/commands/tak.js b/commands/tak.js index 1200845..6a160f4 100644 --- a/commands/tak.js +++ b/commands/tak.js @@ -97,6 +97,18 @@ module.exports = { .setDescription("Flat stones per player") .setMinValue(3) ) + .addIntegerOption((option) => + option + .setName("inactive-interval") + .setDescription("Configure inactivity reminders") + .addChoices( + { name: "1 Day (default)", value: 864e5 }, + { name: "2 Days", value: 1728e5 }, + { name: "3 Days", value: 2592e5 }, + { name: "1 Week", value: 6048e5 }, + { name: "Disabled", value: -1 } + ) + ) .addStringOption((option) => option .setName("theme") @@ -193,6 +205,9 @@ module.exports = { const caps = options.getInteger("caps"); const flats = options.getInteger("flats"); + // Inactivity Reminder Interval + const inactiveInterval = options.getInteger("inactive-interval") || 864e5; + // Theme let theme = options.getString("theme"); if (theme) { @@ -205,7 +220,7 @@ module.exports = { theme = getTheme(interaction); } - // Toggles mode + // Toggles const flatCounts = options.getBoolean("flat-counts"); const stackCounts = options.getBoolean("stack-counts"); const showRoads = options.getBoolean("road-connections"); @@ -221,6 +236,7 @@ module.exports = { size, komi, opening, + inactiveInterval, }; if (tpsParsed) { gameData.initialTPS = tps; diff --git a/util.js b/util.js index 7623593..3554f30 100644 --- a/util.js +++ b/util.js @@ -29,7 +29,7 @@ Ply.prototype.toString = function () { let client; const defaultTheme = "discord"; -const INACTIVE_TIMER_MS = 864e5; +const DELETE_TIMER_MS = 864e5; const deleteTimers = {}; const inactiveTimers = {}; const reminderTimers = {}; @@ -603,7 +603,8 @@ module.exports = { } }, - saveTimer(type, timestamp, channelId, playerId) { + saveTimer(timer, channelId) { + const { type, timestamp, playerId, interval } = timer; try { const timersDir = path.join(__dirname, "data", channelId, "timers"); const timerPath = path.join( @@ -613,9 +614,12 @@ module.exports = { fs.mkdirSync(timersDir, { recursive: true }); fs.writeFileSync( timerPath, - JSON.stringify({ type, timestamp, playerId }) + JSON.stringify({ type, timestamp, playerId, interval }) + ); + module.exports.setTimer( + { type, timestamp, playerId, interval }, + channelId ); - module.exports.setTimer(type, timestamp, channelId, playerId); } catch (err) { console.error(`Failed to save timer ${timerPath}:`, err); } @@ -660,7 +664,8 @@ module.exports = { } }, - async setTimer(type, timestamp, channelId, playerId) { + async setTimer(timer, channelId) { + let { type, timestamp, playerId, interval } = timer; let channel; try { channel = await client.channels.fetch(channelId); @@ -685,10 +690,14 @@ module.exports = { module.exports.getReminderMessage(playerId), true ); - timestamp = Math.round( - (new Date().getTime() + INACTIVE_TIMER_MS) / 1e3 + if (!interval) { + interval = DELETE_TIMER_MS; + } + timestamp = Math.round((new Date().getTime() + interval) / 1e3); + module.exports.saveTimer( + { type, timestamp, playerId, interval }, + channelId ); - module.exports.saveTimer(type, timestamp, channelId, playerId); }, delay); break; case "reminder": @@ -706,16 +715,18 @@ module.exports = { }, async setDeleteTimer(msg) { - const delay = INACTIVE_TIMER_MS; + const delay = DELETE_TIMER_MS; const timestamp = Math.round((new Date().getTime() + delay) / 1e3); await msg.channel.send( `This channel will self-destruct unless a new game is started.` ); module.exports.saveTimer( - "delete", - timestamp, - msg.channelId || msg.channel.id, - msg.author ? msg.author.id : msg.member.id + { + type: "delete", + timestamp, + playerId: msg.author ? msg.author.id : msg.member.id, + }, + msg.channelId || msg.channel.id ); }, @@ -724,13 +735,36 @@ module.exports = { }, setInactiveTimer(msg, gameData, canvas) { - const delay = INACTIVE_TIMER_MS; - const timestamp = Math.round((new Date().getTime() + delay) / 1e3); + const interval = gameData.inactiveInterval || DELETE_TIMER_MS; + if (interval < 0) { + // Reminders disabled + return; + } + const timestamp = Math.round((new Date().getTime() + interval) / 1e3); module.exports.saveTimer( - "inactive", - timestamp, - msg.channelId || msg.channel.id, - gameData[`player${canvas.player}Id`] + { + type: "inactive", + timestamp, + playerId: gameData[`player${canvas.player}Id`], + interval, + }, + msg.channelId || msg.channel.id + ); + }, + + async setReminder(msg, delay) { + const timestamp = Math.round((new Date().getTime() + delay) / 1e3); + await module.exports.sendMessage( + msg, + `OK, I will ping you in this channel .` + ); + saveTimer( + { + type: "reminder", + timestamp, + playerId: msg.author ? msg.author.id : msg.member.id, + }, + msg.channelId || msg.channel.id ); },