Welcome! If you've never have made a Discord bot I recommend you to read the Setup section and it will guide you across the Discord's bot creationg process
Otherwise if you have created a Discord bot before and you have it registered on the Discord Developer's portal page and you just want to use the Boilerplate go to Step 9 of the Setup and then go to the Create new command section to learn how the boilerplate works and how to create a new command for your bot
In order to make our discord bot, first we have to create our application on the Discord Developer's portal page, go to the page, login with your Discord account and press the "New Application" button.
Then it will show you a popup where you will write your project's name and click the "Create" button
If you don't know what name to write, simply write your bot's name
Go to the "Bot" page
Click the "Add Bot" button
Change the "Username" field to the name you want your Discord bot to have and click on the "Reset Token" button
Copy the token
In order to invite the bot to your Discord Channel you'll have to go to the URL Generator and check application.commands
, bot
and Administrator
options so the bot can work well.
IMPORTANT: If you don't check the application.commands
option the bot won't work.
This will generate a link at the bottom of the page that you can copy to add the bot to your server.
Create a .env
file (you can use the example.env as a template) and complete the the blanks with your bot's tokens
You can use the same token in both variables
In order to create a new command you have to create a new .js file in the "commands" folder with the name of the command you want to create. This file should have the following structure
const description = "Command's description"
// The main function
const init = (interaction, client) => {
// ...
}
module.exports = { init, description }
If you want to create a command with params you'll have to create a new .js file in the "commands" folder with the name of the command and the file should have the following structure
const DiscordJS = require('discord.js')
const description = 'Show Message'
/*
There are some param types you can use, see the DiscordJS docs in order to know which one to use.
https://discordjs.guide/interactions/slash-commands.html#option-types
*/
const PARAM_TYPE = DiscordJS.Constants.ApplicationCommandOptionTypes.STRING
// Here you have to create an object for each param you want to create
const options = [
{
name: 'param_name', // Name of the param
description: 'Text to Show', // Description of the param
required: true, // Check if the param is obligatory
type: PARAM_TYPE, // The type of param
},
]
const init = (interaction, client) => {
/*
Instread of "getString" you might use other method, it depends on the type of param you are using,
see the DiscordJS documentation in order to know which one to use.
https://discordjs.guide/interactions/slash-commands.html#parsing-options
*/
const text = interaction.options.getString('param_name')
interaction.reply('The text is: ' + text)
}
module.exports = { init, description, options }
npm install
If you want to run the bot for production just write on your console
npm start
If you want to run the bot for development write on your console
npm run dev
If you want the bot to watch the changes and reload every time you modify a file write on your console
npm run dev:watch
If you want to create an executable run
npm run build
The TOKEN_DEV
variable is used when you execute the npm run dev
and npm run dev:watch
commands on your console. The TOKEN_PROD
is used when you use npm start
and npm run build
.
NOTE: You can put the same token on both variables.