Files
bolt/README.md

108 lines
3.0 KiB
Markdown
Raw Normal View History

2025-06-04 19:06:53 +00:00
# bolt
A fast [discordgo](https://github.com/bwmarrin/discordgo) wrapper for bootstrapping Discord bots.
2025-06-04 16:00:53 -04:00
## Usage
### Prerequisites
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
### Example
2025-06-04 16:31:33 -04:00
```go
package main
import (
"fmt"
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
)
/*
A basic example of a bot with two commands and a general message handler for non-command messages. The bot uses a
2026-03-01 00:26:39 -05: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
and mute users.
This example registers three commands:
1. .ping - a basic ping/pong command that can be run by anyone at any time
2026-03-01 00:26:39 -05: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
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
2026-03-01 00:26:39 -05: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
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
*/
2025-06-04 16:31:33 -04:00
func main() {
2026-02-25 15:44:44 -05:00
b, err := bolt.New()
2026-02-24 18:19:41 -05:00
2025-07-10 14:36:21 -04:00
if err != nil {
panic(err)
}
2025-06-04 16:31:33 -04:00
b.AddCommands(
bolt.Command{
Trigger: "ping",
Payload: func(c *bolt.Context) error {
return c.Respond("pong")
2025-06-04 16:31:33 -04: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",
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
}
return c.Respond(fmt.Sprintf("timed out %d users\n", count))
2026-02-24 18:19:41 -05:00
}
return nil
2025-06-04 16:31:33 -04:00
},
2026-02-24 18:19:41 -05:00
Roles: []string{"admin"},
},
2025-06-04 16:31:33 -04: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
}
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
2025-06-04 16:31:33 -04:00
```
2025-06-04 16:01:53 -04:00
## 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