[chore] cleaning
- removing old or commented code - removing unused or deprecated types - commenting everything, probably too much - split msgEventHandler logic into methods - readme update, removed most docs the pkg site looks good - adjusted intents option, no need for a type
This commit is contained in:
127
README.md
127
README.md
@@ -1,70 +1,17 @@
|
||||
# bolt
|
||||
|
||||
TODO
|
||||
pre-1. Break up msg handler method its insane
|
||||
pre-2. Copy main into README
|
||||
1. Read through code and ensure I didn't miss anything
|
||||
2. do research on intents for 'admin' jobs
|
||||
3. comments and README updates, things have changed
|
||||
4. determine adjustments to timeouts and contexts and const vs options for it
|
||||
5. Figure out why the exiting printing is going after terminal exit
|
||||
|
||||
---
|
||||
|
||||
The nuts-and-bolts of Discord bots. Bolt is a wrapper for [discordgo](https://github.com/bwmarrin/discordgo) that provides quick and easy bootstrapping for simple Discord bots.
|
||||
A fast [discordgo](https://github.com/bwmarrin/discordgo) wrapper for bootstrapping Discord bots.
|
||||
|
||||
## Usage
|
||||
### Token
|
||||
### Prerequisites
|
||||
Bolt requires a Discord bot token to run, the token must be set as an environment variable labeled "DISCORD_TOKEN"
|
||||
|
||||
### Commands
|
||||
Commands are represented by the command struct and contain all needed information for bolt to handle the command payload. Including the command timeout as well as the Roles allowed to run the command.
|
||||
```go
|
||||
type Command struct {
|
||||
Trigger string //command that triggers payload NOT including the indicator
|
||||
Payload Payload //payload function to run when a command is detected
|
||||
Timeout time.Duration //the amount of time before command can be run again
|
||||
lastRun time.Time //timestamp of last command run
|
||||
Roles []string //roles that can use command, if none are set anyone can run
|
||||
}
|
||||
```
|
||||
|
||||
### Payload
|
||||
Payload functions are executed when a command is detected
|
||||
```go
|
||||
type Payload func(msg Message) error
|
||||
```
|
||||
Payload functions are given a Message argument containing the needed data for handling commands
|
||||
```go
|
||||
type Message struct {
|
||||
Author string //username of message author
|
||||
ID string //discord ID of message author
|
||||
Words []string //words from message split on whitespace
|
||||
Content string //entire message content
|
||||
Channel string //message channel
|
||||
Server string //message guild
|
||||
Attachments []MessageAttachment //message attachments
|
||||
}
|
||||
```
|
||||
The Message struct also exposes some methods to support replying to, or acknowledging command messages
|
||||
```go
|
||||
func (m *Message) React(emoji Reaction) error
|
||||
```
|
||||
The React method will react to the command message by adding the requested emoji as a reaction. Bolt comes with a few preset emoji's for easy handling but any valid emoji string can be passed.
|
||||
```go
|
||||
func (m *Message) Respond(res string) error
|
||||
```
|
||||
The Respond method will send the value of <b>res</b> in response to the command message.
|
||||
```go
|
||||
func Delete() error
|
||||
```
|
||||
The Delete method will delete the message from the text channel
|
||||
|
||||
### Example
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -72,10 +19,26 @@ import (
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
)
|
||||
|
||||
/*
|
||||
A basic example of a bot with two commands and a general message handler for non-command messages. The bot uses a
|
||||
verbose log level which will log everything for debugging purposes, and registers Discord Intents for message and admin
|
||||
related permissions. This allows the bot to parse messages, send them, delete them, etc. as well as timeout and mute users.
|
||||
|
||||
This example registers three commands:
|
||||
|
||||
1. .ping - a basic ping/pong command that can be run by anyone at any time
|
||||
2. .wait - a dummy command that replies "okay" it can only be run by users with the "user" role and can only be ran once every 25 seconds
|
||||
3. .timeout - a admin command that can only be run by users with an "admin" role, this command will timeout any mentioned users for 5 minutes
|
||||
|
||||
A message handler is also registered in this example, message handlers are used to handle messages that do not contain a command. This enables
|
||||
auto-moderation from the bot without manual intervention. The example message handler does two arbitrary things to demo functionality:
|
||||
|
||||
1. Checks the message content for the phrase "swear word" and, if found, times the user out for 5 minutes
|
||||
2. Checks the message for the phrase "im going to yell in VoiceChat" and mutes the message author until Unmute is called on them
|
||||
*/
|
||||
|
||||
func main() {
|
||||
b, err := bolt.New(bolt.WithLogLevel(bolt.LogLevelAll),
|
||||
bolt.WithMaxGoroutines(150),
|
||||
bolt.WithPermissions(bolt.MessagePermissions, bolt.AdminPermissions))
|
||||
b, err := bolt.New(bolt.WithLogLevel(bolt.LogLevelAll))
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -83,33 +46,47 @@ func main() {
|
||||
|
||||
b.AddCommands(
|
||||
bolt.Command{
|
||||
Trigger: "test",
|
||||
Payload: func(msg *bolt.Message, admin bolt.AdminToolBox) error {
|
||||
return msg.Respond("hi")
|
||||
Trigger: "ping",
|
||||
Payload: func(c *bolt.Context) error {
|
||||
return c.Respond("pong")
|
||||
},
|
||||
},
|
||||
bolt.Command{
|
||||
Trigger: "wait",
|
||||
Payload: func(c *bolt.Context) error {
|
||||
return c.Respond("okay")
|
||||
},
|
||||
Timeout: time.Second * 25,
|
||||
Roles: []string{"user"},
|
||||
},
|
||||
bolt.Command{
|
||||
Trigger: "timeout",
|
||||
Payload: func(msg *bolt.Message, tools bolt.AdminToolBox) error {
|
||||
if len(msg.Mentions) > 0 {
|
||||
err := tools.Timeout(msg.Mentions[0].ID, msg.ServerID, time.Now().Add(time.Minute*5))
|
||||
if err != nil {
|
||||
return err
|
||||
Payload: func(c *bolt.Context) error {
|
||||
if len(c.Message.Mentions) > 0 {
|
||||
count := 0
|
||||
for _, m := range c.Message.Mentions {
|
||||
err := c.Timeout(m.ID, time.Now().Add(time.Minute*5))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
count++
|
||||
}
|
||||
|
||||
return c.Respond(fmt.Sprintf("timed out %d users\n", count))
|
||||
}
|
||||
return msg.Respond("done")
|
||||
return nil
|
||||
},
|
||||
Roles: []string{"admin"},
|
||||
},
|
||||
)
|
||||
|
||||
b.AddMessageHandler(func(msg *bolt.Message, tools bolt.AdminToolBox) error {
|
||||
if strings.Contains(msg.Content, "swear word") {
|
||||
return tools.Timeout(msg.Author.ID, msg.ServerID, time.Now().Add(time.Hour*1))
|
||||
b.AddMessageHandler(func(c *bolt.Context) error {
|
||||
if strings.Contains(c.Message.Content, "swear word") {
|
||||
return c.Timeout(c.Message.Author.ID, time.Now().Add(time.Hour*1))
|
||||
}
|
||||
|
||||
if msg.Content == "im a menace in VC" {
|
||||
return tools.Mute(msg.Author.ID, msg.ServerID)
|
||||
if c.Message.Content == "im going to yell in VoiceChat" {
|
||||
return c.Mute(c.Message.Author.ID)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@@ -120,8 +97,8 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
## Development
|
||||
bolt is in development at the moment and may break occasionally before a v1 release
|
||||
bolt is in early development and may encounter breaking changes until a full v1 rollout, I will do my best to communicate
|
||||
these changes. Use a tagged version to avoid any surprises with live code in main
|
||||
Reference in New Issue
Block a user