This project demonstrates how to configure an extensible Discord Slash Command bot that uses Configuration and Dependency Injection.
Table of Contents
- .\DiscordSlashCommandBot.Commands - Contains definitions and logic for Discord Slash Commands.
- .\DiscordSlashCommandBot.Interfaces - Contains interface definitions.
- .\DiscordSlashCommandBot\Options - Contains IOptions models for configuration.
- .\DiscordSlashCommandBot\Services - Services to be consumed by the ServiceProvider container, including the primary bot service.
- .\DiscordSlashCommandBot\Services\BotService.cs - The primary bot service responsible for the application logic.
- .\DiscordSlashCommandBot\ServicesConfiguration.cs - Where services are registered.
- .\DiscordSlashCommandBot\Statics - Stores static classes and values used throughout the application.
- .\DiscordSlashCommandBot\Program.cs - Launches the program.
The AppSettings
class defines the model for all application settings.
These can be set:
- With User Secrets for development.
- In
appSettings.json
, - Or as Environment variables prefixed with
DISCORD_BOT_
(e.g.DISCORD_BOT_LogLevel
),
User secrets will be overwritten by appSettings.json
, and those will be over written by environment variables.
You can change this precedence by changing the order in which they are add in ServicesConfiguration.cs
.
Dependency Injection is handled by .\DiscordSlashCommandBot\ServicesConfiguration.cs.
The AddBotServices()
extension method handles all configuration and registers all services and options.
- Create class (preferably ending with
Command
, e.g.EchoCommand
) and implement theIBotSlashCommand
interface. - Create and return and un-built
SlashCommandBuilder
inGetSlashCommandBuilder()
. TheBotService
will build the command and register it for you. Do not implement your own command registration! - Create your command handling logic in
SlashCommandHandler(SocketSlashCommand command)
. This will be called for you byBotService
when a user uses your slash command. - Register the command as a singleton in
ServicesConfiguration
. Example:services.AddSingleton<IBotSlashCommand, EchoCommand>();