Hey there, fellow Go enthusiast! Ready to dive into the world of Discord bots? You're in for a treat. We'll be using the awesome discordgo
package to create a Discord API integration that'll make your bot dreams come true. Let's get cracking!
Before we jump in, make sure you've got:
Got those? Great! Let's move on.
First things first, let's create a new Go module:
mkdir discord-bot cd discord-bot go mod init github.com/yourusername/discord-bot
Now, let's grab the discordgo
package:
go get github.com/bwmarrin/discordgo
Alright, time to write some code! Create a main.go
file and let's get started:
package main import ( "fmt" "os" "os/signal" "syscall" "github.com/bwmarrin/discordgo" ) func main() { token := "YOUR_BOT_TOKEN" dg, err := discordgo.New("Bot " + token) if err != nil { fmt.Println("Error creating Discord session:", err) return } err = dg.Open() if err != nil { fmt.Println("Error opening connection:", err) return } fmt.Println("Bot is now running. Press CTRL-C to exit.") sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc dg.Close() }
Don't forget to replace "YOUR_BOT_TOKEN"
with your actual bot token!
Let's make our bot do something cool. We'll add a message handler:
func init() { dg.AddHandler(messageCreate) } func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if m.Author.ID == s.State.User.ID { return } if m.Content == "!ping" { s.ChannelMessageSend(m.ChannelID, "Pong!") } }
Add this to your main.go
, and your bot will respond "Pong!" whenever someone says "!ping". Simple, but effective!
Ready to level up? Let's add a slash command:
var ( commands = []*discordgo.ApplicationCommand{ { Name: "hello", Description: "Says hello", }, } commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ "hello": func(s *discordgo.Session, i *discordgo.InteractionCreate) { s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ Content: "Hello there!", }, }) }, } ) func init() { dg.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok { h(s, i) } }) }
Don't forget to register your commands in the main
function:
s.ApplicationCommandBulkOverwrite(s.State.User.ID, "", commands)
Always be prepared for the unexpected! Add some error handling and logging:
log.Printf("Error occurred: %v", err)
Use this instead of fmt.Println
for better logging practices.
Ready to unleash your bot on the world? Consider these hosting options:
Remember to use environment variables for sensitive info like your bot token:
token := os.Getenv("BOT_TOKEN")
And there you have it! You've just built a Discord bot using Go. Pretty cool, right? Remember, this is just the beginning. The Discord API is vast, and there's so much more you can do. Keep exploring, keep coding, and most importantly, have fun!
For more info, check out the discordgo
documentation and the Discord API docs. Happy coding!