108 lines
3.0 KiB
Markdown
108 lines
3.0 KiB
Markdown
# bolt
|
|
|
|
A fast [discordgo](https://github.com/bwmarrin/discordgo) wrapper for bootstrapping Discord bots.
|
|
|
|
## Usage
|
|
### Prerequisites
|
|
Bolt requires a Discord bot token to run, the token must be set as an environment variable labeled "DISCORD_TOKEN"
|
|
|
|
### Example
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"code.jakeyoungdev.com/jake/bolt"
|
|
_ "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()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
b.AddCommands(
|
|
bolt.Command{
|
|
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(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 nil
|
|
},
|
|
Roles: []string{"admin"},
|
|
},
|
|
)
|
|
|
|
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 c.Message.Content == "im going to yell in VoiceChat" {
|
|
return c.Mute(c.Message.Author.ID)
|
|
}
|
|
return nil
|
|
})
|
|
|
|
err = b.Start()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
```
|
|
|
|
## Development
|
|
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 |