Step 5: Adding New Commands
👉 Commands​
Registering Commands​
- Create an
.env
file in the root directory of the project. Do not upload this file to github, it contains secrets. - Make sure these environmental variables are in your
.env
file.TOKEN=<your discord bot token>
ID=<your discord bot ID>
- Enter new commands in this format, with each one on a new line in the file
commands/discord_commands.yaml
- name: <name of your command>
description: <command description>
options:
- name: <parameter 1>
description: <parameter description>
type: 3 # string
required: true
- name: <parameter 2>
description: <parameter 2 description>
type: 3 # string
required: true
- From your root directory, run
python3 register_commands.py
- You should receive the status
201
or200
printing out in your terminal.
Adding Command Functionality​
- Commands can be defined in the file
src/app/main.py
. - This bot uses the Discord Interactions API, which is a REST API. When a user executes a slash command, Discord will make an API request to the endpoint you specify in the Discord Developer Portal
- The full structure of the Interactions API can be found in the Discord API Documentation.
- For the purposes of our bot, we will turn the JSON request into a Python object and then use the
request['data']['name']
field to determine which command to run.