adding options
- ability to update indicator
This commit is contained in:
parent
d9ff09da6b
commit
55b7a717f6
10
README.md
10
README.md
@ -3,10 +3,10 @@
|
|||||||
Base Discord bot framework
|
Base Discord bot framework
|
||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
bolt is a wrapper for Discordgo to provide very quick and easy setup for simple Discord bots. The only code required to run bolt is the command handler functions, this provides developers with the ability to have text-based commands on a Discord server without all the bootstrapping and setup usually required. Any strings returned from the Payload function will be sent back to the Discord server as a reply to the command message.
|
bolt is a wrapper for Discordgo to provide very quick and easy setup for simple Discord bots. The only code required to run bolt are the command handler functions, this provides developers with the ability to have text-based commands on a Discord server without all the bootstrapping and setup usually required. Any strings returned from the Payload function will be sent back to the Discord server as a reply to the command message.
|
||||||
|
|
||||||
## Basic Usage
|
## Basic Usage
|
||||||
bolt allows developers to create a Discord bot with simply a discord bot token and a few lines of Go code, the below example creates a Discord bot and registers three commands: ".ping", ".time", and ".role"
|
bolt allows developers to create a Discord bot with simply a discord bot token and a few lines of Go code, the below example creates a Discord bot and registers three commands: ".ping", ".time", and ".role" the "." character is the default command indicator but that can be changed using the WithIndicator option.
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
@ -27,14 +27,14 @@ func main() {
|
|||||||
b.AddCommands(
|
b.AddCommands(
|
||||||
// .ping can be run at any time by anyone and will respond with 'pong'
|
// .ping can be run at any time by anyone and will respond with 'pong'
|
||||||
bolt.Command{
|
bolt.Command{
|
||||||
Trigger: ".ping",
|
Trigger: "ping",
|
||||||
Payload: func(msg bolt.Message) (res string, err error) {
|
Payload: func(msg bolt.Message) (res string, err error) {
|
||||||
return "pong", nil //any strings returned will be sent in response to the Discord message
|
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'
|
// .time can be run every 25 seconds by anyone and will respond with 'yer'
|
||||||
bolt.Command{
|
bolt.Command{
|
||||||
Trigger: ".time",
|
Trigger: "time",
|
||||||
Payload: func(msg bolt.Message) (res string, err error) {
|
Payload: func(msg bolt.Message) (res string, err error) {
|
||||||
return "yer", nil
|
return "yer", nil
|
||||||
},
|
},
|
||||||
@ -42,7 +42,7 @@ func main() {
|
|||||||
},
|
},
|
||||||
// .role can be run every 10 seconds by anyone with the 'admin' role and will respond with 'hi'
|
// .role can be run every 10 seconds by anyone with the 'admin' role and will respond with 'hi'
|
||||||
bolt.Command{
|
bolt.Command{
|
||||||
Trigger: ".role",
|
Trigger: "role",
|
||||||
Payload: func(msg bolt.Message) (res string, err error) {
|
Payload: func(msg bolt.Message) (res string, err error) {
|
||||||
return "hi", nil
|
return "hi", nil
|
||||||
},
|
},
|
||||||
|
21
bolt.go
21
bolt.go
@ -25,6 +25,7 @@ const (
|
|||||||
type bolt struct {
|
type bolt struct {
|
||||||
*dg.Session //holds discordgo internals
|
*dg.Session //holds discordgo internals
|
||||||
Commands map[string]Command //maps trigger phrase to command struct for fast lookup
|
Commands map[string]Command //maps trigger phrase to command struct for fast lookup
|
||||||
|
indicator string //the indicator used to detect whether a message is a command
|
||||||
}
|
}
|
||||||
|
|
||||||
type Bolt interface {
|
type Bolt interface {
|
||||||
@ -46,18 +47,25 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create a new bolt interface
|
// create a new bolt interface
|
||||||
func New() *bolt {
|
func New(opts ...Option) *bolt {
|
||||||
bot, err := dg.New(fmt.Sprintf("Bot %s", os.Getenv(TOKEN_ENV_VAR)))
|
bot, err := dg.New(fmt.Sprintf("Bot %s", os.Getenv(TOKEN_ENV_VAR)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
bot.Identify.Intents = BOT_INTENTS
|
bot.Identify.Intents = BOT_INTENTS
|
||||||
|
|
||||||
return &bolt{
|
b := &bolt{
|
||||||
Session: bot,
|
Session: bot,
|
||||||
Commands: make(map[string]Command, 0),
|
Commands: make(map[string]Command, 0),
|
||||||
}
|
}
|
||||||
|
//set default command indicator
|
||||||
|
b.indicator = "."
|
||||||
|
|
||||||
|
for _, o := range opts {
|
||||||
|
o(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// adds command handler and starts the bot
|
// adds command handler and starts the bot
|
||||||
@ -108,10 +116,11 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//does the message have the command indicator "."
|
//does the message have the command indicator
|
||||||
if msg.Content[:1] == "." {
|
lg := len(b.indicator)
|
||||||
|
if msg.Content[:lg] == b.indicator {
|
||||||
words := strings.Split(msg.Content, " ")
|
words := strings.Split(msg.Content, " ")
|
||||||
run, ok := b.Commands[words[0]]
|
run, ok := b.Commands[words[0][lg:]]
|
||||||
if !ok {
|
if !ok {
|
||||||
return //command doesn't exist, maybe log or respond to author
|
return //command doesn't exist, maybe log or respond to author
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package bolt
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
Trigger string //.command that triggers payload INCLUDING the '.'
|
Trigger string //command that triggers payload NOT including the indicator
|
||||||
Payload Payload //payload function to run when a command is detected
|
Payload Payload //payload function to run when a command is detected
|
||||||
Timeout time.Duration //the amount of time before command can run again
|
Timeout time.Duration //the amount of time before command can run again
|
||||||
LastRun time.Time //timestamp of last command run
|
LastRun time.Time //timestamp of last command run
|
||||||
|
Loading…
x
Reference in New Issue
Block a user