Skip to content

Commit

Permalink
chore: add tests on welcome handlers (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
rancoud committed Feb 22, 2024
1 parent 20bdb54 commit 88d4b7c
Show file tree
Hide file tree
Showing 3 changed files with 648 additions and 8 deletions.
58 changes: 51 additions & 7 deletions welcome/welcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ func (w *Manager) hasValidConfigurationAgainstDiscordServer() bool {
// Run do the main task of Welcome.
func (w *Manager) Run() error {
log.Info().Msg("Adding Handler on Message Reaction Add")
w.session.AddHandler(w.onMessageReactionAdd)
w.session.AddHandler(w.OnMessageReactionAdd)

log.Info().Msg("Adding Handler on Message Reaction Remove")
w.session.AddHandler(w.onMessageReactionRemove)
w.session.AddHandler(w.OnMessageReactionRemove)

log.Info().Msg("Adding messages to channel")

Expand Down Expand Up @@ -510,20 +510,35 @@ func (w *Manager) addMessage(message *Message) error {
return nil
}

func (w *Manager) onMessageReactionAdd(_ *discordgo.Session, reaction *discordgo.MessageReactionAdd) {
// OnMessageReactionAdd is public for tests, never call it directly
//
//nolint:dupl,funlen
func (w *Manager) OnMessageReactionAdd(_ *discordgo.Session, reaction *discordgo.MessageReactionAdd) {
if reaction == nil || reaction.MessageReaction == nil {
log.Error().Msg("OnMessageReactionAdd - SKIP - Reaction is nil")

return
}

log.Info().
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Msg("Incoming Message Reaction Add")

if reaction.ChannelID != w.config.ChannelID {
log.Info().Msg("SKIP - Channel is not matching")
log.Info().
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Msg("SKIP - Channel is not matching")

return
}

if w.isUserBot(reaction.UserID) {
log.Info().
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Str("user_id", reaction.UserID).
Msg("SKIP - User is the bot")

return
Expand All @@ -542,6 +557,7 @@ func (w *Manager) onMessageReactionAdd(_ *discordgo.Session, reaction *discordgo
if idxMessageFound == -1 {
log.Info().
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Msg("SKIP - Message is not matching")

return
Expand All @@ -550,6 +566,8 @@ func (w *Manager) onMessageReactionAdd(_ *discordgo.Session, reaction *discordgo
if reaction.Emoji.Name != w.config.Messages[idxMessageFound].Emoji {
log.Info().
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Str("emoji", reaction.Emoji.Name).
Msg("SKIP - Emoji is not matching")

return
Expand All @@ -559,6 +577,7 @@ func (w *Manager) onMessageReactionAdd(_ *discordgo.Session, reaction *discordgo
Str("role_id", w.config.Messages[idxMessageFound].RoleID).
Str("role", w.config.Messages[idxMessageFound].Role).
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Str("user_id", reaction.UserID).
Msg("Adding Role to User")

Expand All @@ -568,24 +587,40 @@ func (w *Manager) onMessageReactionAdd(_ *discordgo.Session, reaction *discordgo
Str("role_id", w.config.Messages[idxMessageFound].RoleID).
Str("role", w.config.Messages[idxMessageFound].Role).
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Str("user_id", reaction.UserID).
Msg("Could not add Role to User")
}
}

func (w *Manager) onMessageReactionRemove(_ *discordgo.Session, reaction *discordgo.MessageReactionRemove) {
log.Info().Msg("Incoming Message Reaction Remove")
// OnMessageReactionRemove is public for tests, never call it directly
//
//nolint:dupl,funlen
func (w *Manager) OnMessageReactionRemove(_ *discordgo.Session, reaction *discordgo.MessageReactionRemove) {
if reaction == nil || reaction.MessageReaction == nil {
log.Error().Msg("OnMessageReactionRemove - SKIP - Reaction is nil")

return
}

log.Info().
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Msg("Incoming Message Reaction Remove")

if reaction.ChannelID != w.config.ChannelID {
log.Info().
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Msg("SKIP - Channel is not matching")

return
}

if w.isUserBot(reaction.UserID) {
log.Info().
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Str("user_id", reaction.UserID).
Msg("SKIP - User is the bot")

Expand All @@ -603,13 +638,18 @@ func (w *Manager) onMessageReactionRemove(_ *discordgo.Session, reaction *discor
}

if idxMessageFound == -1 {
log.Info().Msg("SKIP - Message is not matching")
log.Info().
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Msg("SKIP - Message is not matching")

return
}

if reaction.Emoji.Name != w.config.Messages[idxMessageFound].Emoji {
log.Info().
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Str("emoji", reaction.Emoji.Name).
Msg("SKIP - Emoji is not matching")

Expand All @@ -619,6 +659,8 @@ func (w *Manager) onMessageReactionRemove(_ *discordgo.Session, reaction *discor
log.Info().
Str("role_id", w.config.Messages[idxMessageFound].RoleID).
Str("role", w.config.Messages[idxMessageFound].Role).
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Str("user_id", reaction.UserID).
Msg("Removing Role to User")

Expand All @@ -627,6 +669,8 @@ func (w *Manager) onMessageReactionRemove(_ *discordgo.Session, reaction *discor
log.Error().Err(err).
Str("role_id", w.config.Messages[idxMessageFound].RoleID).
Str("role", w.config.Messages[idxMessageFound].Role).
Str("channel_id", reaction.ChannelID).
Str("message_id", reaction.MessageID).
Str("user_id", reaction.UserID).
Msg("Could not remove Role to User")
}
Expand Down
Loading

0 comments on commit 88d4b7c

Please sign in to comment.