response and readme updates

- fixed example syntax
- framework now response on timeout skips
- framework will now alert of missing permissions
This commit is contained in:
jake 2025-06-04 17:06:13 -04:00
parent 6ae84c0d5b
commit ca67dc71ca
2 changed files with 43 additions and 6 deletions

View File

@ -47,7 +47,7 @@ func main() {
return "hi", nil return "hi", nil
}, },
Timeout: time.Second * 10, Timeout: time.Second * 10,
Roles: []string{"admin"} Roles: []string{"admin"},
}, },
) )

45
bolt.go
View File

@ -14,7 +14,8 @@ import (
const ( const (
TOKEN_ENV_VAR = "DISCORD_TOKEN" //label for token environment variable TOKEN_ENV_VAR = "DISCORD_TOKEN" //label for token environment variable
BOT_INTENTS = dg.IntentGuildMembers | BOT_INTENTS = dg.IntentGuilds |
dg.IntentGuildMembers |
dg.IntentGuildPresences | dg.IntentGuildPresences |
dg.IntentMessageContent | dg.IntentMessageContent |
dg.IntentsGuildMessages dg.IntentsGuildMessages
@ -32,6 +33,7 @@ type Bolt interface {
AddCommands(cmd Command) AddCommands(cmd Command)
messageHandler(s *dg.Session, msg *dg.MessageCreate) messageHandler(s *dg.Session, msg *dg.MessageCreate)
createReply(content, message, channel, guild string) *dg.MessageSend createReply(content, message, channel, guild string) *dg.MessageSend
getRemainingTimeout(timeout time.Time) string
} }
// setup // setup
@ -115,12 +117,19 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
} }
//has command met its timeout requirements //has command met its timeout requirements
if !time.Now().After(run.LastRun.Add(run.Timeout)) { wait := run.LastRun.Add(run.Timeout)
return //running too soon, maybe respond letting know time remaining if !time.Now().After(wait) {
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)
if err != nil {
log.Println(err)
}
return //running too soon
} }
//does user have correct permissions //does user have correct permissions
if run.Roles != nil { if run.Roles != nil {
var found bool
for _, r := range msg.Member.Roles { for _, r := range msg.Member.Roles {
n, err := s.State.Role(msg.GuildID, r) n, err := s.State.Role(msg.GuildID, r)
if err != nil { if err != nil {
@ -129,13 +138,20 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
} }
check := slices.Contains(run.Roles, n.Name) check := slices.Contains(run.Roles, n.Name)
if check { if check {
break found = true
} }
} }
//can't find role, don't run command //can't find role, don't run command
if !found {
reply := b.createReply("you do not have permissions to run that command", msg.ID, msg.ChannelID, msg.GuildID)
_, err := s.ChannelMessageSendComplex(msg.ChannelID, reply)
if err != nil {
log.Println(err)
}
return return
} }
}
//run command payload //run command payload
res, err := run.Payload(Message{ res, err := run.Payload(Message{
@ -180,3 +196,24 @@ func (b *bolt) createReply(content, message, channel, guild string) *dg.MessageS
Reference: details, Reference: details,
} }
} }
// used to calculate the remaining time left in a timeout and returning it in a human-readable format
func (b *bolt) getRemainingTimeout(timeout time.Time) string {
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)
}