Skip to content

JoaquinGiordano/discord-bot-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Discord Bot Boilerplate

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

Setup

Step 1

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.

Step 1

Step 2

Then it will show you a popup where you will write your project's name and click the "Create" button

Step 2

If you don't know what name to write, simply write your bot's name

Step 3

Go to the "Bot" page

Step 3

Step 4

Click the "Add Bot" button

Step 4

Step 5

Change the "Username" field to the name you want your Discord bot to have and click on the "Reset Token" button

Step 5

Step 6

Copy the token

Step 6

Step 7

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.

Step 7

Step 8

This will generate a link at the bottom of the page that you can copy to add the bot to your server.

Step 8

Step 9

Create a .env file (you can use the example.env as a template) and complete the the blanks with your bot's tokens

Step 9

You can use the same token in both variables

Create new command

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 }

Create new command with params

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 }

Install Dependencies

npm install

Run the bot

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

Build and create an executable

If you want to create an executable run

npm run build

Diferences between TOKEN_PROD and TOKEN_DEV

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.

About

A DiscordJS boilerplate to create your own discord bot

Topics

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •