79 lines
2.6 KiB
Markdown
79 lines
2.6 KiB
Markdown
# 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.
|
|
|
|
## Usage
|
|
### Token
|
|
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. 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
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"code.jakeyoungdev.com/jake/bolt"
|
|
_ "github.com/joho/godotenv/autoload"
|
|
)
|
|
|
|
func main() {
|
|
//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))
|
|
|
|
b.AddCommands(
|
|
// .ping can be run at any time by anyone and will respond with 'pong'
|
|
bolt.Command{
|
|
Trigger: "ping",
|
|
Payload: func(msg bolt.Message) (string, error) {
|
|
return "pong", nil //any strings returned will be sent in response to the Discord message
|
|
},
|
|
},
|
|
// .time can be run every 25 seconds by anyone and will respond with 'yer'
|
|
bolt.Command{
|
|
Trigger: "time",
|
|
Payload: func(msg bolt.Message) (string, error) {
|
|
return "yer", nil
|
|
},
|
|
Timeout: time.Second * 25,
|
|
},
|
|
// .role can be run every 10 seconds by anyone with the 'admin' role and will respond with 'hi'
|
|
bolt.Command{
|
|
Trigger: "role",
|
|
Payload: func(msg bolt.Message) (string, error) {
|
|
return "hi", nil
|
|
},
|
|
Timeout: time.Second * 10,
|
|
Roles: []string{"admin"},
|
|
},
|
|
)
|
|
|
|
//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 {
|
|
panic(err)
|
|
}
|
|
}
|
|
```
|
|
|
|
## Development
|
|
bolt is in development at the moment and may break occasionally before a v1 release |