Discord Bot Template
If you want to make a Discord bot, this is a good place to start.
Purpose
I am often requested to make Discord bots for people, and got tired of building them from the ground up. This repository contains a set of files which are a good starting point for implementing features. It may also function as a learning resource for those interested in creating their first bot.
Pre-Packaged Functionality
This template bot comes with a few handy features:
!help
command which sends a DM to a user describing the available commands. This command takes into account the permissions required to use each command, and only informs users of commands they are authorized to use.- Command Categories make it easy to define commands for your bot, and they provide permission levels in case you need to restrict certain commands to moderators or administrators.
- Automatic registration of slash commands
- The bot will automatically create a
Moderator
role in any server it joins if the role is not already present. This role is configurable in theconfig.json
file, and is required for the command permission system to function properly.
Repository Contents
Root-level files
bot.js
- The entry point for the bot. Handles logging into Discord, and routes events to their handlersconfig.example.json
- Example configuration file. Contains data required for the bot to functionerrorHandlers.js
- Exports functions used by the bot to handle various errorslib.js
- Contains helper functions used throughout the botpackage.json
- Contains identifying data and dependency information for the bot.gitignore
- Lists files which should not be committed to gitREADME.md
- What you're reading right now!
Directories
assets
- Used to contain additional config files, images, and other assets your bot may need to referenceclientEventHandlers
- Contains files which export functions used to handle various events emitted by DiscordcommandCategories
- Contains files which define commands your users may be granted access tocron
- Contains files which should be run regularly as cron jobs. These are not loaded by the bot directlyscripts
- Contains files which should be run standalone to perform bot maintenanceslashCommands
- Contains two directories:global
- Defines slash commands available to all guildsguild
- Defines slash commands specific to a subset of guilds your bot has joined
Create and Run the Bot
Required Software
Create a Discord Application
- You will need to access the Discord Developer Portal and create an application.
- Click the "Bot" button on the sidebar.
- Click the "Add Bot" button and confirm the dialog.
- Note your bot's
Token
. - Click the "OAuth2" button on the sidebar and note your
Client ID
. - Access the Permissions Calculator and determine what permissions your
bot will need, then copy the URL at the bottom of the page. You will need to enter your bot's Client ID here, as well
as setting the scope to
applications.commands
. By default, this bot will require the following permissions:- Read Messages
- Send Messages
- View Channel
- Navigate to the link in your URL, and grant the bot access to your server.
Run the Bot
- Run the following commands in a terminal
git clone https://github.com/LegendaryLinux/DiscordBotTemplate.git
cd DiscordBotTemplate
npm install
- Create a new file called
config.json
and place it in the root directory of the project, then copy the contents ofconfig.example.json
into it. - Put the client ID and token you noted above into
config.json
where specified. - Back in your terminal, run
npm run dev
- Observe the bot has come online in your Discord server.
- Type
!hello
to have the bot reply to you.