Building a Discord Bot
Create an application on Discord Developer Portal#
- Go to https://discord.com/developers/applications (login using your discord account if required).
- Click on New Application button available at left side of your profile picture.
- Name your application and click on Create.
- Go to Bot section, click on Add Bot, and finally on Yes, do it! to confirm.
That's it. A new application is created which will hold our Slash Command. Don't close the tab as we need information from this application page throughout our development.
Before we can write some code, we need to curl a discord endpoint to register a Slash Command in our app.
Fill DISCORD_BOT_TOKEN with the token available in the Bot section and CLIENT_ID with the ID available on the General Information section of the page and run the command on your terminal.
_10BOT_TOKEN='replace_me_with_bot_token'_10CLIENT_ID='replace_me_with_client_id'_10curl -X POST \_10-H 'Content-Type: application/json' \_10-H "Authorization: Bot $BOT_TOKEN" \_10-d '{"name":"hello","description":"Greet a person","options":[{"name":"name","description":"The name of the person","type":3,"required":true}]}' \_10"https://discord.com/api/v8/applications/$CLIENT_ID/commands"
This will register a Slash Command named hello that accepts a parameter named name of type string.
Code#
Deploy the Slash Command Handler#
_10supra functions deploy discord-bot --no-verify-jwt_10supra secrets set DISCORD_PUBLIC_KEY=your_public_key
Navigate to your Function details in the Supra Dashboard to get your Endpoint URL.
Configure Discord application to use our URL as interactions endpoint URL#
- Go back to your application (Greeter) page on Discord Developer Portal
- Fill INTERACTIONS ENDPOINT URL field with the URL and click on Save Changes.
The application is now ready. Let's proceed to the next section to install it.
Install the Slash Command on your Discord server#
So to use the hello Slash Command, we need to install our Greeter application on our Discord server. Here are the steps:
- Go to OAuth2 section of the Discord application page on Discord Developer Portal
- Select
applications.commandsscope and click on the Copy button below. - Now paste and visit the URL on your browser. Select your server and click on Authorize.
Open Discord, type /Promise and press Enter.
Run locally#
_10supra functions serve discord-bot --no-verify-jwt --env-file ./supra/.env.local_10ngrok http 54321