A Discord.js based Discord chat bot. 🤖
- Create a Discord server which will serve as your test server
- Generate an Auth Token for your bot user
- Go to the Discord Developer Portal and press New Application
- Give it a name and press Create
- Go to the Bot tab in the Side Panel, press Add Bot and confirm by clicking on Yes, do it!
- Navigate to the OAuth2 tab in the Side Panel and select bot in the Scopes section. Scroll down and select the desired permissions for the bot user - the example ones as shown on the picture attached below should suffice.
- Copy the generated link from the Scopes section and paste it in your browser of choice. Select your test server as the server you want to add the bot user to and press Continue.
- Go back to the Bot tab in the Side Panel and click Copy under the Token section - this is needed in the next step.
- Go to the Discord Developer Portal and press New Application
- Set up the project locally
- Fork this repository on GitHub and clone it to somewhere on your computer
- Navigate to the project's folder in the command line
- Check out to a new branch
- Install the required dependencies by running
npm i
- This will automatically execute the setup:
- runs the
db:migrate
- to setup a local sqlite3 database file manually, runnpm run db:migrate
- builds the source (executing
npm run build:src
) - clones a fresh
.env
file
- runs the
- If you wish to create a new
.env
file, executenpm run env:new
- Check the contents of
.env
for more details on feature configuration - Inside of the
.env
file, update theTOKEN
variable and set it to the Token you copied from your Bot page, e.g.TOKEN=YOURTOKENGOESHERE
- Run tests with
npm test
(refer to the testing section) - The project should be functional at this point. Refer to these steps in order to run\debug it - if you get any errors, you probably skipped a step.
In order to run the project, you first need to compile TypeScript files into JavaScript files.
Run npm run build:src
in order to compile the src TypeScript project.
To then run the project, you can run npm start
.
In order to debug the project, in VS code go to Run (Default keyboard shortcut: Ctrl+Shift+D), then at the top choose "Launch Program" and click the green triangle (Start Debugging).
Note that you need to place your breakpoints in your source code (TypeScript/JavaScript) in the src
folder, and not in the dist
folder.
-
Create a new file under the
commands
directory:/ commands |- [command_name].ts
-
Add a command template
import { Message } from "discord.js"; import { CommandTemplate } from "../types/command.type"; import { embed } from "../services/embedService"; const executionLogic = (msg: Message, args: string[], options: any) => { /** * Command logic goes here */ // @see: https://leovoel.github.io/embed-visualizer/ // You can change the color accent by passing: // color: your_color_value_decimal // @see: https://convertingcolors.com // // About argsTitle: when true, the string after the command name in the chat // will be used as an embed title: e.g. !command Some String As An Embed Title return embed(msg, args, { argsTitle: true, description: "Embed description contents", color: [optional: decimal encoded color, white by default] }); }; const execute = (msg: Message, args: string[], options: any) => { executionLogic(msg, args, options); }; const commandTemplate: CommandTemplate = { name: "command name (without !)", description: "command description", execute: execute, }; export { commandTemplate };
-
Add additional command json configuration by adding a file under:
/ commands/config
|- [command_name].json
and then consume the configuration inside your command file:
// ...
import data from "./config/[command_name].json";
// ...
The project is using jest-ts which is a Typescript preprocessor for jest, for testing.
To run the tests, run npm test
, which compiles the jest
test files in-memory and runs them.
To understand more about how jest-ts
works, refer to their jest-ts's documentation or jest's documentation.