Docs · Discord bots

Host a Go Discord bot (discordgo)

Upload your Go code with go.mod and go.sum; we build it on every start and run it. The token stays in a .env file.

Startup → Docker Image → Go 1.23. Upload your code with go.mod and go.sum; we build it on every start (only what changed, so it’s quick after the first time) and run it.

The files

On your computer, in your bot’s folder:

go mod init mybot
go get github.com/bwmarrin/discordgo github.com/joho/godotenv

main.go:

package main

import (
	"log"
	"os"
	"os/signal"
	"syscall"

	"github.com/bwmarrin/discordgo"
	"github.com/joho/godotenv"
)

func main() {
	_ = godotenv.Load()
	dg, err := discordgo.New("Bot " + os.Getenv("DISCORD_TOKEN"))
	if err != nil {
		log.Fatal(err)
	}
	dg.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
		log.Printf("Logged in as %s", r.User.Username)
	})
	if err := dg.Open(); err != nil {
		log.Fatal(err)
	}
	defer dg.Close()

	stop := make(chan os.Signal, 1)
	signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
	<-stop
}

.env holds the token: DISCORD_TOKEN=your-token-here.

Upload go.mod, go.sum, your .go files and .env, then press Start.

Good to know

  • main package in a folder (like cmd/bot/main.go): type that path in Main file.
  • Already built it? Build for Linux on your computer (GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o bot), upload the bot file and type bot in Main file. Turn off Install packages on start.
  • Packages that need C (like mattn/go-sqlite3) can’t build here. Use a pure-Go one instead (like modernc.org/sqlite).
  • The signal.Notify lines let Stop close the bot cleanly.

More help: Run your Discord bot 24/7.

Still stuck?

Open a ticket with your service name and the console error, and a real person helps you.