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 {
|
||||||
|
Session *discordgo.Session
|
||||||
|
Config Config
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
type Bot struct {
|
MessageHandler func(s *discordgo.Session, m *discordgo.MessageCreate)
|
||||||
Session *discordgo.Session
|
)
|
||||||
Config Config
|
|
||||||
}
|
|
||||||
|
|
||||||
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,55 +10,43 @@ 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
|
if m.Author.ID == s.State.User.ID {
|
||||||
}
|
return
|
||||||
)
|
}
|
||||||
|
|
||||||
func NewReactionHandler() *ReactionHandler {
|
emojis := b.Config.Handler.Reaction.Emojis
|
||||||
return new(ReactionHandler)
|
channels := b.Config.Handler.Reaction.Channels
|
||||||
}
|
|
||||||
|
|
||||||
func (h *ReactionHandler) SetConfig(config Config) {
|
if len(emojis) == 0 {
|
||||||
h.Config = config
|
log.Warning("emoji list is empty")
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (h *ReactionHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
|
channel, err := s.Channel(m.ChannelID)
|
||||||
if m.Author.ID == s.State.User.ID {
|
if err != nil {
|
||||||
return
|
log.Fatalf("unable to get channel name: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
emojis := h.Config.Handler.Reaction.Emojis
|
if len(channels) > 0 && !lib.Contains(channels, channel.Name) {
|
||||||
channels := h.Config.Handler.Reaction.Channels
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if len(emojis) == 0 {
|
for _, a := range m.Attachments {
|
||||||
log.Warning("emoji list is empty")
|
if strings.HasPrefix(a.ContentType, "image/") {
|
||||||
return
|
for i := 1; i <= lib.RandInt(1, len(emojis)); i++ {
|
||||||
}
|
r := emojis[rand.Intn(len(emojis))]
|
||||||
|
s.MessageReactionAdd(m.ChannelID, m.ID, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
channel, err := s.Channel(m.ChannelID)
|
for range m.Embeds {
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("unable to get channel name: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(channels) > 0 && !lib.Contains(channels, channel.Name) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, a := range m.Attachments {
|
|
||||||
if strings.HasPrefix(a.ContentType, "image/") {
|
|
||||||
for i := 1; i <= lib.RandInt(1, len(emojis)); i++ {
|
for i := 1; i <= lib.RandInt(1, len(emojis)); i++ {
|
||||||
r := emojis[rand.Intn(len(emojis))]
|
r := emojis[rand.Intn(len(emojis))]
|
||||||
s.MessageReactionAdd(m.ChannelID, m.ID, r)
|
s.MessageReactionAdd(m.ChannelID, m.ID, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for range m.Embeds {
|
|
||||||
for i := 1; i <= lib.RandInt(1, len(emojis)); i++ {
|
|
||||||
r := emojis[rand.Intn(len(emojis))]
|
|
||||||
s.MessageReactionAdd(m.ChannelID, m.ID, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user