diff --git a/bot/handlers.go b/bot/handlers.go new file mode 100644 index 0000000..b0b3dfc --- /dev/null +++ b/bot/handlers.go @@ -0,0 +1,10 @@ +package bot + +import ( + "github.com/bwmarrin/discordgo" +) + +type MessageCreateHandler interface { + Handle(s *discordgo.Session, m *discordgo.MessageCreate) + SetConfig(config Config) +} diff --git a/bot/handlers/reaction.go b/bot/handlers/reaction.go new file mode 100644 index 0000000..99ec608 --- /dev/null +++ b/bot/handlers/reaction.go @@ -0,0 +1,66 @@ +package handler + +import ( + "math/rand" + "strings" + + "git.kill0.net/chill9/beepboop/bot" + "git.kill0.net/chill9/beepboop/command" + "git.kill0.net/chill9/beepboop/lib" + + "github.com/bwmarrin/discordgo" + log "github.com/sirupsen/logrus" +) + +type ( + ReactionHandler struct { + config bot.Config + } +) + +func NewReactionHandler() *ReactionHandler { + return new(ReactionHandler) +} + +func (h *ReactionHandler) SetConfig(config bot.Config) { + h.config = config +} + +func (h *ReactionHandler) Handle(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 + + if len(emojis) == 0 { + log.Warning("emoji list is empty") + return + } + + channel, err := s.Channel(m.ChannelID) + 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 <= command.RandInt(1, len(emojis)); i++ { + r := emojis[rand.Intn(len(emojis))] + s.MessageReactionAdd(m.ChannelID, m.ID, r) + } + } + } + + for range m.Embeds { + for i := 1; i <= command.RandInt(1, len(emojis)); i++ { + r := emojis[rand.Intn(len(emojis))] + s.MessageReactionAdd(m.ChannelID, m.ID, r) + } + } +} diff --git a/cmd/bb/main.go b/cmd/bb/main.go index 33166a9..709ef5b 100644 --- a/cmd/bb/main.go +++ b/cmd/bb/main.go @@ -6,13 +6,12 @@ import ( //"log" - "math/rand" "os" "os/signal" - "strings" "syscall" "git.kill0.net/chill9/beepboop/bot" + handler "git.kill0.net/chill9/beepboop/bot/handlers" "git.kill0.net/chill9/beepboop/command" "git.kill0.net/chill9/beepboop/lib" @@ -33,6 +32,7 @@ var ( command.NewTimeHandler(), command.NewVersionHandler("version"), command.NewWeatherHandler(), + handler.NewReactionHandler(), } ) @@ -41,6 +41,8 @@ func main() { err error ) + C = bot.NewConfig() + flag.Bool("debug", false, "enable debug logging") pflag.CommandLine.AddGoFlagSet(flag.CommandLine) pflag.Parse() @@ -84,9 +86,6 @@ func main() { dg.AddHandler(h.Handle) } - dg.AddHandler(reactionHandler) - dg.AddHandler(praiseHandler) - dg.Identify.Intents = discordgo.IntentsGuildMessages err = dg.Open() @@ -104,62 +103,3 @@ func main() { log.Info("Shutting down") } - -func praiseHandler(s *discordgo.Session, m *discordgo.MessageCreate) { - if m.Author.ID == s.State.User.ID { - return - } - - if strings.Contains(m.Content, "good bot") { - s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("<@%s> Thank you, daddy.", m.Author.ID)) - } -} - -func reactionHandler(s *discordgo.Session, m *discordgo.MessageCreate) { - if m.Author.ID == s.State.User.ID { - return - } - - emojis := C.Handler.Reaction.Emojis - channels := C.Handler.Reaction.Channels - - if len(emojis) == 0 { - log.Warning("emoji list is empty") - return - } - - channel, err := s.Channel(m.ChannelID) - if err != nil { - log.Fatalf("unable to get channel name: %v", err) - } - - if len(channels) > 0 && !contains(channels, channel.Name) { - return - } - - for _, a := range m.Attachments { - if strings.HasPrefix(a.ContentType, "image/") { - for i := 1; i <= command.RandInt(1, len(emojis)); i++ { - r := emojis[rand.Intn(len(emojis))] - s.MessageReactionAdd(m.ChannelID, m.ID, r) - } - } - } - - for range m.Embeds { - for i := 1; i <= command.RandInt(1, len(emojis)); i++ { - r := emojis[rand.Intn(len(emojis))] - s.MessageReactionAdd(m.ChannelID, m.ID, r) - } - } - -} - -func contains[T comparable](s []T, v T) bool { - for _, x := range s { - if x == v { - return true - } - } - return false -} diff --git a/lib/common.go b/lib/common.go new file mode 100644 index 0000000..61bdd83 --- /dev/null +++ b/lib/common.go @@ -0,0 +1,10 @@ +package lib + +func Contains[T comparable](s []T, v T) bool { + for _, x := range s { + if x == v { + return true + } + } + return false +}