bb/bot/reaction.go

53 lines
1.1 KiB
Go
Raw Permalink Normal View History

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