Skip to content

Commit 4edb171

Browse files
feat: /cron command to add recurring messages
1 parent ef17bbe commit 4edb171

File tree

7 files changed

+88
-5
lines changed

7 files changed

+88
-5
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
},
1515
"dependencies": {
1616
"@keyv/redis": "2.7.0",
17-
"discord.js": "14.13.0",
1817
"cheerio": "1.0.0-rc.12",
18+
"cron": "2.4.3",
19+
"discord.js": "14.13.0",
1920
"env-var": "7.4.1",
2021
"keyv": "4.5.3",
2122
"open-graph-scraper": "6.2.2",

pnpm-lock.yaml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/add-cron.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { CronJob } from 'cron';
2+
import { ChatInputCommandInteraction } from 'discord.js';
3+
4+
import { getCronTime } from './helpers/get-cron-time';
5+
6+
export const addCron = (interaction: ChatInputCommandInteraction): void => {
7+
const frequency = interaction.options.getString('every', true);
8+
const message = interaction.options.getString('message', true);
9+
10+
const job = new CronJob(
11+
getCronTime(frequency),
12+
() => {
13+
interaction.channel?.send(message).catch(console.error);
14+
},
15+
null,
16+
true,
17+
'Europe/Paris',
18+
);
19+
job.start();
20+
};

src/commands.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,28 @@ export const voiceOnDemandCommand = new SlashCommandBuilder()
1010

1111
export const fartCommand = new SlashCommandBuilder()
1212
.setName('fart')
13-
.setDescription("Replies with https://prout.dev")
13+
.setDescription('Replies with https://prout.dev')
1414
.toJSON();
15+
16+
export const cronCommand = new SlashCommandBuilder()
17+
.setName('cron')
18+
.setDescription('Manage recurring messages')
19+
.addSubcommand((subcommand) =>
20+
subcommand
21+
.setName('add')
22+
.setDescription('Add a recurring message')
23+
.addStringOption((option) =>
24+
option
25+
.setName('every')
26+
.setDescription('How often to send the message')
27+
.addChoices(
28+
{ name: 'day', value: 'day' },
29+
{ name: 'week', value: 'week' },
30+
{ name: 'month', value: 'month' },
31+
)
32+
.setRequired(true),
33+
)
34+
.addStringOption((option) =>
35+
option.setName('message').setDescription('The message to send').setRequired(true),
36+
),
37+
);

src/handlers/handle-interaction-creation.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import type { Interaction } from 'discord.js';
22

3+
import { addCron } from '../add-cron';
34
import { createLobby } from '../create-lobby';
45

56
export const handleInteractionCreation = async (interaction: Interaction): Promise<void> => {
67
if (
78
!interaction.isCommand() ||
89
!interaction.inGuild() ||
910
!interaction.isChatInputCommand() ||
10-
!['voice-on-demand', 'fart'].includes(interaction.commandName)
11+
!['voice-on-demand', 'fart', 'cron'].includes(interaction.commandName)
1112
) {
1213
return;
1314
}
@@ -23,5 +24,12 @@ export const handleInteractionCreation = async (interaction: Interaction): Promi
2324
case 'fart':
2425
await interaction.reply('https://prout.dev');
2526
break;
27+
case 'cron':
28+
if (interaction.options.getSubcommand(true) !== 'add') {
29+
await interaction.reply('Unknown subcommand');
30+
return;
31+
}
32+
addCron(interaction);
33+
break;
2634
}
2735
};

src/helpers/get-cron-time.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const getCronTime = (cron: string): string => {
2+
switch (cron) {
3+
case 'day':
4+
return '0 0 9 * * *';
5+
case 'week':
6+
return '0 0 9 * * 0';
7+
case 'month':
8+
return '0 0 9 1 * *';
9+
default:
10+
throw new Error('Unknown cron time');
11+
}
12+
};

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Client, REST, Routes } from 'discord.js';
22

3-
import { fartCommand, voiceOnDemandCommand } from './commands';
3+
import { cronCommand, fartCommand, voiceOnDemandCommand } from './commands';
44
import { config } from './config';
55
import { deleteExistingCommands } from './delete-existing-commands';
66
import { handleGuildMessageCreation } from './handlers/handle-guild-message-creation';
@@ -51,7 +51,7 @@ const rest = new REST({ version: '10' }).setToken(discord.token);
5151
await deleteExistingCommands(rest, discord);
5252

5353
await rest.put(Routes.applicationGuildCommands(discord.clientId, discord.guildId), {
54-
body: [voiceOnDemandCommand, fartCommand],
54+
body: [voiceOnDemandCommand, fartCommand, cronCommand],
5555
});
5656

5757
console.log('Bot started.');

0 commit comments

Comments
 (0)