A template for creating Discord bots using Go, featuring a clean command structure, logging, and environment configuration.
To try out the example bot, you can install it using the following link:
- Slash command support with validation
- Subcommand handling
- Structured logging with Zap
- Environment configuration with .env
- Clean, modular command architecture
- Example commands included
- Go 1.16 or higher
- A Discord bot token
- Discord application with slash commands enabled
- Clone the repository:
git clone https://github.com/maks112v/discord-bot-go
cd discord-bot-go
- Install dependencies:
go mod download
- Copy the example environment file:
cp .env.example .env
- Add your Discord bot token to
.env
:
DISCORD_TOKEN="your-bot-token-here"
- Run the bot:
go run main.go
This template includes VS Code debugging configuration. To debug the bot:
- Open the project in VS Code
- Set any breakpoints in your code
- Press F5 or select "Run and Debug" from the sidebar
- Select "Launch Bot" from the debug configuration dropdown
The bot will start in debug mode, allowing you to:
- Step through code execution
- Inspect variables
- Set breakpoints
- Use debug console
discord-bot-go/
├── main.go # Entry point
├── .env # Environment configuration
└── pkg/
└── commands/ # Command implementations
├── commands.go # Command handler and registration
├── ping.go # Simple ping command
├── subcommands.go # Command with subcommands
├── validation.go # Command with validation
└── followup.go # Command with follow-up messages
- Create a new command file in
pkg/commands/
- Implement the
Command
interface:
type Command interface {
Name() string
Description() string
Options() []*discordgo.ApplicationCommandOption
Validate(s *discordgo.Session, i *discordgo.InteractionCreate, username string) error
Execute(s *discordgo.Session, i *discordgo.InteractionCreate) error
}
- Register your command in
commands.go
/ping
- Basic ping command/validation
- Example of command with guild-only validation/followup
- Example of using follow-up messages in threads/subcommand
- Example of command with subcommands/subcommand list
/subcommand create
Unlicense license. See LICENSE for more information.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.