Add handlers to Bot struct
This commit is contained in:
parent
31cf6f6c9a
commit
e2032942ca
29
bot/bot.go
29
bot/bot.go
@ -14,18 +14,16 @@ 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()
|
||||
|
@ -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)
|
||||
|
@ -1,10 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
type MessageCreateHandler interface {
|
||||
Handle(*discordgo.Session, *discordgo.MessageCreate)
|
||||
SetConfig(Config)
|
||||
}
|
@ -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")
|
||||
@ -61,4 +48,5 @@ func (h *ReactionHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreat
|
||||
s.MessageReactionAdd(m.ChannelID, m.ID, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user