Files
bolt/README.md
T

117 lines
3.1 KiB
Markdown
Raw Normal View History

2025-06-04 19:06:53 +00:00
# bolt
2026-02-25 13:24:27 -05:00
A fast [discordgo](https://github.com/bwmarrin/discordgo) wrapper for bootstrapping Discord bots.
2025-06-04 16:00:53 -04:00
2025-06-08 02:26:06 -04:00
## Usage
2026-02-25 13:24:27 -05:00
### Prerequisites
2025-06-08 02:26:06 -04:00
Bolt requires a Discord bot token to run, the token must be set as an environment variable labeled "DISCORD_TOKEN"
2025-06-04 16:01:53 -04:00
2025-06-08 02:26:06 -04:00
### Example
2025-06-04 16:31:33 -04:00
```go
package main
import (
2026-02-25 13:24:27 -05:00
"fmt"
2026-06-25 16:02:47 -04:00
"os"
"os/signal"
2026-02-24 18:19:41 -05:00
"strings"
2025-06-04 16:31:33 -04:00
"time"
"code.jakeyoungdev.com/jake/bolt"
2026-02-24 18:19:41 -05:00
_ "github.com/joho/godotenv/autoload"
2025-06-04 16:31:33 -04:00
)
2026-02-25 13:24:27 -05:00
/*
A basic example of a bot with two commands and a general message handler for non-command messages. The bot uses a
2026-06-25 16:02:47 -04:00
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
2026-03-01 00:26:39 -05:00
and mute users.
2026-02-25 13:24:27 -05:00
This example registers three commands:
1. .ping - a basic ping/pong command that can be run by anyone at any time
2026-06-25 16:02:47 -04:00
2. .wait - a dummy command that replies "okay" it can only be run by users with the "user" role and can only be ran
2026-03-01 00:26:39 -05:00
once every 25 seconds
2026-06-25 16:02:47 -04:00
3. .timeout - a admin command that can only be run by users with an "admin" role, this command will timeout any mentioned
2026-03-01 00:26:39 -05:00
users for 5 minutes
2026-02-25 13:24:27 -05:00
2026-06-25 16:02:47 -04:00
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
2026-03-01 00:26:39 -05:00
things to demo functionality:
2026-02-25 13:24:27 -05:00
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
*/
2025-06-04 16:31:33 -04:00
func main() {
2026-02-25 15:44:44 -05:00
b, err := bolt.New()
2025-07-10 14:36:21 -04:00
if err != nil {
panic(err)
}
2025-06-04 16:31:33 -04:00
b.AddCommands(
2025-07-09 20:11:12 +00:00
bolt.Command{
2026-02-25 13:24:27 -05:00
Trigger: "ping",
Payload: func(c *bolt.Context) error {
return c.Respond("pong")
2025-06-04 16:31:33 -04:00
},
},
2026-02-25 13:24:27 -05:00
bolt.Command{
Trigger: "wait",
Payload: func(c *bolt.Context) error {
return c.Respond("okay")
},
Timeout: time.Second * 25,
Roles: []string{"user"},
},
2025-06-04 16:31:33 -04:00
bolt.Command{
2026-02-24 18:19:41 -05:00
Trigger: "timeout",
2026-02-25 13:24:27 -05:00
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++
2026-02-24 18:19:41 -05:00
}
2026-02-25 13:24:27 -05:00
return c.Respond(fmt.Sprintf("timed out %d users\n", count))
2026-02-24 18:19:41 -05:00
}
2026-02-25 13:24:27 -05:00
return nil
2025-06-04 16:31:33 -04:00
},
2026-02-24 18:19:41 -05:00
Roles: []string{"admin"},
2025-07-09 20:11:12 +00:00
},
2025-06-04 16:31:33 -04:00
)
2026-02-25 13:24:27 -05:00
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))
2026-02-24 18:19:41 -05:00
}
2026-02-25 13:24:27 -05:00
if c.Message.Content == "im going to yell in VoiceChat" {
return c.Mute(c.Message.Author.ID)
2026-02-24 18:19:41 -05:00
}
return nil
})
2025-07-10 14:36:21 -04:00
err = b.Start()
2025-06-04 21:00:23 -04:00
if err != nil {
2025-06-04 16:31:33 -04:00
panic(err)
}
2025-07-10 14:36:21 -04:00
2026-06-25 16:02:47 -04:00
qc := make(chan os.Signal, 1)
signal.Notify(qc, os.Interrupt)
<-qc
err = b.Stop()
if err != nil {
panic(err)
}
}
2025-06-04 16:31:33 -04:00
```
2025-06-04 16:01:53 -04:00
## Development
2026-02-25 13:24:27 -05:00
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