/discordbooter

spin up a discord bot to log or consume messages from / to a discord channel with go

Primary LanguageGoMIT LicenseMIT

discordbooter

Go reference GitHub Workflow Status Go Report Card go version license GitHub Release

discordbooter is a package that extends discordgo to spin up a discord bot to log or consume messages from / to a discord channel.

Getting started

Installing

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.

Info

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.

Example

go run example/example.go --token xxx --channel yyy

Usage

Import the package into your project

import "github.com/zaskoh/discordbooter"

Start the bot

err := discordbooter.Start(ctx, &wg, *token)
if err != nil {
    log.Fatalf("booting failed %s", err)
}

Add handlers

// 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)
}

Send message

err := discordbooter.SendMessage(*channel, "hello world!")
if err != nil {
    log.Printf("unable to send message: %s", err)
}