2025-06-04 16:00:53 -04:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
|
|
import (
|
2026-02-24 16:54:25 -05:00
|
|
|
"context"
|
2025-06-04 16:00:53 -04:00
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
2025-06-04 20:44:25 -04:00
|
|
|
"os/signal"
|
2025-06-04 16:47:50 -04:00
|
|
|
"slices"
|
2025-06-04 16:00:53 -04:00
|
|
|
"strings"
|
2026-02-24 16:54:25 -05:00
|
|
|
"sync"
|
2025-06-04 20:44:25 -04:00
|
|
|
"syscall"
|
2025-06-04 16:00:53 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
dg "github.com/bwmarrin/discordgo"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2026-02-24 16:54:25 -05:00
|
|
|
//Environment variable name for discord token, this is the only required variable
|
|
|
|
|
TOKEN_ENV_VAR = "DISCORD_TOKEN"
|
|
|
|
|
|
|
|
|
|
//bot defaults
|
|
|
|
|
DEFAULT_INDICATOR = "."
|
|
|
|
|
DEFAULT_MAX_GOROUTINES = 50
|
2025-06-04 16:00:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// basic bot structure containing discordgo connection as well as the command map
|
|
|
|
|
type bolt struct {
|
|
|
|
|
*dg.Session //holds discordgo internals
|
2025-06-08 02:26:06 -04:00
|
|
|
commands map[string]Command //maps trigger phrase to command struct for fast lookup
|
2025-06-04 18:28:27 -04:00
|
|
|
indicator string //the indicator used to detect whether a message is a command
|
2025-06-25 22:51:32 +00:00
|
|
|
logLvl LogLevel //determines how much the bot logs
|
2026-02-24 16:54:25 -05:00
|
|
|
wg sync.WaitGroup
|
|
|
|
|
pool chan struct{}
|
|
|
|
|
maxRoutines int
|
|
|
|
|
msgHandlerf Payload
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Bolt interface {
|
|
|
|
|
Start() error
|
2025-06-07 00:34:26 -04:00
|
|
|
AddCommands(cmd ...Command)
|
|
|
|
|
//filtered methods
|
2025-06-04 20:44:25 -04:00
|
|
|
stop() error
|
2026-02-24 16:54:25 -05:00
|
|
|
msgEventHandler(s *dg.Session, msg *dg.MessageCreate)
|
|
|
|
|
handleCommand(msgEvent *Message, lg int) error
|
|
|
|
|
handleMessage(event *Message) error
|
2025-06-04 16:00:53 -04:00
|
|
|
createReply(content, message, channel, guild string) *dg.MessageSend
|
2026-02-24 16:54:25 -05:00
|
|
|
remainingTimeout(timeout time.Time) string
|
2025-08-17 20:37:22 -04:00
|
|
|
roleCheck(guild string, roles []string, s *dg.Session, run Command) (bool, error)
|
|
|
|
|
timeoutCheck(msgID, channelID, guildID string, s *dg.Session, run Command) (bool, error)
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-09 18:05:55 -04:00
|
|
|
func New(opts ...Option) (Bolt, error) {
|
2025-06-04 16:00:53 -04:00
|
|
|
_, check := os.LookupEnv(TOKEN_ENV_VAR)
|
|
|
|
|
if !check {
|
2025-07-09 18:05:55 -04:00
|
|
|
return nil, fmt.Errorf("environment variable %s must be set", TOKEN_ENV_VAR)
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bot, err := dg.New(fmt.Sprintf("Bot %s", os.Getenv(TOKEN_ENV_VAR)))
|
|
|
|
|
if err != nil {
|
2025-07-09 18:05:55 -04:00
|
|
|
return nil, fmt.Errorf("failed to create Discord session: %e", err)
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-04 18:28:27 -04:00
|
|
|
b := &bolt{
|
2026-02-24 16:54:25 -05:00
|
|
|
Session: bot,
|
|
|
|
|
commands: make(map[string]Command, 0),
|
|
|
|
|
logLvl: LogLevelAll,
|
|
|
|
|
indicator: DEFAULT_INDICATOR,
|
|
|
|
|
wg: sync.WaitGroup{},
|
|
|
|
|
maxRoutines: DEFAULT_MAX_GOROUTINES,
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
2025-06-04 18:28:27 -04:00
|
|
|
|
2025-08-17 20:37:22 -04:00
|
|
|
//apply options
|
2025-06-25 22:51:32 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
|
opt(b)
|
2025-06-04 18:28:27 -04:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
//options can change pool size, create post-options
|
|
|
|
|
b.pool = make(chan struct{}, b.maxRoutines)
|
|
|
|
|
|
2025-07-09 18:05:55 -04:00
|
|
|
return b, nil
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *bolt) Start() error {
|
2026-02-24 16:54:25 -05:00
|
|
|
b.AddHandler(b.msgEventHandler)
|
2025-06-04 20:44:25 -04:00
|
|
|
err := b.Open()
|
|
|
|
|
if err != nil {
|
2025-07-09 18:05:55 -04:00
|
|
|
return fmt.Errorf("failed to open websocket connection with Discord: %e", err)
|
2025-06-04 20:44:25 -04:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
log.Println("bot started")
|
|
|
|
|
|
2025-06-04 20:44:25 -04:00
|
|
|
sigChannel := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(sigChannel, syscall.SIGINT)
|
|
|
|
|
<-sigChannel
|
|
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
//move this to an option, maybe?
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
|
|
|
|
defer cancel()
|
|
|
|
|
closeChan := make(chan struct{}, 0)
|
|
|
|
|
go func() {
|
|
|
|
|
b.wg.Wait()
|
|
|
|
|
close(closeChan)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
log.Println("shutdown timed out waiting for commands to finish, some may have been incomplete")
|
|
|
|
|
case <-closeChan:
|
|
|
|
|
log.Println("command routines cleaned up, exiting")
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-04 20:44:25 -04:00
|
|
|
if err := b.stop(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
log.Println("bot stopped")
|
|
|
|
|
|
2025-06-04 20:44:25 -04:00
|
|
|
return nil
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-04 20:44:25 -04:00
|
|
|
func (b *bolt) stop() error {
|
2025-06-04 16:00:53 -04:00
|
|
|
return b.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// adds commands to bot command map for use
|
|
|
|
|
func (b *bolt) AddCommands(cmd ...Command) {
|
|
|
|
|
for _, c := range cmd {
|
2025-06-08 02:26:06 -04:00
|
|
|
b.commands[c.Trigger] = c
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
func (b *bolt) msgEventHandler(s *dg.Session, msg *dg.MessageCreate) {
|
2025-06-04 16:00:53 -04:00
|
|
|
//get server information
|
|
|
|
|
server, err := s.Guild(msg.GuildID)
|
|
|
|
|
if err != nil {
|
2025-07-09 18:05:55 -04:00
|
|
|
log.Printf("failed to get guild: %e\n", err)
|
2025-06-04 16:00:53 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
channel, err := s.Channel(msg.ChannelID)
|
|
|
|
|
if err != nil {
|
2025-07-09 18:05:55 -04:00
|
|
|
log.Printf("failed to get channel from guild: %e\n", err)
|
2025-06-04 16:00:53 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//the bot will ignore it's own messages to prevent command loops
|
|
|
|
|
if msg.Author.ID == s.State.User.ID {
|
2026-02-24 16:54:25 -05:00
|
|
|
if b.logLvl != LogLevelErr && b.logLvl != LogLevelNone {
|
2025-07-09 18:05:55 -04:00
|
|
|
//log command responses
|
2025-06-25 22:51:32 +00:00
|
|
|
log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content)
|
|
|
|
|
}
|
2025-06-04 16:00:53 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 18:05:55 -04:00
|
|
|
if b.logLvl == LogLevelAll {
|
|
|
|
|
//log message
|
|
|
|
|
log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
m := Message{
|
|
|
|
|
Author: msg.Author.Username,
|
|
|
|
|
authorID: msg.Author.ID,
|
|
|
|
|
authorRoles: msg.Member.Roles,
|
|
|
|
|
ID: msg.ID,
|
|
|
|
|
Content: msg.Content,
|
|
|
|
|
Channel: channel.Name,
|
|
|
|
|
channelID: channel.ID,
|
|
|
|
|
Server: server.Name,
|
|
|
|
|
serverID: server.ID,
|
|
|
|
|
sesh: b,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w := strings.Fields(msg.Content)
|
|
|
|
|
if len(w) > 0 {
|
|
|
|
|
m.Words = w
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(msg.Mentions) > 0 {
|
|
|
|
|
m.Mentions = msg.Mentions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(msg.Attachments) > 0 {
|
|
|
|
|
var att []MessageAttachment
|
|
|
|
|
for _, a := range msg.Attachments {
|
|
|
|
|
att = append(att, MessageAttachment{
|
|
|
|
|
ID: a.ID,
|
|
|
|
|
URL: a.URL,
|
|
|
|
|
ProxyURL: a.ProxyURL,
|
|
|
|
|
Filename: a.Filename,
|
|
|
|
|
ContentType: a.ContentType,
|
|
|
|
|
Width: a.Width,
|
|
|
|
|
Height: a.Height,
|
|
|
|
|
Size: a.Size,
|
|
|
|
|
DurationSecs: a.DurationSecs,
|
|
|
|
|
})
|
2025-08-17 20:37:22 -04:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
m.Attachments = att
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//using a patter based on a stackoverflow comment I saw that mentioned the use of a buffered channel as a lock (semaphore)
|
|
|
|
|
//to limit the amount of goroutines used at once
|
|
|
|
|
|
|
|
|
|
//could be an issue if the bot is used like a long-term calendar, not sure that is my concern we now have a timeout so it will only wait so long
|
|
|
|
|
|
|
|
|
|
lg := len(b.indicator)
|
|
|
|
|
if msg.Content[:lg] == b.indicator {
|
2025-08-17 20:37:22 -04:00
|
|
|
if b.logLvl == LogLevelCmd {
|
|
|
|
|
//log commands
|
2026-02-24 16:54:25 -05:00
|
|
|
log.Printf("< %s | %s | %s > %s\n", m.Server, m.Channel, m.authorID, m.Content)
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
2025-08-17 20:37:22 -04:00
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
b.pool <- struct{}{} //'aquire' a routine
|
|
|
|
|
|
2025-08-17 20:37:22 -04:00
|
|
|
//handled in its own goroutine to allow for async commands
|
2026-02-24 16:54:25 -05:00
|
|
|
b.wg.Go(func() {
|
|
|
|
|
err := b.handleCommand(&m, lg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
}
|
|
|
|
|
<-b.pool //release routine
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
b.pool <- struct{}{} //'aquire' a routine
|
|
|
|
|
b.wg.Go(func() {
|
|
|
|
|
err := b.handleMessage(&m)
|
2025-08-17 20:37:22 -04:00
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
}
|
2026-02-24 16:54:25 -05:00
|
|
|
<-b.pool //release routine
|
|
|
|
|
})
|
2025-06-07 00:34:26 -04:00
|
|
|
}
|
|
|
|
|
}
|
2025-06-04 16:00:53 -04:00
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
func (b *bolt) handleMessage(event *Message) error {
|
|
|
|
|
if b.msgHandlerf != nil {
|
|
|
|
|
return b.msgHandlerf(event)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *bolt) handleCommand(msg *Message, lg int) error {
|
|
|
|
|
run, ok := b.commands[msg.Words[0][lg:]]
|
2025-06-07 00:34:26 -04:00
|
|
|
if !ok {
|
|
|
|
|
return nil //command doesn't exist, maybe log or respond to author
|
|
|
|
|
}
|
2025-06-04 16:47:50 -04:00
|
|
|
|
2025-06-07 00:34:26 -04:00
|
|
|
//has command met its timeout requirements
|
2026-02-24 16:54:25 -05:00
|
|
|
tc, err := b.timeoutCheck(msg.ID, msg.channelID, msg.serverID, b.Session, run)
|
2025-06-07 00:34:26 -04:00
|
|
|
if err != nil {
|
2025-07-09 18:05:55 -04:00
|
|
|
return fmt.Errorf("failed to calculate timeout for %s\n%e", run.Trigger, err)
|
2025-06-07 00:34:26 -04:00
|
|
|
}
|
|
|
|
|
if !tc {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-06-04 16:00:53 -04:00
|
|
|
|
2025-06-07 00:34:26 -04:00
|
|
|
//does user have correct permissions
|
|
|
|
|
if run.Roles != nil {
|
2026-02-24 16:54:25 -05:00
|
|
|
check, err := b.roleCheck(msg.serverID, msg.authorRoles, b.Session, run)
|
2025-06-04 16:00:53 -04:00
|
|
|
if err != nil {
|
2025-07-09 18:05:55 -04:00
|
|
|
return fmt.Errorf("failed to perform permission checks for %s\n%e", run.Trigger, err)
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
2025-06-07 00:34:26 -04:00
|
|
|
if !check {
|
2026-02-24 16:54:25 -05:00
|
|
|
reply := b.createReply("you do not have permissions to run that command", msg.ID, msg.channelID, msg.serverID)
|
|
|
|
|
_, err := b.Session.ChannelMessageSendComplex(msg.channelID, reply)
|
2025-08-17 20:37:22 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-06-07 00:34:26 -04:00
|
|
|
return nil
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
2025-06-07 00:34:26 -04:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 16:54:25 -05:00
|
|
|
err = run.Payload(msg)
|
2025-07-09 20:11:12 +00:00
|
|
|
if err != nil {
|
2026-02-24 16:54:25 -05:00
|
|
|
return fmt.Errorf("encountered an error while handling command (%s): %e", msg.Words[0], err)
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
2025-06-07 00:34:26 -04:00
|
|
|
|
|
|
|
|
//update run time
|
|
|
|
|
run.lastRun = time.Now()
|
2025-06-08 02:26:06 -04:00
|
|
|
b.commands[run.Trigger] = run
|
2025-06-07 00:34:26 -04:00
|
|
|
return nil
|
2025-06-04 16:00:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// basic wrapper function to create easy Discord responses
|
|
|
|
|
func (b *bolt) createReply(content, message, channel, guild string) *dg.MessageSend {
|
|
|
|
|
details := &dg.MessageReference{
|
|
|
|
|
MessageID: message,
|
|
|
|
|
ChannelID: channel,
|
|
|
|
|
GuildID: guild,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &dg.MessageSend{
|
|
|
|
|
Content: content,
|
|
|
|
|
Reference: details,
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-04 17:06:13 -04:00
|
|
|
|
|
|
|
|
// used to calculate the remaining time left in a timeout and returning it in a human-readable format
|
2026-02-24 16:54:25 -05:00
|
|
|
func (b *bolt) remainingTimeout(timeout time.Time) string {
|
2025-06-04 17:06:13 -04:00
|
|
|
r := time.Until(timeout)
|
|
|
|
|
var (
|
|
|
|
|
timeLeft int
|
|
|
|
|
metric string
|
|
|
|
|
)
|
|
|
|
|
timeLeft = int(r.Hours())
|
|
|
|
|
metric = "h"
|
|
|
|
|
if timeLeft < 1 {
|
|
|
|
|
timeLeft = int(r.Minutes())
|
|
|
|
|
metric = "m"
|
|
|
|
|
if timeLeft < 1 {
|
|
|
|
|
timeLeft = int(r.Seconds())
|
|
|
|
|
metric = "s"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("%d%s", timeLeft, metric)
|
|
|
|
|
}
|
2025-06-07 00:34:26 -04:00
|
|
|
|
|
|
|
|
// checks if the author of msg has the correct role to run the requested command
|
2025-08-17 20:37:22 -04:00
|
|
|
func (b *bolt) roleCheck(guild string, roles []string, s *dg.Session, run Command) (bool, error) {
|
2025-06-07 00:34:26 -04:00
|
|
|
var found bool
|
2025-06-07 09:31:00 -04:00
|
|
|
//loop thru author roles, there may be a better way to check for this UNION
|
|
|
|
|
//TODO: improve role search performance to support bigger lists
|
2025-08-17 20:37:22 -04:00
|
|
|
for _, r := range roles {
|
2025-06-07 00:34:26 -04:00
|
|
|
//get role name from ID
|
2025-08-17 20:37:22 -04:00
|
|
|
n, err := s.State.Role(guild, r)
|
2025-06-07 00:34:26 -04:00
|
|
|
if err != nil {
|
2025-08-17 20:37:22 -04:00
|
|
|
return false, fmt.Errorf("failed to get role from ID %s\n%e", guild, err)
|
2025-06-07 00:34:26 -04:00
|
|
|
}
|
|
|
|
|
//does this role exist in command roles
|
|
|
|
|
check := slices.Contains(run.Roles, n.Name)
|
|
|
|
|
if check {
|
|
|
|
|
found = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 20:37:22 -04:00
|
|
|
//can't find role, don't run command
|
2025-06-07 00:34:26 -04:00
|
|
|
if !found {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 20:37:22 -04:00
|
|
|
func (b *bolt) timeoutCheck(msgID, channelID, guildID string, s *dg.Session, run Command) (bool, error) {
|
2025-06-07 00:34:26 -04:00
|
|
|
wait := run.lastRun.Add(run.Timeout)
|
|
|
|
|
if !time.Now().After(wait) {
|
2026-02-24 16:54:25 -05:00
|
|
|
reply := b.createReply(fmt.Sprintf("that command cannot be run for another %s", b.remainingTimeout(wait)), msgID, channelID, guildID)
|
2025-08-17 20:37:22 -04:00
|
|
|
_, err := s.ChannelMessageSendComplex(channelID, reply)
|
2025-06-07 00:34:26 -04:00
|
|
|
if err != nil {
|
2025-07-09 18:05:55 -04:00
|
|
|
return false, fmt.Errorf("failed to send timeout response: %e", err)
|
2025-06-07 00:34:26 -04:00
|
|
|
}
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|