role updates
- bolt will now ensure role is present before exec payload - readme updates - removed author struct since roles are checked in bolt framework
This commit is contained in:
parent
381073dc39
commit
6ae84c0d5b
@ -6,7 +6,7 @@ Base Discord bot framework
|
||||
bolt is a wrapper for Discordgo to provide very quick and easy setup for simple Discord bots. The only code required to run bolt is the command handler functions, this provides developers with the ability to have text-based commands on a Discord server without all the bootstrapping and setup usually required. Any strings returned from the Payload function will be sent back to the Discord server as a reply to the command message.
|
||||
|
||||
## Basic Usage
|
||||
bolt allows developers to create a Discord bot with simply a discord bot token and a few lines of Go code, the below example creates a Discord bot and registers two commands: ".test" and ".time"
|
||||
bolt allows developers to create a Discord bot with simply a discord bot token and a few lines of Go code, the below example creates a Discord bot and registers three commands: ".test", ".time", and ".role"
|
||||
```go
|
||||
package main
|
||||
|
||||
|
24
bolt.go
24
bolt.go
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -118,12 +119,27 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
|
||||
return //running too soon, maybe respond letting know time remaining
|
||||
}
|
||||
|
||||
//does user have correct permissions
|
||||
if run.Roles != nil {
|
||||
for _, r := range msg.Member.Roles {
|
||||
n, err := s.State.Role(msg.GuildID, r)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
check := slices.Contains(run.Roles, n.Name)
|
||||
if check {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
//can't find role, don't run command
|
||||
return
|
||||
}
|
||||
|
||||
//run command payload
|
||||
res, err := run.Payload(Message{
|
||||
Author: Author{
|
||||
Username: msg.Author.Username,
|
||||
Roles: msg.Member.Roles,
|
||||
},
|
||||
Author: msg.Author.Username,
|
||||
Words: words,
|
||||
Message: msg.Content,
|
||||
Channel: channel.Name,
|
||||
|
@ -16,15 +16,9 @@ type Payload func(msg Message) (res string, err error)
|
||||
|
||||
// contains the basic information needed for a message command
|
||||
type Message struct {
|
||||
Author Author
|
||||
Author string //username of message author
|
||||
Words []string //words from message split on whitespace
|
||||
Message string //entire message content
|
||||
Channel string //channel message was sent in
|
||||
Server string //guild message was sent in
|
||||
}
|
||||
|
||||
// username and roles of message authors
|
||||
type Author struct {
|
||||
Username string
|
||||
Roles []string
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user