Slash command starter (Go)

Registers one slash command with discordgo and answers it.

Go 1.23

Environment variables you'll need

KeyWhat it is
DISCORD_TOKENYour bot's token from the Discord Developer Portal. Paste it on the Secrets tab; we store it encrypted.
GUILD_IDGuild to register the command in

Create an account to use it

Files

go.mod

module example.com/slash-starter

go 1.23

require github.com/bwmarrin/discordgo v0.28.1

go.sum

main.go

package main

import (
	"log"
	"os"

	"github.com/bwmarrin/discordgo"
)

func main() {
	s, err := discordgo.New("Bot " + os.Getenv("DISCORD_TOKEN"))
	if err != nil {
		log.Fatal(err)
	}
	s.AddHandler(func(sess *discordgo.Session, i *discordgo.InteractionCreate) {
		_ = sess.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
			Type: discordgo.InteractionResponseChannelMessageWithSource,
			Data: &discordgo.InteractionResponseData{Content: "pong"},
		})
	})
	if err := s.Open(); err != nil {
		log.Fatal(err)
	}
	defer s.Close()
	_, err = s.ApplicationCommandCreate(s.State.User.ID, os.Getenv("GUILD_ID"),
		&discordgo.ApplicationCommand{Name: "ping", Description: "replies pong"})
	if err != nil {
		log.Fatal(err)
	}
	log.Println("ready")
	select {}
}