updates from testing

This commit is contained in:
2026-02-24 18:19:41 -05:00
parent 6816d7359b
commit 5297a480b8
8 changed files with 153 additions and 110 deletions

67
bolt.go
View File

@@ -9,7 +9,6 @@ import (
"slices"
"strings"
"sync"
"syscall"
"time"
dg "github.com/bwmarrin/discordgo"
@@ -34,11 +33,14 @@ type bolt struct {
pool chan struct{}
maxRoutines int
msgHandlerf Payload
admin bool
tools AdminToolBox
}
type Bolt interface {
Start() error
AddCommands(cmd ...Command)
AddMessageHandler(p Payload)
//filtered methods
stop() error
msgEventHandler(s *dg.Session, msg *dg.MessageCreate)
@@ -67,6 +69,7 @@ func New(opts ...Option) (Bolt, error) {
logLvl: LogLevelAll,
indicator: DEFAULT_INDICATOR,
wg: sync.WaitGroup{},
admin: false,
maxRoutines: DEFAULT_MAX_GOROUTINES,
}
@@ -75,8 +78,9 @@ func New(opts ...Option) (Bolt, error) {
opt(b)
}
//options can change pool size, create post-options
//options can change these fields so we must create post-opts
b.pool = make(chan struct{}, b.maxRoutines)
b.tools = NewToolbox(b)
return b, nil
}
@@ -91,7 +95,7 @@ func (b *bolt) Start() error {
log.Println("bot started")
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel, syscall.SIGINT)
signal.Notify(sigChannel, os.Interrupt)
<-sigChannel
//move this to an option, maybe?
@@ -105,18 +109,13 @@ func (b *bolt) Start() error {
select {
case <-ctx.Done():
log.Println("shutdown timed out waiting for commands to finish, some may have been incomplete")
log.Println("shutdown timed out waiting for handlers to finish, some may have been incomplete")
case <-closeChan:
log.Println("command routines cleaned up, exiting")
log.Println("handler routines cleaned")
}
if err := b.stop(); err != nil {
return err
}
log.Println("bot stopped")
return nil
log.Println("exiting")
return b.stop()
}
func (b *bolt) stop() error {
@@ -130,6 +129,10 @@ func (b *bolt) AddCommands(cmd ...Command) {
}
}
func (b *bolt) AddMessageHandler(p Payload) {
b.msgHandlerf = p
}
func (b *bolt) msgEventHandler(s *dg.Session, msg *dg.MessageCreate) {
//get server information
server, err := s.Guild(msg.GuildID)
@@ -157,17 +160,20 @@ func (b *bolt) msgEventHandler(s *dg.Session, msg *dg.MessageCreate) {
log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content)
}
//this hsould be moved to a parseMessageEvent method
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,
Author: Author{
Name: msg.Author.Username,
ID: msg.Author.ID,
Roles: 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)
@@ -207,7 +213,7 @@ func (b *bolt) msgEventHandler(s *dg.Session, msg *dg.MessageCreate) {
if msg.Content[:lg] == b.indicator {
if b.logLvl == LogLevelCmd {
//log commands
log.Printf("< %s | %s | %s > %s\n", m.Server, m.Channel, m.authorID, m.Content)
log.Printf("< %s | %s | %s > %s\n", m.Server, m.Channel, m.Author.Name, m.Content)
}
b.pool <- struct{}{} //'aquire' a routine
@@ -234,7 +240,7 @@ func (b *bolt) msgEventHandler(s *dg.Session, msg *dg.MessageCreate) {
func (b *bolt) handleMessage(event *Message) error {
if b.msgHandlerf != nil {
return b.msgHandlerf(event)
return b.msgHandlerf(event, b.tools)
}
return nil
@@ -247,7 +253,7 @@ func (b *bolt) handleCommand(msg *Message, lg int) error {
}
//has command met its timeout requirements
tc, err := b.timeoutCheck(msg.ID, msg.channelID, msg.serverID, b.Session, run)
tc, err := b.timeoutCheck(msg.ID, msg.ChannelID, msg.ServerID, b.Session, run)
if err != nil {
return fmt.Errorf("failed to calculate timeout for %s\n%e", run.Trigger, err)
}
@@ -257,13 +263,13 @@ func (b *bolt) handleCommand(msg *Message, lg int) error {
//does user have correct permissions
if run.Roles != nil {
check, err := b.roleCheck(msg.serverID, msg.authorRoles, b.Session, run)
check, err := b.roleCheck(msg.ServerID, msg.Author.Roles, b.Session, run)
if err != nil {
return fmt.Errorf("failed to perform permission checks for %s\n%e", run.Trigger, err)
}
if !check {
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)
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)
if err != nil {
return err
}
@@ -271,7 +277,7 @@ func (b *bolt) handleCommand(msg *Message, lg int) error {
}
}
err = run.Payload(msg)
err = run.Payload(msg, b.tools)
if err != nil {
return fmt.Errorf("encountered an error while handling command (%s): %e", msg.Words[0], err)
}
@@ -346,7 +352,8 @@ func (b *bolt) roleCheck(guild string, roles []string, s *dg.Session, run Comman
func (b *bolt) timeoutCheck(msgID, channelID, guildID string, s *dg.Session, run Command) (bool, error) {
wait := run.lastRun.Add(run.Timeout)
if !time.Now().After(wait) {
now := time.Now()
if !now.After(wait) && !now.Equal(wait) {
reply := b.createReply(fmt.Sprintf("that command cannot be run for another %s", b.remainingTimeout(wait)), msgID, channelID, guildID)
_, err := s.ChannelMessageSendComplex(channelID, reply)
if err != nil {