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"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var C Config
|
||||||
C Config
|
|
||||||
|
|
||||||
handlers []MessageCreateHandler = []MessageCreateHandler{
|
type (
|
||||||
NewReactionHandler(),
|
Bot struct {
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
type Bot struct {
|
|
||||||
Session *discordgo.Session
|
Session *discordgo.Session
|
||||||
Config Config
|
Config Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MessageHandler func(s *discordgo.Session, m *discordgo.MessageCreate)
|
||||||
|
)
|
||||||
|
|
||||||
func NewBot(s *discordgo.Session, config Config) *Bot {
|
func NewBot(s *discordgo.Session, config Config) *Bot {
|
||||||
return &Bot{Session: s, Config: config}
|
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 {
|
func Run() error {
|
||||||
setupConfig()
|
setupConfig()
|
||||||
|
|
||||||
@ -80,16 +83,10 @@ func Run() error {
|
|||||||
log.Fatalf("error creating Discord session: %v\n", err)
|
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 := NewBot(dg, C)
|
||||||
|
b.RegisterHandlers()
|
||||||
b.RegisterCommands()
|
b.RegisterCommands()
|
||||||
|
|
||||||
dg.AddHandler(NewCommandHandler(b))
|
|
||||||
|
|
||||||
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
||||||
|
|
||||||
err = dg.Open()
|
err = dg.Open()
|
||||||
|
@ -55,7 +55,7 @@ func GetCommand(name string) (*Command, bool) {
|
|||||||
return cmd, ok
|
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) {
|
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
var cmd *Command
|
var cmd *Command
|
||||||
|
|
||||||
@ -63,18 +63,18 @@ func NewCommandHandler(bot *Bot) func(s *discordgo.Session, m *discordgo.Message
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !lib.HasCommand(m.Content, bot.Config.Prefix) {
|
if !lib.HasCommand(m.Content, b.Config.Prefix) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdName, arg := lib.SplitCommandAndArg(m.Content, bot.Config.Prefix)
|
cmdName, arg := lib.SplitCommandAndArg(m.Content, b.Config.Prefix)
|
||||||
|
|
||||||
cmd, ok := GetCommand(cmdName)
|
cmd, ok := GetCommand(cmdName)
|
||||||
|
|
||||||
args := lib.SplitArgs(arg, cmd.NArgs)
|
args := lib.SplitArgs(arg, cmd.NArgs)
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
cmd.Config = bot.Config
|
cmd.Config = b.Config
|
||||||
|
|
||||||
log.Debugf("command: %v, args: %v, nargs: %d", cmd.Name, args, len(args))
|
log.Debugf("command: %v, args: %v, nargs: %d", cmd.Name, args, len(args))
|
||||||
cmd.Func(args, m)
|
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"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
func (b *Bot) ReactionHandler() func(*discordgo.Session, *discordgo.MessageCreate) {
|
||||||
ReactionHandler struct {
|
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
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) {
|
|
||||||
if m.Author.ID == s.State.User.ID {
|
if m.Author.ID == s.State.User.ID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
emojis := h.Config.Handler.Reaction.Emojis
|
emojis := b.Config.Handler.Reaction.Emojis
|
||||||
channels := h.Config.Handler.Reaction.Channels
|
channels := b.Config.Handler.Reaction.Channels
|
||||||
|
|
||||||
if len(emojis) == 0 {
|
if len(emojis) == 0 {
|
||||||
log.Warning("emoji list is empty")
|
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)
|
s.MessageReactionAdd(m.ChannelID, m.ID, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user