better errors

- no fatal calls
- more descriptive errors
This commit is contained in:
jake 2025-07-09 18:05:55 -04:00
parent 90a17ded2b
commit 113c6927cb
Signed by: jake
GPG Key ID: 9C14443265672660

44
bolt.go
View File

@ -44,20 +44,16 @@ type Bolt interface {
timeoutCheck(msg *dg.MessageCreate, s *dg.Session, run Command) (bool, error) timeoutCheck(msg *dg.MessageCreate, s *dg.Session, run Command) (bool, error)
} }
// setup // create a new bolt interface
func init() { func New(opts ...Option) (Bolt, error) {
//validate environment variables
_, check := os.LookupEnv(TOKEN_ENV_VAR) _, check := os.LookupEnv(TOKEN_ENV_VAR)
if !check { if !check {
log.Fatalf("the %s environment variable must be set", TOKEN_ENV_VAR) return nil, fmt.Errorf("environment variable %s must be set", TOKEN_ENV_VAR)
} }
}
// create a new bolt interface
func New(opts ...Option) Bolt {
bot, err := dg.New(fmt.Sprintf("Bot %s", os.Getenv(TOKEN_ENV_VAR))) bot, err := dg.New(fmt.Sprintf("Bot %s", os.Getenv(TOKEN_ENV_VAR)))
if err != nil { if err != nil {
log.Fatal(err) return nil, fmt.Errorf("failed to create Discord session: %e", err)
} }
bot.Identify.Intents = BOT_INTENTS bot.Identify.Intents = BOT_INTENTS
@ -74,7 +70,7 @@ func New(opts ...Option) Bolt {
opt(b) opt(b)
} }
return b return b, nil
} }
// starts the bot, commands are added and the connection to Discord is opened, this is a BLOCKING // starts the bot, commands are added and the connection to Discord is opened, this is a BLOCKING
@ -85,7 +81,7 @@ func (b *bolt) Start() error {
err := b.Open() err := b.Open()
if err != nil { if err != nil {
return err return fmt.Errorf("failed to open websocket connection with Discord: %e", err)
} }
//safe shutdown handler //safe shutdown handler
@ -117,12 +113,12 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
//get server information //get server information
server, err := s.Guild(msg.GuildID) server, err := s.Guild(msg.GuildID)
if err != nil { if err != nil {
log.Println(err) log.Printf("failed to get guild: %e\n", err)
return return
} }
channel, err := s.Channel(msg.ChannelID) channel, err := s.Channel(msg.ChannelID)
if err != nil { if err != nil {
log.Println(err) log.Printf("failed to get channel from guild: %e\n", err)
return return
} }
@ -132,20 +128,20 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
msg.Content = "[Embedded Content]" msg.Content = "[Embedded Content]"
} }
if b.logLvl == LogLevelAll {
//log message
log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content)
}
//the bot will ignore it's own messages to prevent command loops //the bot will ignore it's own messages to prevent command loops
if msg.Author.ID == s.State.User.ID { if msg.Author.ID == s.State.User.ID {
if b.logLvl == LogLevelCmd { if b.logLvl == LogLevelCmd {
//log commands //log command responses
log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content) log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content)
} }
return return
} }
if b.logLvl == LogLevelAll {
//log message
log.Printf("< %s | %s | %s > %s\n", server.Name, channel.Name, msg.Author.Username, msg.Content)
}
//does the message have the command indicator //does the message have the command indicator
lg := len(b.indicator) lg := len(b.indicator)
if msg.Content[:lg] == b.indicator { if msg.Content[:lg] == b.indicator {
@ -173,7 +169,7 @@ func (b *bolt) handleCommand(msg *dg.MessageCreate, s *dg.Session, server *dg.Gu
//has command met its timeout requirements //has command met its timeout requirements
tc, err := b.timeoutCheck(msg, s, run) tc, err := b.timeoutCheck(msg, s, run)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to calculate timeout for %s\n%e", run.Trigger, err)
} }
if !tc { if !tc {
return nil return nil
@ -183,7 +179,7 @@ func (b *bolt) handleCommand(msg *dg.MessageCreate, s *dg.Session, server *dg.Gu
if run.Roles != nil { if run.Roles != nil {
check, err := b.roleCheck(msg, s, run) check, err := b.roleCheck(msg, s, run)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to perform permission checks for %s\n%e", run.Trigger, err)
} }
if !check { if !check {
return nil return nil
@ -226,7 +222,7 @@ func (b *bolt) handleCommand(msg *dg.MessageCreate, s *dg.Session, server *dg.Gu
//run command payload //run command payload
err = run.Payload(plMsg) err = run.Payload(plMsg)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to execute payload function: %e", err)
} }
//update run time //update run time
@ -279,7 +275,7 @@ func (b *bolt) roleCheck(msg *dg.MessageCreate, s *dg.Session, run Command) (boo
//get role name from ID //get role name from ID
n, err := s.State.Role(msg.GuildID, r) n, err := s.State.Role(msg.GuildID, r)
if err != nil { if err != nil {
return false, err return false, fmt.Errorf("failed to get role from ID %s\n%e", msg.GuildID, err)
} }
//does this role exist in command roles //does this role exist in command roles
check := slices.Contains(run.Roles, n.Name) check := slices.Contains(run.Roles, n.Name)
@ -294,7 +290,7 @@ func (b *bolt) roleCheck(msg *dg.MessageCreate, s *dg.Session, run Command) (boo
reply := b.createReply("you do not have permissions to run that command", msg.ID, msg.ChannelID, msg.GuildID) reply := b.createReply("you do not have permissions to run that command", msg.ID, msg.ChannelID, msg.GuildID)
_, err := s.ChannelMessageSendComplex(msg.ChannelID, reply) _, err := s.ChannelMessageSendComplex(msg.ChannelID, reply)
if err != nil { if err != nil {
return false, err return false, fmt.Errorf("failed to send permission response: %e", err)
} }
return false, nil return false, nil
} }
@ -309,7 +305,7 @@ func (b *bolt) timeoutCheck(msg *dg.MessageCreate, s *dg.Session, run Command) (
reply := b.createReply(fmt.Sprintf("that command cannot be run for another %s", b.getRemainingTimeout(wait)), msg.ID, msg.ChannelID, msg.GuildID) reply := b.createReply(fmt.Sprintf("that command cannot be run for another %s", b.getRemainingTimeout(wait)), msg.ID, msg.ChannelID, msg.GuildID)
_, err := s.ChannelMessageSendComplex(msg.ChannelID, reply) _, err := s.ChannelMessageSendComplex(msg.ChannelID, reply)
if err != nil { if err != nil {
return false, err return false, fmt.Errorf("failed to send timeout response: %e", err)
} }
return false, nil return false, nil
} }