This example creates an application command with a subcommand group, subcommands, and options. Once the command is created, it listens for an interaction (while connected to the Discord Gateway), then responds to the interaction (with a calculation based on user input). The bot waits until the program receives a signal to terminate, then disconnects from the Discord Gateway. For more information about subcommands, read the Discord API Documentation.
Use this example to create subcommands users can interact with.
You must create a Discord Application in the Discord Developer Portal to receive your Bot Token.
Assign an environment variable in the command line you will be running the program from.
set TOKEN=value
set APPID=value
export TOKEN=value
export APPID=value
NEVER SHOW YOUR TOKEN TO THE PUBLIC.
NOTE: Get the Application ID by enabling Developer Mode from the settings of your account, then right clicking your bot.
Use go build
to build the executable binary. Use subcommand
to run it from the command line.
> subcommand
Program is started.
Creating an application command...
Adding an event handler.
Connecting to the Discord Gateway...
Successfully connected to the Discord Gateway. Waiting for an interaction...
The following output results from using a command in any channel with the bot (includes direct messages).
/calculate add int
There was nothing to do.
/calculate add int 1
1
/calculate add int 2 4
6
/calculate add string
There was nothing to do.
/calculate add string 1
1
/calculate add int 2 4
24
/calculate subtract 5.3 2
3.300
User input of both options is required to submit /calculate subtract
.
Use ctrl + C
or cmd + C
in the terminal.
Exiting program due to signal...
Disconnected from the Discord Gateway.
Program executed successfully.