Skip to content

Commit 8f68f76

Browse files
committed
refactor: improve interaction handling for slash and context menu commands with enhanced error logging
1 parent 9ccd534 commit 8f68f76

1 file changed

Lines changed: 46 additions & 18 deletions

File tree

index.ts

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,68 @@ import { config } from "./env.js";
33
import { loadCommands } from "./src/utils/loadCommands.js";
44
import { registerCommands } from "./src/utils/registerCommands.js";
55

6-
// Create a new client instance
76
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
87
client.commands = new Collection();
98

109
loadCommands(client);
1110
registerCommands();
1211

1312
client.on(Events.InteractionCreate, async (interaction) => {
14-
if (!interaction.isChatInputCommand()) {
15-
return;
16-
}
13+
/**
14+
* Slash Commands
15+
*/
16+
if (interaction.isChatInputCommand()) {
17+
const command = client.commands.get(interaction.commandName);
18+
if (!command) {
19+
console.log(`Command not found ${interaction.commandName}`);
20+
return;
21+
}
1722

18-
const command = client.commands.get(interaction.commandName);
19-
if (!command) {
23+
try {
24+
await command.execute(interaction);
25+
} catch (error) {
26+
console.error(error);
27+
await interaction.reply({
28+
content: `There was an error while executing the ${interaction.commandName} command.`,
29+
ephemeral: true,
30+
});
31+
console.log(`Error executing command ${interaction.commandName}`);
32+
console.error(error);
33+
}
2034
return;
2135
}
2236

23-
try {
24-
await command.execute(interaction);
25-
} catch (error) {
26-
console.error(error);
27-
await interaction.reply({
28-
content: "There was an error while executing this command.",
29-
ephemeral: true,
30-
});
37+
/**
38+
* Message Context Menu Commands
39+
*/
40+
if (interaction.isMessageContextMenuCommand()) {
41+
const command = client.commands.get(interaction.commandName);
42+
if (!command) {
43+
console.log(`Command not found ${interaction.commandName}`);
44+
return;
45+
}
46+
47+
try {
48+
await command.execute(interaction);
49+
} catch (error) {
50+
console.error(error);
51+
try {
52+
await interaction.reply({
53+
content: `There was an error while executing the ${interaction.commandName} command.`,
54+
ephemeral: true,
55+
});
56+
} catch {
57+
console.log(`Error replying to interaction ${interaction.commandName}`);
58+
}
59+
}
3160
}
3261
});
3362

34-
// When the client is ready, run this code (only once).
35-
// The distinction between `client: Client<boolean>` and `readyClient: Client<true>` is important for TypeScript developers.
36-
// It makes some properties non-nullable.
63+
/**
64+
* Client Ready
65+
*/
3766
client.once(Events.ClientReady, (readyClient) => {
3867
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
3968
});
4069

41-
// Log in to Discord with your client's token
4270
client.login(config.discord.token);

0 commit comments

Comments
 (0)