Skip to content

Commit

Permalink
chore: add tests on welcome run purge (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
rancoud committed Feb 21, 2024
1 parent 07c5c61 commit 20bdb54
Show file tree
Hide file tree
Showing 4 changed files with 486 additions and 11 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ If the message is not found then it will publish it and add a reaction to show u
If the message is found then it will fetch all reactions by the users and apply role.

If the user is no longer in the Discord server and you have set `can_purge_reactions` to `true` then it will:
1. check `purge_threshold_members_reacted` whether the threshold for the number of users who have reacted to the message has been exceeded.
2. check `purge_below_count_members_not_in_guild` whether the number of invalid users is below a certain threshold.
1. check `purge_threshold_members_reacted` whether the threshold for the number of users who have reacted to the message has been exceeded or equal.
2. check `purge_below_count_members_not_in_guild` whether the number of invalid users is below or equal a certain threshold.

If the answer to these questions is positive, the reaction to the message will be deleted.
If the answer to these questions is positive, the reaction to the message will be deleted.
For example you can set `purge_threshold_members_reacted` to 150 and `purge_below_count_members_not_in_guild` to 10.
It will purge only if you have 150 or more reactions and only 10 or less users not in the server.
16 changes: 11 additions & 5 deletions welcome/welcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func (w *Manager) updateRoleBelongMessage(message Message) error {
Str("role", message.Role).
Str("user_id", user.ID).
Str("username", user.Username).
Msg("Add Role to User")
Msg("Adding Role to User")

err = w.session.GuildMemberRoleAdd(w.config.GuildID, user.ID, message.RoleID)
if err != nil {
Expand All @@ -433,17 +433,23 @@ func (w *Manager) updateRoleBelongMessage(message Message) error {
Msg("Members not found in Guild")

if message.CanPurgeReactions &&
len(users) > message.PurgeThresholdMembersReacted &&
len(membersNotInGuild) < message.PurgeBelowCountMembersNotInGuild {
len(users) >= message.PurgeThresholdMembersReacted &&
len(membersNotInGuild) <= message.PurgeBelowCountMembersNotInGuild {
log.Info().
Msg("Do purge")

for idx := range membersNotInGuild {
log.Info().
Str("message_id", message.ID).
Str("emoji", message.Emoji+":"+message.EmojiID).
Str("user_id", membersNotInGuild[idx]).
Msg("Removing Reaction on Message for User")

err = w.session.MessageReactionRemove(w.config.ChannelID, message.ID, message.Emoji+":"+message.EmojiID, membersNotInGuild[idx])
if err != nil {
log.Error().Err(err).
Str("role_id", message.RoleID).
Str("role", message.Role).
Str("message_id", message.ID).
Str("emoji", message.Emoji+":"+message.EmojiID).
Str("user_id", membersNotInGuild[idx]).
Msg("Could not remove Reaction")
}
Expand Down
Loading

0 comments on commit 20bdb54

Please sign in to comment.