diff --git a/code-samples/creating-your-bot/event-handling/commands/utility/ping.js b/code-samples/creating-your-bot/event-handling/commands/utility/ping.js new file mode 100644 index 000000000..64e2ba6f9 --- /dev/null +++ b/code-samples/creating-your-bot/event-handling/commands/utility/ping.js @@ -0,0 +1,10 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('ping') + .setDescription('Replies with Pong!'), + async execute(interaction) { + await interaction.reply('Pong!'); + }, +}; diff --git a/code-samples/creating-your-bot/event-handling/commands/utility/server.js b/code-samples/creating-your-bot/event-handling/commands/utility/server.js new file mode 100644 index 000000000..277a28a4a --- /dev/null +++ b/code-samples/creating-your-bot/event-handling/commands/utility/server.js @@ -0,0 +1,10 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('server') + .setDescription('Provides information about the server.'), + async execute(interaction) { + await interaction.reply(`This server is ${interaction.guild.name} and has ${interaction.guild.memberCount} members.`); + }, +}; diff --git a/code-samples/creating-your-bot/event-handling/commands/utility/user.js b/code-samples/creating-your-bot/event-handling/commands/utility/user.js new file mode 100644 index 000000000..6007c727e --- /dev/null +++ b/code-samples/creating-your-bot/event-handling/commands/utility/user.js @@ -0,0 +1,10 @@ +const { SlashCommandBuilder } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('user') + .setDescription('Provides information about the user.'), + async execute(interaction) { + await interaction.reply(`This command was run by ${interaction.user.username}, who joined on ${interaction.member.joinedAt}.`); + }, +}; diff --git a/code-samples/creating-your-bot/event-handling/config.json b/code-samples/creating-your-bot/event-handling/config.json index 56f07a696..14bc10ce5 100644 --- a/code-samples/creating-your-bot/event-handling/config.json +++ b/code-samples/creating-your-bot/event-handling/config.json @@ -1,3 +1,5 @@ { + "clientId": "123456789012345678", + "guildId": "876543210987654321", "token": "your-token-goes-here" } diff --git a/code-samples/creating-your-bot/event-handling/events/interactionCreate.js b/code-samples/creating-your-bot/event-handling/events/interactionCreate.js index f9d08c24e..a4c31226d 100644 --- a/code-samples/creating-your-bot/event-handling/events/interactionCreate.js +++ b/code-samples/creating-your-bot/event-handling/events/interactionCreate.js @@ -15,8 +15,12 @@ module.exports = { try { await command.execute(interaction); } catch (error) { - console.error(`Error executing ${interaction.commandName}`); console.error(error); + if (interaction.replied || interaction.deferred) { + await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); + } else { + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + } } }, }; diff --git a/guide/creating-your-bot/event-handling.md b/guide/creating-your-bot/event-handling.md index 74d59dfdd..3702907eb 100644 --- a/guide/creating-your-bot/event-handling.md +++ b/guide/creating-your-bot/event-handling.md @@ -31,8 +31,12 @@ client.on(Events.InteractionCreate, async interaction => { try { await command.execute(interaction); } catch (error) { - console.error(`Error executing ${interaction.commandName}`); console.error(error); + if (interaction.replied || interaction.deferred) { + await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); + } else { + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + } } }); ``` @@ -95,8 +99,12 @@ module.exports = { try { await command.execute(interaction); } catch (error) { - console.error(`Error executing ${interaction.commandName}`); console.error(error); + if (interaction.replied || interaction.deferred) { + await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); + } else { + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + } } }, };