Your first bot
Create a complete Discord bot from scratch with Go, discordgo, and DiscordKit, from the Developer Portal to a working /ping command.
In this cookbook, you will create and run a real bot. At the end, it will be installed in a test server and respond to the /ping command.
The bot uses application commands, so it does not need to read messages or enable privileged intents.
What you need
Section titled “What you need”- Go 1.26 or newer;
- a Discord account;
- a test server where you can install applications;
- a terminal.
Check your Go version:
go version1. Create a Discord application
Section titled “1. Create a Discord application”- Open the Discord Developer Portal and select New Application.
- Name your application and create it.
- On the Bot page, generate or copy the bot token. Store it in a password manager. This token works like a password: do not share it or put it in your code.
- On the Installation page, configure Guild Install with the
applications.commandsandbotscopes. - For this tutorial, grant only the Send Messages permission to the bot.
- Use the Install Link to add the application to your test server.
Do not enable Message Content Intent or other privileged intents. This example receives slash command interactions through the Gateway and does not read regular messages.
2. Create the Go project
Section titled “2. Create the Go project”Create a folder and initialize the module:
mkdir my-botcd my-botgo mod init example.com/my-botgo get github.com/freitaseric/discordkitDiscordKit installs the required discordgo dependency. It requires Go 1.26 or newer.
Create a .gitignore file:
.envCreate .env to keep your token out of your code:
DISCORD_TOKEN=paste-your-token-hereDISCORD_GUILD_ID=your-test-server-idIn Discord, enable Developer Mode in User Settings → Advanced, right-click your test server icon, and select Copy Server ID. Replace the example values in the file.
Never publish the
.envfile. If your token leaks, reset it immediately on the Bot page of the Developer Portal.
3. Write the bot
Section titled “3. Write the bot”Create main.go:
package main
import ( "log" "os" "os/signal" "syscall"
"github.com/bwmarrin/discordgo" "github.com/freitaseric/discordkit")
func main() { token := os.Getenv("DISCORD_TOKEN") if token == "" { log.Fatal("DISCORD_TOKEN is not set") }
guildID := os.Getenv("DISCORD_GUILD_ID") if guildID == "" { log.Fatal("DISCORD_GUILD_ID is not set") }
session, err := discordgo.New("Bot " + token) if err != nil { log.Fatalf("create Discord session: %v", err) }
session.Identify.Intents = discordgo.IntentsGuilds
router := discordkit.NewRouter( discordkit.Recovery(), discordkit.Logging(nil), )
if err := router.Command("ping", func(c *discordkit.Context) error { return c.ReplyText("Pong! 🏓") }); err != nil { log.Fatalf("register /ping route: %v", err) }
session.AddHandler(router.Handle)
if err := session.Open(); err != nil { log.Fatalf("connect to Discord: %v", err) } defer session.Close()
commands, err := discordkit.BuildCommands( discordkit.Command("ping", "Check whether the bot is responding"), ) if err != nil { log.Fatalf("validate commands: %v", err) }
plan, err := discordkit.SyncCommands( session, session.State.User.ID, commands, discordkit.SyncOptions{GuildID: guildID}, ) if err != nil { log.Fatalf("synchronize commands: %v", err) } log.Printf("commands synchronized: %d created, %d updated, %d unchanged", len(plan.Create), len(plan.Update), len(plan.Unchanged))
log.Printf("bot connected as %s; press Ctrl+C to stop", session.State.User.Username)
stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt, syscall.SIGTERM) <-stop}The command is registered only in the test server by using GuildID. This makes it available quickly during development. SyncCommands creates or updates the declared command and leaves obsolete remote commands in place.
4. Run it
Section titled “4. Run it”On macOS or Linux, load the variables from the file and start the bot:
set -asource .envset +ago run .In PowerShell:
Get-Content .env | ForEach-Object { if ($_ -match '^([^#][^=]*)=(.*)$') { [Environment]::SetEnvironmentVariable($matches[1], $matches[2]) }}go run .Keep the process running. In your test server, type /ping and run the command. The bot should respond with Pong! 🏓.
What just happened
Section titled “What just happened”discordgo.Sessionopened a connection to the Discord Gateway;router.Handleforwarded incoming interactions to DiscordKit;router.Commandconnected thepingroute to its handler;BuildCommandsvalidated the slash command declaration;SyncCommandsregistered the declaration in your test server;ReplyTextsent the initial interaction response.
Registering a route and publishing a command are separate steps: the route handles the interaction; synchronization makes the command appear in Discord.
Troubleshooting
Section titled “Troubleshooting”The /ping command does not appear: make sure the bot is installed in the correct server with the applications.commands scope, DISCORD_GUILD_ID is that server’s ID, and the program output confirms synchronization.
The bot appears offline: make sure the process is still running and the token is valid. Generate a new token in the Developer Portal if the old one was exposed.
401: Unauthorized: the token is invalid or was copied with spaces. Generate a new token and update DISCORD_TOKEN.
403: Missing Access while synchronizing: confirm the server ID and install the application there with the applications.commands scope.
Next steps
Section titled “Next steps”- Learn how the Router works in Router.
- Understand responses and the interaction lifecycle in The interaction model.
- Continue with Your first command to learn more about command declaration, synchronization, and routing.
For production, keep the token in a secrets manager, run the process under a supervisor, and synchronize global commands deliberately.