bolt/README.md

79 lines
2.6 KiB
Markdown
Raw Normal View History

2025-06-04 19:06:53 +00:00
# bolt
The nuts and bolts of a Discord bots. Bolt is a wrapper for [discordgo](https://github.com/bwmarrin/discordgo) that provides very quick bootstrapping for simple Discord bots.
2025-06-04 16:00:53 -04:00
## Usage
### Token
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
### Commands
Commands are represented by the Command struct. Any roles in the Command struct can run the command, if the Roles field is empty anyone can 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 the command
}
```
### Payload
Payload functions are executed when a command is detected, if no errors are returned and the returned string is not empty, then the returned string is sent in reply to the command message.
```go
type Payload func(msg Message) (string, error)
```
### Example
2025-06-04 16:31:33 -04:00
```go
package main
import (
"time"
"code.jakeyoungdev.com/jake/bolt"
_ "github.com/joho/godotenv/autoload"
)
func main() {
2025-06-07 00:34:26 -04:00
//bolt defaults the command indicator to '.' however that can be changed with the options
//Example: bolt.New(bolt.WithIndicator('!')) would support commands like !ping
b := bolt.New(bolt.WithLogLevel(bolt.LogLevelCmd))
2025-06-04 16:31:33 -04:00
b.AddCommands(
2025-06-04 17:08:50 -04:00
// .ping can be run at any time by anyone and will respond with 'pong'
2025-06-04 16:31:33 -04:00
bolt.Command{
Trigger: "ping",
2025-06-07 00:34:26 -04:00
Payload: func(msg bolt.Message) (string, error) {
2025-06-04 17:08:50 -04:00
return "pong", nil //any strings returned will be sent in response to the Discord message
2025-06-04 16:31:33 -04:00
},
},
2025-06-04 17:08:50 -04:00
// .time can be run every 25 seconds by anyone and will respond with 'yer'
2025-06-04 16:31:33 -04:00
bolt.Command{
Trigger: "time",
2025-06-07 00:34:26 -04:00
Payload: func(msg bolt.Message) (string, error) {
2025-06-04 16:31:33 -04:00
return "yer", nil
},
2025-06-04 16:36:11 -04:00
Timeout: time.Second * 25,
2025-06-04 16:31:33 -04:00
},
2025-06-04 17:08:50 -04:00
// .role can be run every 10 seconds by anyone with the 'admin' role and will respond with 'hi'
2025-06-04 16:36:11 -04:00
bolt.Command{
Trigger: "role",
2025-06-07 00:34:26 -04:00
Payload: func(msg bolt.Message) (string, error) {
2025-06-04 16:36:11 -04:00
return "hi", nil
},
Timeout: time.Second * 10,
Roles: []string{"admin"},
2025-06-04 16:36:11 -04:00
},
2025-06-04 16:31:33 -04:00
)
2025-06-04 21:00:23 -04:00
//start is a blocking call that handles safe-shutdown, all configuration and setup should be done before calling Start()
err := b.Start()
if err != nil {
2025-06-04 16:31:33 -04:00
panic(err)
}
}
```
2025-06-04 16:01:53 -04:00
## Development
2025-06-07 00:34:26 -04:00
bolt is in development at the moment and may break occasionally before a v1 release