Skip to content

Latest commit

 

History

History

command

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Example: Create an Application Command

This example creates an application command, listens for an interaction (while connected to the Discord Gateway), then responds to the interaction. The bot waits until the program receives a signal to terminate, then disconnects from the Discord Gateway and deletes the created command. For more information about Application Commands, read the Discord API Documentation.

Use this example to create commands users can interact with.

Setup

You must create a Discord Application in the Discord Developer Portal to receive your Bot Token.

Environment Variables

Assign an environment variable in the command line you will be running the program from.

Windows

set TOKEN=value
set APPID=value

Mac/Linux

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.

Usage

Use go build to build the executable binary. Use command to run it from the command line.

> command
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...

/main

main called by SCB.
Creating a response to the interaction...
Sent a response to the interaction.

SIGINT

Use ctrl + C or cmd + C in the terminal.

Exiting program due to signal...
Disconnected from the Discord Gateway.
Deleting the application command...
Program executed successfully.

Read More

Example Description
subcommand Create an application command with subcommand groups, subcommands, and options.
localization Create an application command with a localized name and description.
autocomplete Create an application command with required option choices and send autocompletion choices based on user input.
followup Create interaction responses and followup messages.

FAQ

I am receiving Invalid interaction application command from Discord when I send an interaction.

Discord propagates registered Global Application Commands. As a result, it can take time to add or update a Global Application Command. In addition, the user's client must reload the commands that are available to them, so that the user's client selects the new propagated Global Application Command (with a new ID and Token). In contrast, Guild Application Commands (registered via CreateGuildApplicationCommand) are updated instantly. Due to this behavior, use Guild Application Commands to test your application without waiting for propagation. For more information, read the Discord API Documentation.

I am receiving a nil pointer dereference when the Bot's Application Command is used in a DM or Guild.

When an Application Command is used in a direct message, the Interaction.User field is provided, while the Interaction.Member is NOT. When an Application Command is used in a guild, the Interaction.Member field is provided, while the Interaction.User is NOT. For the sake of simplicity, these examples assume that you will use your command in a Direct Message Channel. To protect against this behavior in production-level code, ensure that the Interaction.User or Interaction.Member is != nil before referencing their fields.