Skip to main content

Examples

Thanks to Shadeys over on Github! I am offering examples on how to create commands like a normal reply or an embed reply.

info

Commands should always be placed in the Commands folder but can be in a sub-folder inside that folder aswell.

Normal Reply

/Commands/Public/Fun/hi.js
const {
ChatInputCommandInteraction,
SlashCommandBuilder,
PermissionFlagsBits,
} = require("discord.js");

module.exports = {
data: new SlashCommandBuilder(),
.setDefaultMemberPermissions(PermissionFlagsBits.SendMessages)
.setName("hi")
.setDescription("Says hello to you!!"),
/**
* @param {ChatInputCommandInteraction} interaction
*/
async execute(interaction) {
await interaction.reply(`Hello, ${interaction.user.username}!`);
},
};
  • First, the required dependencies are imported from the Discord.js library using the destructuring assignment syntax. These dependencies are ChatInputCommandInteraction, SlashCommandBuilder, and PermissionFlagsBits.
const {
ChatInputCommandInteraction,
SlashCommandBuilder,
PermissionFlagsBits,
} = require("discord.js");
  • Next, a JavaScript object is exported using module.exports. This object contains the command data and execution function. The data property is an instance of SlashCommandBuilder, which is used to define the properties of the command. This command is named "hi" and has a description of "Says hello to you!!". The setDefaultMemberPermissions method sets the default permission for members to send messages in the channel. The execute function is an async function that takes in a ChatInputCommandInteraction object as a parameter. This object contains the details of the user's command interaction, such as the user and channel. The function starts by using the interaction object to get the user's username and storing it in a variable named username.
async execute(interaction) {
const username = interaction.user.username;
  • Then, the function replies to the interaction with a greeting that includes the user's username.
interaction.reply(`Hello, ${username}!`);

Embed Reply

/Commands/Public/Utilities/weather.js
const {
ChatInputCommandInteraction,
SlashCommandBuilder,
EmbedBuilder,
} = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("weather")
.setDescription("Displays the weather for your city!")
.addStringOption((options) =>
options
.setName("city")
.setDescription("Provide your city name.")
.setRequired(true)
),
/**
* @param {ChatInputCommandInteraction} interaction
*/
async execute(interaction) {
const city = interaction.options.getString("city");
const temperature = "57";
const embed = new EmbedBuilder()
.setTitle(
`${interaction.member.user} Here is your weather report for ${city}!`
)
.setColor("Red")
.setTimestamp()
.setImage("https://cdn-icons-png.flaticon.com/512/218/218706.png")
.setFooter({ text: "https://github.com/josephistired" })
.setAuthor({
name: `${interaction.member.user.tag}`,
iconURL: `${interaction.user.displayAvatarURL()}`,
})
.addFields(
{
name: "User",
value: `\`\`\`${interaction.user.username}\`\`\``,
},
{
name: "Weather",
value: `${temperature}`,
}
);
await interaction.reply({
embeds: [embed],
ephemeral: true,
});
},
};
  • First, the required dependencies are imported from the Discord.js library using the destructuring assignment syntax. These dependencies are ChatInputCommandInteraction, SlashCommandBuilder, and EmbedBuilder.
const {
ChatInputCommandInteraction,
SlashCommandBuilder,
EmbedBuilder,
} = require("discord.js");
  • Next, a JavaScript object is exported using module.exports. This object contains the command data and execution function.
module.exports = {
data: new SlashCommandBuilder() /* ... */,
async execute(interaction) /* ... */,
};
  • The data property is an instance of SlashCommandBuilder, which is used to define the properties of the command. This command is named "weather" and has a description of "Displays the weather for your city!". It also has a required string option named "city" that takes the user's city as input.
data: new SlashCommandBuilder()
.setName("weather")
.setDescription("Displays the weather for your city!")
.addStringOption((options) =>
options
.setName("city")
.setDescription("Provide your city name.")
.setRequired(true)
),

  • The execute function is an async function that takes in a ChatInputCommandInteraction object as a parameter. This object contains the details of the user's command interaction, such as the user, channel, and input options.
async execute(interaction) {
// ...
}
  • The function starts by getting the user's city input from the interaction object and storing it in a variable named city.
const city = interaction.options.getString("city");
  • In this example, the temperature is hardcoded to a value of "57". However, in a real-world application, an API or npm module would be used to get the temperature data.
const temperature = "57";
  • An embed is created using the EmbedBuilder dependency. The embed has a title that includes the user's name and city, a red color, a timestamp, an image, a footer, an author that is set to the user's tag and avatar, and two fields named "User" and "Weather" with their respective values.
const embed = new EmbedBuilder()
.setTitle(
`${interaction.member.user} Here is your weather report for ${city}!`
)
.setColor("Red")
.setTimestamp()
.setImage("https://cdn-icons-png.flaticon.com/512/218/218706.png")
.setFooter({ text: "https://github.com/josephistired" })
.setAuthor({
name: `${interaction.member.user.tag}`,
iconURL: `${interaction.user.displayAvatarURL()}`,
})
.addFields(
{
name: "User",
value: `\`\`\`${interaction.user.username}\`\`\``,
},
{
name: "Weather",
value: `${temperature}`,
}
);
  • Finally, the interaction is replied to using the embed. The ephemeral option is set to true, which means that only the user who issued the command can see the reply.
interaction.reply({
embeds: [embed],
ephemeral: true,
});
info

I hope to update this in the near future to include even more examples (: