adding options

- ability to update indicator
This commit is contained in:
2025-06-04 18:28:27 -04:00
parent d9ff09da6b
commit 55b7a717f6
4 changed files with 31 additions and 12 deletions

21
bolt.go
View File

@@ -25,6 +25,7 @@ const (
type bolt struct {
*dg.Session //holds discordgo internals
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 {
@@ -46,18 +47,25 @@ func init() {
}
// 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)))
if err != nil {
log.Fatal(err)
}
bot.Identify.Intents = BOT_INTENTS
return &bolt{
b := &bolt{
Session: bot,
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
@@ -108,10 +116,11 @@ func (b *bolt) messageHandler(s *dg.Session, msg *dg.MessageCreate) {
return
}
//does the message have the command indicator "."
if msg.Content[:1] == "." {
//does the message have the command indicator
lg := len(b.indicator)
if msg.Content[:lg] == b.indicator {
words := strings.Split(msg.Content, " ")
run, ok := b.Commands[words[0]]
run, ok := b.Commands[words[0][lg:]]
if !ok {
return //command doesn't exist, maybe log or respond to author
}