Documentation Index
Fetch the complete documentation index at: https://mintlify.com/disgoorg/disgo/llms.txt
Use this file to discover all available pages before exploring further.
This guide will walk you through creating your first Discord bot using DisGo. By the end, you’ll have a working bot that responds to messages.
Prerequisites
Before you begin, make sure you have:
Installation
First, create a new Go project and install DisGo:
mkdir my-discord-bot
cd my-discord-bot
go mod init my-discord-bot
go get github.com/disgoorg/disgo
go get github.com/disgoorg/snowflake/v2
Creating your bot
Set up your bot token
Create a .env file or export your bot token as an environment variable:export DISGO_TOKEN="your-bot-token-here"
Never commit your bot token to version control. Keep it secret and secure.
Write the bot code
Create a main.go file with the following code:package main
import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
)
func main() {
slog.Info("starting bot...")
slog.Info("disgo version", slog.String("version", disgo.Version))
client, err := disgo.New(os.Getenv("DISGO_TOKEN"),
bot.WithGatewayConfigOpts(
gateway.WithIntents(
gateway.IntentGuildMessages,
gateway.IntentMessageContent,
),
),
bot.WithEventListenerFunc(onMessageCreate),
)
if err != nil {
slog.Error("error while building disgo", slog.Any("err", err))
return
}
defer client.Close(context.TODO())
if err = client.OpenGateway(context.TODO()); err != nil {
slog.Error("errors while connecting to gateway", slog.Any("err", err))
return
}
slog.Info("bot is now running. Press CTRL-C to exit.")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
}
func onMessageCreate(event *events.MessageCreate) {
if event.Message.Author.Bot {
return
}
var message string
if event.Message.Content == "ping" {
message = "pong"
} else if event.Message.Content == "pong" {
message = "ping"
}
if message != "" {
_, _ = event.Client().Rest.CreateMessage(
event.ChannelID,
discord.NewMessageCreate().WithContent(message),
)
}
}
Run your bot
Start your bot:You should see:INFO starting bot...
INFO disgo version version=v0.x.x
INFO bot is now running. Press CTRL-C to exit.
Test your bot
Invite your bot to a Discord server and send “ping” in a channel. Your bot should respond with “pong”!
Understanding the code
Let’s break down what each part does:
Creating the client
client, err := disgo.New(os.Getenv("DISGO_TOKEN"),
bot.WithGatewayConfigOpts(
gateway.WithIntents(
gateway.IntentGuildMessages,
gateway.IntentMessageContent,
),
),
bot.WithEventListenerFunc(onMessageCreate),
)
This creates a new DisGo client with:
- Your bot token for authentication
- Gateway intents to receive message events
- An event listener for message creation events
Gateway intents control which events your bot receives. The IntentGuildMessages and IntentMessageContent intents are required to read message content.
Handling events
func onMessageCreate(event *events.MessageCreate) {
if event.Message.Author.Bot {
return
}
// Handle the message
}
This function is called whenever a message is created. We check if the author is a bot to avoid responding to other bots (including ourselves).
Connecting to Discord
if err = client.OpenGateway(context.TODO()); err != nil {
slog.Error("errors while connecting to gateway", slog.Any("err", err))
return
}
This establishes a WebSocket connection to Discord’s gateway to receive real-time events.
Graceful shutdown
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
This waits for a termination signal (like Ctrl+C) and keeps the bot running until you stop it.
Next steps
Now that you have a basic bot running, you can: