GoBot
A personal Discord bot project built with Go, utilizing the DiscordGo library for Discord API integration.
Table of Contents
-
About
-
Getting Started
- Usage
- Features
- Command Registration
About
Gobot is a side project of mine which was solely created to understand the "Go" language better.
Built With
- Go - The programming language used
- DiscordGo - Go package for Discord API integration
- GoDotEnv - Environment variable management
- Docker - Containerization
Getting Started
Prerequisites
Installation
- Clone the repository
git clone https://github.com/bluejutzu/GoBot.git
- Install dependencies
go mod download
- Create a
.env file in the root directory with the following:
BOT_TOKEN="your_bot_token_here"
or rename the .env.example to .env and change the BOT_TOKEN value.
Usage
Running Locally
go run main.go
Using Docker
# Build the container
docker build -t gobot .
# Run the container
docker run -d --name gobot --env-file .env gobot
Updating Docker Container
When you make changes to your .env file or any other files, you'll need to rebuild and restart the container:
# Using Docker Compose (recommended):
docker compose down
docker compose up -d --build
# Or using Docker directly:
docker stop gobot
docker rm gobot
docker build -t gobot .
docker run -d --name gobot --env-file .env gobot
Features
/ping - Check if the bot is online
/what-is-my-id - Get your Discord user ID
- Working on more features
Command Registration
Commands in GoBot are registered through a structured system:
-
Command Definition
- Commands are defined as ApplicationCommand structs in the
commands slice
- Each command specifies its
name, description, and any options
-
Command Handler Mapping
- Each command has a corresponding handler function in the
commandHandlers map
- Handlers process the command when it's triggered by a user
-
Registration Process
- Commands are automatically registered with Discord's API during bot startup
- The bot creates
application_commands for each defined command
Example of how commands are structured:
package misc
// Command definition
var ExampleCommand = &discordgo.ApplicationCommand{
Name: "example",
Description: "An example command",
}
// Hanlder function
func HandleExampleCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
// ...add code here
}
And in the bot/bot.go file:
// command mapping
commands = []*discordgo.ApplicationCommand {
misc.ExampleCommand,
}
// Handler mapping
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) {
"example": HandleExampleCommand,
}
Command Line Interface (CLI)
Gobot has it's own CLI, named gobot, that has tools and utilities for this certain project. As of now the CLI is available in the repository.