A template implementation of a Discord bot using Node.js and Typescript
See the implementation of a sample command at src/commands/example.ts
This is starter code for running a single server discord bot. The server's ID is an environment variable. However, this could be reworked to get server(guild) IDs another way
- Make sure you have Node installed and are using version
16.^
, have git installed, a Discord account, and have a GitHub account - Clone this repository and then:
cd <this-cloned-repo-location>
- Create a new repository on GitHub for your new project to duplicate your cloned repository into
git remote set-url origin <url-of-new-repo>
git push -u origin master
- Create a
.env
file in the project's root directory and add environment variables- Add
BOT_TOKEN=YOUR_BOT_TOKEN_HERE
Get Discord Bot ID - Add
GUILD_ID=YOUR_GUILD_ID_HERE
- In Discord, open your User Settings by clicking the Settings Cog next to your user name on the bottom.
- Go to Appearance and enable Developer Mode under the Advanced section, then close User Settings.
- Open your Discord server, right-click on the server name, then select Copy ID
- Paste the Server ID in you .env file
- If desired, you can disable Developer Mode now.
- Add
- Run
npm run build
thennpm start
- If properly setup, you should see the following in your console:
Env variables loaded successfully Starting Discord bot client... Discord bot client ready!
- Start customizing commands in
src/commands/example.ts
- Or create a new command in
src/commands
// src/commands/newCommand.ts import { SlashCommandBuilder } from "@discordjs/builders"; import { MessageEmbed } from "discord.js"; import { Command } from "../interfaces/Command"; export const newCommand : Command = { data: new SlashCommandBuilder() // command entered as: /testcommand .setName('testcommand') .setDescription('description of command'), run: async (interaction) => { // allow for response to take longer than 3 seconds await interaction.deferReply() const embedResponse = new MessageEmbed(); embedResponse.setTitle('Response title here') embedResponse.setDescription('Response description here') await interaction.editReply({embeds: [embedResponse]}); } }
- then in
src/_CommandList.ts
append your new command to the list:import { Command } from "./interfaces/Command"; import { command } from "./commands/example"; import { newCommand } from "./commands/newCommand" export const CommandList: Command[] = [command, newCommand];
- BEFORE DEPLOYING TO HEROKU: FOLLOW THE LOCAL DEVELOPMENT GUIDE to setup repository for your bot and ensure it works properly with discord in your local dev environment
- Ensure Procfile (no extension) exists in your project's root directory with contents:
worker: npm start
- Create a Heroku App
- Set up Automatic Deployments with Github
- Ensure you have your most recent
BOT_TOKEN
from you Discord Bot admin panel - Set config vars on the settings page of your Heroku app dashboard:
BOT_TOKEN=YOUR_BOT_TOKEN_HERE GUILD_ID=YOUR_GUILD_ID_HERE