Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ MODERATORS_ROLE_IDS=849481536654803004
GUIDES_TRACKER_PATH=/app/data/guides-tracker.json
ADVENT_OF_CODE_TRACKER_PATH=/app/data/advent-of-code-tracker.json

CLEAR_GLOBAL_COMMANDS=true

# Note: DISCORD_TOKEN & CLIENT_ID should be in .env.local (not committed)
1 change: 1 addition & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const config = {
},
serverId: requireEnv('SERVER_ID'),
fetchAndSyncMessages: true,
clearGlobalCommands: optionalEnv('CLEAR_GLOBAL_COMMANDS') === 'true',
guidesTrackerPath: optionalEnv('GUIDES_TRACKER_PATH'),
adventOfCodeTrackerPath: requireEnv('ADVENT_OF_CODE_TRACKER_PATH'),
roleIds: {
Expand Down
3 changes: 0 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ActivityType, Client, GatewayIntentBits } from 'discord.js';
import { commands } from './commands/index.js';
import { config } from './env.js';
import { events } from './events/index.js';
import { registerCommands } from './util/commands.js';
import { registerEvents } from './util/events.js';

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

// Register events and commands
await registerEvents(client, events);
await registerCommands(client, commands);

void client.login(config.discord.token);
17 changes: 0 additions & 17 deletions src/util/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,6 @@ export const createCommands = (commands: Array<Command>): Command[] => {
return commands.map(createCommand);
};

export const registerCommands = async (
client: Client,
commands: Map<string, Command>
): Promise<void> => {
const commandArray = Array.from(commands.values()).map((cmd) => cmd.data);

try {
await client.application?.commands.set(commandArray);
commandArray.forEach((cmd) => {
console.log(`Registered command: ${cmd.type}, ${cmd.name}`);
});
console.log(`Registered ${commandArray.length} commands globally.`);
} catch (error) {
console.error('Error registering commands:', error);
}
};

export const buildCommandString = (interaction: ChatInputCommandInteraction): string => {
const commandName = interaction.commandName;
return `/${commandName} ${interaction.options.data.map((option) => `${option.name}:${option.value}`).join(' ')}`;
Expand Down
6 changes: 6 additions & 0 deletions src/util/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export async function deployCommands(): Promise<RESTPutAPIApplicationCommandsRes

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

if (config.clearGlobalCommands) {
console.log('Clearing global commands...');
await rest.put(Routes.applicationCommands(config.discord.clientId), { body: [] });
console.log('Global commands cleared.');
}

Comment on lines +10 to +15
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is temporary to fix the duplicate commands.
We were always registering global commands that triggered every time the bot started.
When this is live, we could then remove it and only rely on the deploy commands script to deploy commands.

const result = (await rest.put(
Routes.applicationGuildCommands(config.discord.clientId, config.serverId),
{
Expand Down