A simple discord bot template using discord.js.
- Create a repository from this template
OR
clone this repository. - Use your favourite package manager (Yarn is recommended) to install the dependencies,
npm install
oryarn
. - Run
npm start
oryarn start
to start the bot.
- Create a
.env
file in the root directory of the project. - Add the following variables to the
.env
file:
TOKEN=your-bot-token
CLIENT_ID=your-bot-client-id
GUILD_ID=your-guild-id # Optional but is required for easy testing of slash commands.
- To create a slash command, create a new file in the
commands
directory. - Use this code to get started:
import { SlashCommandBuilder } from "discord.js";
// This is required export for the main file to import the command.
export const data = new SlashCommandBuilder()
.setName("name")
.setDescription("description");
export async function execute(interaction) {
// Slash command response goes here.
}
- The command should be registered every time the bot is started.
- To create an event, create a new file in the
events
directory. - Here is an example:
import { Events } from "discord.js";
export const event = Events.YourEventName; // The event name. Refer to the discord.js documentation for more info.
export const once = true; // Whether the event should only be executed once, when recieved.
export async function execute(client) {
// Event code goes here.
}
- To create an interaction, create a new file in the
interactions
directory. - Here is an example:
export const interactionId = "ID"; // The interaction id you gave when registering the interaction.
export async function execute(interaction) {
// Interaction response code goes here.
}