Something went wrong. Try again.
Simple template for Discord bots, using Discord.Net
Something went wrong. Try again.
Files
- C# 100%
README.md
Discord Bot Template #
A simple Discord bot template built with Discord.NET, designed as a starting point for creating your own Discord bots with slash commands.
Features #
- Slash Commands: Modern Discord interactions using Discord.Net's InteractionService
- Modular Architecture: Organize commands into separate modules for easy maintenance
- Logging: Integrated console logging with Microsoft.Extensions.Logging
- Configuration: Environment variable support with .env file loading
- Dependency Injection: Services registered in the main bot class and automatically injected into command modules
Prerequisites #
- .NET 9.0 SDK
- A Discord bot token (create one in the Discord Developer Portal)
Setup #
-
Create a new repository from this template in GitHub
-
Create your Discord bot application:
- Go to the Discord Developer Portal
- Create a new application
- Go to the "Bot" section and create a bot
- Copy the bot token
-
Invite the bot to your server:
- In the Developer Portal, go to "OAuth2" > "URL Generator"
- Select scopes:
botandapplications.commands - Select permissions: Send Messages, Use Slash Commands, etc.
- Use the generated URL to invite the bot to your server
-
Configure the bot:
- Create a
.envfile in the project root - Add your bot token:
BOT_TOKEN=your_bot_token_here
- Create a
-
Restore dependencies:
dotnet restore
Running the Bot #
dotnet run
The bot will connect to Discord and register its slash commands globally. You should see confirmation in the console.
Usage #
The template includes example commands in Modules/GeneralModule.cs:
/ping- Basic ping command/echo <message>- Echoes back your message/adminonly- Admin-only command (requires administrator permissions)/dm- Command that only works in DMs
Adding New Commands #
- Create a new class in the
Modulesfolder that inherits fromInteractionModuleBase<SocketInteractionContext> - Add methods decorated with
[SlashCommand("commandname", "description")] - The bot will automatically register new commands when it starts
Modules can inject services via their constructors for dependencies like databases or APIs.
Example:
// SlashCommand takes the command name and description
[SlashCommand("hello", "Says hello")]
public async Task HelloCommand() =>
await RespondAsync("Hello!");
Configuration #
BOT_TOKEN: Your Discord bot token (required)- The bot uses specific gateway intents. Modify in
DiscordBot.csif needed.
Project Structure #
DiscordBot.cs- Main bot class, entry point, and dependency injection setup (registers client, interactions, logger)Modules/- Command modules (inherit fromInteractionModuleBase, can inject services via constructors)DiscordBot.csproj- Project configuration and dependencies
Dependencies #
- Discord.Net 3.18.0 - Discord API wrapper
- DotNetEnv - Environment variable loading
- Microsoft.Extensions.Logging - Logging framework
Contributing #
This is a template project. Feel free to modify and extend it for your own Discord bots.
License #
This project is released under the Unlicense, placing it in the public domain.