bolt/README.md
jake dd20b73b76 adding log level option (#1)
Reviewed-on: #1
Co-authored-by: jake <jake.young.dev@gmail.com>
Co-committed-by: jake <jake.young.dev@gmail.com>
2025-06-25 22:51:32 +00:00

2.6 KiB

bolt

The nuts and bolts of a Discord bots. Bolt is a wrapper for discordgo that provides very quick bootstrapping for simple Discord bots.

Usage

Token

Bolt requires a Discord bot token to run, the token must be set as an environment variable labeled "DISCORD_TOKEN"

Commands

Commands are represented by the Command struct. Any roles in the Command struct can run the command, if the Roles field is empty anyone can run the command.

type Command struct {
	Trigger string        //command that triggers payload NOT including the indicator
	Payload Payload       //payload function to run when a command is detected
	Timeout time.Duration //the amount of time before command can be run again
	lastRun time.Time     //timestamp of last command run
	Roles   []string      //roles that can use command, if none are set anyone can run the command
}

Payload

Payload functions are executed when a command is detected, if no errors are returned and the returned string is not empty, then the returned string is sent in reply to the command message.

type Payload func(msg Message) (string, error)

Example

package main

import (
	"time"

	"code.jakeyoungdev.com/jake/bolt"
	_ "github.com/joho/godotenv/autoload"
)

func main() {
	//bolt defaults the command indicator to '.' however that can be changed with the options
	//Example: bolt.New(bolt.WithIndicator('!')) would support commands like !ping
	b := bolt.New(bolt.WithLogLevel(bolt.LogLevelCmd))

	b.AddCommands(
        // .ping can be run at any time by anyone and will respond with 'pong'
		bolt.Command{
			Trigger: "ping",
			Payload: func(msg bolt.Message) (string, error) {
				return "pong", nil //any strings returned will be sent in response to the Discord message
			},
		},
        // .time can be run every 25 seconds by anyone and will respond with 'yer'
		bolt.Command{
			Trigger: "time",
			Payload: func(msg bolt.Message) (string, error) {
				return "yer", nil
			},
			Timeout: time.Second * 25,
		},
        // .role can be run every 10 seconds by anyone with the 'admin' role and will respond with 'hi'
        bolt.Command{
            Trigger: "role",
            Payload: func(msg bolt.Message) (string, error) {
				return "hi", nil
			},
            Timeout: time.Second * 10,
            Roles: []string{"admin"},
        },
	)

	//start is a blocking call that handles safe-shutdown, all configuration and setup should be done before calling Start()
	err := b.Start()
	if err != nil {
		panic(err)
	}
}

Development

bolt is in development at the moment and may break occasionally before a v1 release