discordbooter is a package that extends discordgo to spin up a discord bot to log or consume messages from / to a discord channel.
go get github.com/zaskoh/discordbooter
To get started you need a discord bot and a token from discord. Follow these articles to prepare it.
- https://discord.com/developers/docs/getting-started#creating-an-app
- https://discord.com/developers/docs/getting-started#configuring-a-bot
- add scope bot + all bot permissions needed (send message / ...) and invite bot (OAuth2) to your server
The bot will run as a goroutine. To handle the shutdown gracefully you need a context and a waitgroup.
An example how to use it can be found under examples.
go run example/example.go --token xxx --channel yyy
import "github.com/zaskoh/discordbooter"
err := discordbooter.Start(ctx, &wg, *token)
if err != nil {
log.Fatalf("booting failed %s", err)
}
// define a function you want to add
func discord_handler_x(s *discordgo.Session, m *discordgo.MessageCreate){
// code goes here
}
// define an array of handlers
var handlers = []interface{}{
func(s *discordgo.Session, r *discordgo.Ready) {
log.Println("discord bot started")
},
discord_handler_x,
}
// add them
err = discordbooter.AddHandlers(handlers)
if err != nil {
log.Printf("unable to add handlers: %s", err)
}
err := discordbooter.SendMessage(*channel, "hello world!")
if err != nil {
log.Printf("unable to send message: %s", err)
}