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
},
Timeout: time.Second * 10,
Roles: []string{"admin"}
Roles: []string{"admin"},
},
)

47
bolt.go
View File

@ -14,7 +14,8 @@ import (
const (
TOKEN_ENV_VAR = "DISCORD_TOKEN" //label for token environment variable
BOT_INTENTS = dg.IntentGuildMembers |
BOT_INTENTS = dg.IntentGuilds |
dg.IntentGuildMembers |
dg.IntentGuildPresences |
dg.IntentMessageContent |
dg.IntentsGuildMessages
@ -32,6 +33,7 @@ type Bolt interface {
AddCommands(cmd Command)
messageHandler(s *dg.Session, msg *dg.MessageCreate)
createReply(content, message, channel, guild string) *dg.MessageSend
getRemainingTimeout(timeout time.Time) string
}
// setup
@ -115,12 +117,19 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
}
//has command met its timeout requirements
if !time.Now().After(run.LastRun.Add(run.Timeout)) {
return //running too soon, maybe respond letting know time remaining
wait := run.LastRun.Add(run.Timeout)
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
if run.Roles != nil {
var found bool
for _, r := range msg.Member.Roles {
n, err := s.State.Role(msg.GuildID, r)
if err != nil {
@ -129,12 +138,19 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
}
check := slices.Contains(run.Roles, n.Name)
if check {
break
found = true
}
}
//can't find role, don't run command
return
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
}
}
//run command payload
@ -180,3 +196,24 @@ func (b *bolt) createReply(content, message, channel, guild string) *dg.MessageS
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)
}