Skip to content

Commit 61b530e

Browse files
authored
feat: command registration (#43)
* 🌟 feat: add CLEAR_GLOBAL_COMMANDS env variable * 🌟 feat: remove command registration from main bot entry point * 🌟 feat: add global command clearing functionality in deployCommands
1 parent 9382bf2 commit 61b530e

File tree

5 files changed

+9
-20
lines changed

5 files changed

+9
-20
lines changed

β€Ž.env.productionβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ MODERATORS_ROLE_IDS=849481536654803004
1818
GUIDES_TRACKER_PATH=/app/data/guides-tracker.json
1919
ADVENT_OF_CODE_TRACKER_PATH=/app/data/advent-of-code-tracker.json
2020

21+
CLEAR_GLOBAL_COMMANDS=true
22+
2123
# Note: DISCORD_TOKEN & CLIENT_ID should be in .env.local (not committed)

β€Žsrc/env.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const config = {
2222
},
2323
serverId: requireEnv('SERVER_ID'),
2424
fetchAndSyncMessages: true,
25+
clearGlobalCommands: optionalEnv('CLEAR_GLOBAL_COMMANDS') === 'true',
2526
guidesTrackerPath: optionalEnv('GUIDES_TRACKER_PATH'),
2627
adventOfCodeTrackerPath: requireEnv('ADVENT_OF_CODE_TRACKER_PATH'),
2728
roleIds: {

β€Žsrc/index.tsβ€Ž

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { ActivityType, Client, GatewayIntentBits } from 'discord.js';
2-
import { commands } from './commands/index.js';
32
import { config } from './env.js';
43
import { events } from './events/index.js';
5-
import { registerCommands } from './util/commands.js';
64
import { registerEvents } from './util/events.js';
75

86
// Create a new client instance
@@ -27,6 +25,5 @@ const client = new Client({
2725

2826
// Register events and commands
2927
await registerEvents(client, events);
30-
await registerCommands(client, commands);
3128

3229
void client.login(config.discord.token);

β€Žsrc/util/commands.tsβ€Ž

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,6 @@ export const createCommands = (commands: Array<Command>): Command[] => {
99
return commands.map(createCommand);
1010
};
1111

12-
export const registerCommands = async (
13-
client: Client,
14-
commands: Map<string, Command>
15-
): Promise<void> => {
16-
const commandArray = Array.from(commands.values()).map((cmd) => cmd.data);
17-
18-
try {
19-
await client.application?.commands.set(commandArray);
20-
commandArray.forEach((cmd) => {
21-
console.log(`Registered command: ${cmd.type}, ${cmd.name}`);
22-
});
23-
console.log(`Registered ${commandArray.length} commands globally.`);
24-
} catch (error) {
25-
console.error('Error registering commands:', error);
26-
}
27-
};
28-
2912
export const buildCommandString = (interaction: ChatInputCommandInteraction): string => {
3013
const commandName = interaction.commandName;
3114
return `/${commandName} ${interaction.options.data.map((option) => `${option.name}:${option.value}`).join(' ')}`;

β€Žsrc/util/deploy.tsβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ export async function deployCommands(): Promise<RESTPutAPIApplicationCommandsRes
77

88
const rest = new REST({ version: '10' }).setToken(config.discord.token);
99

10+
if (config.clearGlobalCommands) {
11+
console.log('Clearing global commands...');
12+
await rest.put(Routes.applicationCommands(config.discord.clientId), { body: [] });
13+
console.log('Global commands cleared.');
14+
}
15+
1016
const result = (await rest.put(
1117
Routes.applicationGuildCommands(config.discord.clientId, config.serverId),
1218
{

0 commit comments

Comments
Β (0)