6 Commits

Author SHA1 Message Date
5b32d09a27 better naming standards 2025-06-04 23:15:00 -04:00
cc77adeadc adding license file 2025-06-04 21:03:17 -04:00
4e44b972d8 readme update 2025-06-04 21:00:23 -04:00
dc3ef04778 removing public stop method
- safe shutdown handled in library
2025-06-04 20:44:25 -04:00
55b7a717f6 adding options
- ability to update indicator
2025-06-04 18:28:27 -04:00
d9ff09da6b readme update 2025-06-04 17:08:50 -04:00
5 changed files with 69 additions and 31 deletions

7
LICENSE Normal file
View File

@@ -0,0 +1,7 @@
Copyright 2025 jake
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -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: ".test", ".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
@@ -25,24 +25,24 @@ func main() {
b := bolt.New() b := bolt.New()
b.AddCommands( b.AddCommands(
// .test can be run at any time by anyone // .ping can be run at any time by anyone and will respond with 'pong'
bolt.Command{ bolt.Command{
Trigger: ".test", Trigger: "ping",
Payload: func(msg bolt.Message) (res string, err error) { Payload: func(msg bolt.Message) (res string, err error) {
return "nah", 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 // .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
}, },
Timeout: time.Second * 25, Timeout: time.Second * 25,
}, },
// .role can be run every 10 seconds by anyone with the 'admin' role // .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
}, },
@@ -51,15 +51,9 @@ func main() {
}, },
) )
_ = b.Start() //start is a blocking call that handles safe-shutdown, all configuration and setup should be done before calling Start()
err := b.Start()
//set up safe CTRL-C if err != nil {
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel, syscall.SIGINT)
log.Println("bot running")
<-sigChannel
if err := b.Stop(); err != nil {
panic(err) panic(err)
} }
} }

49
bolt.go
View File

@@ -4,8 +4,10 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"os/signal"
"slices" "slices"
"strings" "strings"
"syscall"
"time" "time"
dg "github.com/bwmarrin/discordgo" dg "github.com/bwmarrin/discordgo"
@@ -25,11 +27,12 @@ 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 {
Start() error Start() error
Stop() error stop() error
AddCommands(cmd Command) AddCommands(cmd Command)
messageHandler(s *dg.Session, msg *dg.MessageCreate) messageHandler(s *dg.Session, msg *dg.MessageCreate)
createReply(content, message, channel, guild string) *dg.MessageSend createReply(content, message, channel, guild string) *dg.MessageSend
@@ -46,30 +49,53 @@ 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, this is a BLOCKING CALL
// starts the bot, commands are added and the connection to Discord is opened, this is a BLOCKING
// call and handles safe shutdown of the bot
func (b *bolt) Start() error { func (b *bolt) Start() error {
//register commands and open connection //register commands and open connection
b.AddHandler(b.messageHandler) b.AddHandler(b.messageHandler)
return b.Open() err := b.Open()
if err != nil {
return err
}
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel, syscall.SIGINT)
<-sigChannel
if err := b.stop(); err != nil {
return err
}
return nil
} }
// stops the bot // stops the bot
func (b *bolt) Stop() error { func (b *bolt) stop() error {
return b.Close() return b.Close()
} }
@@ -108,10 +134,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
} }
@@ -157,7 +184,7 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
res, err := run.Payload(Message{ res, err := run.Payload(Message{
Author: msg.Author.Username, Author: msg.Author.Username,
Words: words, Words: words,
Message: msg.Content, Content: msg.Content,
Channel: channel.Name, Channel: channel.Name,
Server: server.Name, Server: server.Name,
}) })

View File

@@ -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
@@ -18,7 +18,7 @@ type Payload func(msg Message) (res string, err error)
type Message struct { type Message struct {
Author string //username of message author Author string //username of message author
Words []string //words from message split on whitespace Words []string //words from message split on whitespace
Message string //entire message content Content string //entire message content
Channel string //channel message was sent in Channel string //channel message was sent in
Server string //guild message was sent in Server string //guild message was sent in
} }

10
option.go Normal file
View File

@@ -0,0 +1,10 @@
package bolt
type Option func(b *bolt)
// sets the substring that must be present at the beginning of the message to indicate a command
func WithIndicator(i string) Option {
return func(b *bolt) {
b.indicator = i
}
}