bb/bot/reaction.go

53 lines
1.1 KiB
Go
Raw Normal View History

2022-09-06 21:48:29 -05:00
package bot
2022-08-23 13:25:44 -05:00
import (
"math/rand"
"strings"
"git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
)
2022-09-07 00:29:28 -05:00
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
}
2022-08-23 13:25:44 -05:00
2022-09-07 00:29:28 -05:00
emojis := b.Config.Handler.Reaction.Emojis
channels := b.Config.Handler.Reaction.Channels
2022-08-23 13:25:44 -05:00
2022-09-07 00:29:28 -05:00
if len(emojis) == 0 {
log.Warning("emoji list is empty")
return
}
2022-08-23 13:25:44 -05:00
2022-09-07 00:29:28 -05:00
channel, err := s.Channel(m.ChannelID)
if err != nil {
log.Fatalf("unable to get channel name: %v", err)
}
2022-08-23 13:25:44 -05:00
2022-09-07 00:29:28 -05:00
if len(channels) > 0 && !lib.Contains(channels, channel.Name) {
return
}
2022-08-23 13:25:44 -05:00
2022-09-07 00:29:28 -05:00
for _, a := range m.Attachments {
if strings.HasPrefix(a.ContentType, "image/") {
for i := 1; i <= lib.RandInt(1, len(emojis)); i++ {
r := emojis[rand.Intn(len(emojis))]
s.MessageReactionAdd(m.ChannelID, m.ID, r)
}
}
}
2022-08-23 13:25:44 -05:00
2022-09-07 00:29:28 -05:00
for range m.Embeds {
2022-08-24 09:06:00 -05:00
for i := 1; i <= lib.RandInt(1, len(emojis)); i++ {
2022-08-23 13:25:44 -05:00
r := emojis[rand.Intn(len(emojis))]
s.MessageReactionAdd(m.ChannelID, m.ID, r)
}
}
}
}