Add handlers to Bot struct

This commit is contained in:
Ryan Cavicchioni 2022-09-07 00:29:28 -05:00
parent 31cf6f6c9a
commit e2032942ca
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
4 changed files with 44 additions and 69 deletions

View File

@ -14,19 +14,17 @@ import (
"github.com/spf13/viper"
)
var (
C Config
var C Config
handlers []MessageCreateHandler = []MessageCreateHandler{
NewReactionHandler(),
}
)
type Bot struct {
type (
Bot struct {
Session *discordgo.Session
Config Config
}
MessageHandler func(s *discordgo.Session, m *discordgo.MessageCreate)
)
func NewBot(s *discordgo.Session, config Config) *Bot {
return &Bot{Session: s, Config: config}
}
@ -66,6 +64,11 @@ func (b *Bot) RegisterCommands() {
})
}
func (b *Bot) RegisterHandlers() {
b.Session.AddHandler(b.CommandHandler())
b.Session.AddHandler(b.ReactionHandler())
}
func Run() error {
setupConfig()
@ -80,16 +83,10 @@ func Run() error {
log.Fatalf("error creating Discord session: %v\n", err)
}
for _, h := range handlers {
h.SetConfig(C)
dg.AddHandler(h.Handle)
}
b := NewBot(dg, C)
b.RegisterHandlers()
b.RegisterCommands()
dg.AddHandler(NewCommandHandler(b))
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
err = dg.Open()

View File

@ -55,7 +55,7 @@ func GetCommand(name string) (*Command, bool) {
return cmd, ok
}
func NewCommandHandler(bot *Bot) func(s *discordgo.Session, m *discordgo.MessageCreate) {
func (b *Bot) CommandHandler() func(*discordgo.Session, *discordgo.MessageCreate) {
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
var cmd *Command
@ -63,18 +63,18 @@ func NewCommandHandler(bot *Bot) func(s *discordgo.Session, m *discordgo.Message
return
}
if !lib.HasCommand(m.Content, bot.Config.Prefix) {
if !lib.HasCommand(m.Content, b.Config.Prefix) {
return
}
cmdName, arg := lib.SplitCommandAndArg(m.Content, bot.Config.Prefix)
cmdName, arg := lib.SplitCommandAndArg(m.Content, b.Config.Prefix)
cmd, ok := GetCommand(cmdName)
args := lib.SplitArgs(arg, cmd.NArgs)
if ok {
cmd.Config = bot.Config
cmd.Config = b.Config
log.Debugf("command: %v, args: %v, nargs: %d", cmd.Name, args, len(args))
cmd.Func(args, m)

View File

@ -1,10 +0,0 @@
package bot
import (
"github.com/bwmarrin/discordgo"
)
type MessageCreateHandler interface {
Handle(*discordgo.Session, *discordgo.MessageCreate)
SetConfig(Config)
}

View File

@ -10,27 +10,14 @@ import (
log "github.com/sirupsen/logrus"
)
type (
ReactionHandler struct {
Config Config
}
)
func NewReactionHandler() *ReactionHandler {
return new(ReactionHandler)
}
func (h *ReactionHandler) SetConfig(config Config) {
h.Config = config
}
func (h *ReactionHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
func (b *Bot) ReactionHandler() func(*discordgo.Session, *discordgo.MessageCreate) {
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
emojis := h.Config.Handler.Reaction.Emojis
channels := h.Config.Handler.Reaction.Channels
emojis := b.Config.Handler.Reaction.Emojis
channels := b.Config.Handler.Reaction.Channels
if len(emojis) == 0 {
log.Warning("emoji list is empty")
@ -62,3 +49,4 @@ func (h *ReactionHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreat
}
}
}
}