
This post will go over command handling with Discord.Net. Whether you are following along from the Raspberry Pi series, or are just curious about how to do command handling with Discord.Net, welcome!
I am currently in the process of updating this guide to use the new /Slash Command implementation, as you will need a new intent to use the old way (having the bot parse messages)
.NET 6.x
…and we will be building upon the framework laid out here:
If you want to follow along with the finished code, go here: https://github.com/gngrninja/csharpi/tree/02-command-basics.
If you would like the starter code, and want to try building off of it yourself, go here: https://github.com/gngrninja/csharpi/tree/intro.
To keep up with Discord.Net examples and specifics, their repo has code samples: Discord.Net/samples at dev · discord-net/Discord.Net (github.com)
The first thing we’ll want to do is add the command handling service.
This service will be responsible for:
Hooking into the SlashCommandExecuted, ContextCommandExecuted, and ComponentCommandExecuted events to process commands as they come in (to see if they are a valid command), and handle command execution (success/failure/not found actions)
Utilizing Dependency Injection in .NET to setup and pass through services/configurations
Loading all the command modules that inherit from InteractionModuleBase
The first thing we want to do here is create the command handling service.
Create a folder named Services, and under that folder a file named CommandHandler.cs.
2. Here is the code for the command handling service, with comments to help understand what is happening (don’t worry too much about not understanding what is happening, yet!):
You can always view the most updated code, here: https://github.com/gngrninja/csharpi/blob/02-command-basics/Services/CommandHandler.cs.
[top]
Remember :
When debugging you want the config.json file in /bin/Debug/net6.0/
When running the bot normally via dotnet projectname.dll, you want the config.json file in the root folder (same folder as the .dll/executable)
You can find your Discord guild(server) id by right clicking on your server name, and going to Copy ID
To add the Guild id, your config.json should look like this:
{
"Token": "ThIsIsNtMyToKeN",
"TestGuildId": "123456789101112131415"
}
Now what we have created our CommandHandler service, let’s wire up Program.cs to enable dependency injection and use it.
[top]
Add NuGet Package for Dependency Injection
When using the Discord.Net v3.2.0 library, I noticed that I had to manually add the Dependency Injection package. To do this, drop down to your command line, navigate to the project folder, and run:
dotnet add package Microsoft.Extensions.DependencyInjection --version 6.0.0
dotnet restoreNext we’ll want to modify Program.cs to add dependency injection.
What we need to do in Program.cs:
Add a using statement to gain access to the service we created
- For this example that’s using chsarpi.Services;
Create a method that will construct the dependency injection model / ServicesProvider for the model we can consume later (ConfigureServices)
Call upon and use the ConfigureServices method, and work with the bot code via dependency injection
Below is the sample code to achieve what I’ve gone over above:
You can always view the most updated code, here: https://github.com/gngrninja/csharpi/blob/02-command-basics/Program.cs
Now that we’re wired up to use dependency injection, and have our service created to handle commands, let’s create our first set of commands (the right way!).
[top]
In this section we will be writing our first real, robust command. Writing commands this way gives us access to Discord.Net’s command writing goodness.
1. Create a folder named Modules, and under that folder a file named ExampleCommands.cs.
ExampleCommands will contain our first command using this framework!
Below I have some code that will get us started with an 8-ball command:
You can always find the most up to date code, here: https://github.com/gngrninja/csharpi/blob/02-command-basics/Modules/ExampleCommands.cs
That’s it! Now we’re ready to debug/test to see if it is all working!
Remember it is F5 in VS Code, or you can use [Debug] -> [Start Debugging] from the menu bar.
Now that the bot is running, we can test our 8-ball command.
Go into the server your bot is in, and type the following:
/8
The command should show up as an autocomplete, and if you hit enter it will ask for the question:
Type out your question, and hit enter!
[top]
To get this working on our Raspberry Pi we will simply need to push the updated code to the Github repo, and pull it down to the Pi. We will need to update our config.json on the Pi, and copy it to the debug folder, as well as the bot folder (after we’ve added the Prefix line). If you’d like more information on getting the initial setup done with the Raspberry Pi, visit the below post and check out it’s prerequisites as well!
The below steps all assume we are sshed into our Pi.
If you are following along, and want to use my Github repo as reference, you must ensure you’re working with the proper branch for this post.
First you’ll want to get into the directory:
cd csharpiThen, you want to run:
git checkout 02-command-basicsgit pull
You can see here we are in the intro branch, and we need to switch it up! [/caption]
Now that we have the latest code on the Pi (refresher here if you’re using your own repo), let’s edit the config.json file to add the command prefix we want to use.
You’ll want to be in the cloned git repo’s directory, and assuming you have the base config created there from the linked post above, run (if you don’t, create it by using touch config.json):
nano config.json
In this file we want to ensure the Token and TestGuildId are there as such:
{
"Token": "ThIsIsNtMyToKeN",
"TestGuildId": ";"
}
Use the following sequence to save the contents:
CTRL+O
[Enter]
CTRL+XNow let’s copy that to the debug folder so we can test/debug the code, and to the bot’s published location:
cp config.json bin/Debug/net6.0/
cp config.json ~/bot
Let’s test the code from the Git repo…
Now that we copied the new config.json file over to the Debug folder, we can test things out. Ensure you’re in the repo’s folder and run (remember, this method of running the bot takes a while to start):
dotnet run
Now to run a command in Discord just to be sure…
Success! Now to get it published and run it properly.
Let’s get things published and running smoothly! To start out, ensure you are in the git repo’s folder and run:
dotnet publish -o ~/botThen you’ll want to get into the bot’s folder:
cd ~/bot
And finally, you can run:
dotnet csharpi.dll
Looks good, but one more test with Discord to really be sure!
In this post we added some proper command handling to our Discord bot.
Feel free to change things around and see what you can make happen (or break and fix… break + fix = learn, right?).
In the next part of this series, we will add proper logging to the bot.
If you have any questions or comments, leave them below!
No comments yet. Be the first!