Skip to content

Commit

Permalink
Add option inactive-interval
Browse files Browse the repository at this point in the history
  • Loading branch information
gruppler committed Jul 8, 2024
1 parent 4b25103 commit 7a8a2e3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 33 deletions.
1 change: 1 addition & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 2 additions & 12 deletions commands/reminder.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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 <t:${timestamp}:R>.`
);
saveTimer(
"reminder",
timestamp,
interaction.channelId,
interaction.member.id
);
setReminder(interaction, delay);
} catch (err) {
console.error(err);
sendMessage(
Expand Down
18 changes: 17 additions & 1 deletion commands/tak.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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) {
Expand All @@ -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");
Expand All @@ -221,6 +236,7 @@ module.exports = {
size,
komi,
opening,
inactiveInterval,
};
if (tpsParsed) {
gameData.initialTPS = tps;
Expand Down
72 changes: 53 additions & 19 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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(
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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":
Expand All @@ -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 <t:${timestamp}:R> 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
);
},

Expand All @@ -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 <t:${timestamp}:R>.`
);
saveTimer(
{
type: "reminder",
timestamp,
playerId: msg.author ? msg.author.id : msg.member.id,
},
msg.channelId || msg.channel.id
);
},

Expand Down

0 comments on commit 7a8a2e3

Please sign in to comment.