Move handlers to their own package

This commit is contained in:
Ryan Cavicchioni 2022-08-23 13:25:44 -05:00
parent 435884b61a
commit 547063de2e
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
4 changed files with 90 additions and 64 deletions

10
bot/handlers.go Normal file
View File

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

66
bot/handlers/reaction.go Normal file
View File

@ -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)
}
}
}

View File

@ -6,13 +6,12 @@ import (
//"log" //"log"
"math/rand"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
"git.kill0.net/chill9/beepboop/bot" "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/command"
"git.kill0.net/chill9/beepboop/lib" "git.kill0.net/chill9/beepboop/lib"
@ -33,6 +32,7 @@ var (
command.NewTimeHandler(), command.NewTimeHandler(),
command.NewVersionHandler("version"), command.NewVersionHandler("version"),
command.NewWeatherHandler(), command.NewWeatherHandler(),
handler.NewReactionHandler(),
} }
) )
@ -41,6 +41,8 @@ func main() {
err error err error
) )
C = bot.NewConfig()
flag.Bool("debug", false, "enable debug logging") flag.Bool("debug", false, "enable debug logging")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse() pflag.Parse()
@ -84,9 +86,6 @@ func main() {
dg.AddHandler(h.Handle) dg.AddHandler(h.Handle)
} }
dg.AddHandler(reactionHandler)
dg.AddHandler(praiseHandler)
dg.Identify.Intents = discordgo.IntentsGuildMessages dg.Identify.Intents = discordgo.IntentsGuildMessages
err = dg.Open() err = dg.Open()
@ -104,62 +103,3 @@ func main() {
log.Info("Shutting down") 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
}

10
lib/common.go Normal file
View File

@ -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
}