From 8c996a4cc9f08c6728c3767b255a4c119022c2b5 Mon Sep 17 00:00:00 2001 From: Joseph Liu Date: Tue, 31 Dec 2024 11:27:47 -0800 Subject: [PATCH] autorole: ignore muted users (#1806) --- autorole/bot.go | 6 ++++-- moderation/punishments.go | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/autorole/bot.go b/autorole/bot.go index 665d320ab7..fe9981e564 100644 --- a/autorole/bot.go +++ b/autorole/bot.go @@ -16,6 +16,7 @@ import ( scheduledEventsModels "github.com/botlabs-gg/yagpdb/v2/common/scheduledevents2/models" "github.com/botlabs-gg/yagpdb/v2/lib/dcmd" "github.com/botlabs-gg/yagpdb/v2/lib/discordgo" + "github.com/botlabs-gg/yagpdb/v2/moderation" "github.com/mediocregopher/radix/v3" ) @@ -489,8 +490,9 @@ func handleAssignRole(evt *scheduledEventsModels.ScheduledEvent, data interface{ func handleGuildMemberUpdate(evt *eventsystem.EventData) (retry bool, err error) { update := evt.GuildMemberUpdate() member := update.Member - // ignore timedout users - if member.CommunicationDisabledUntil != nil && member.CommunicationDisabledUntil.After(time.Now()) { + // Ignore timed out and muted users. + if (member.CommunicationDisabledUntil != nil && member.CommunicationDisabledUntil.After(time.Now())) || + moderation.IsMuted(member.GuildID, member.User.ID) { return false, nil } diff --git a/moderation/punishments.go b/moderation/punishments.go index ad27d04771..9ba4cdbac7 100644 --- a/moderation/punishments.go +++ b/moderation/punishments.go @@ -440,6 +440,17 @@ const ( ErrNoMuteRole = errors.Sentinel("No mute role") ) +func IsMuted(guildID, userID int64) bool { + mute, err := models.MutedUsers( + models.MutedUserWhere.UserID.EQ(userID), + models.MutedUserWhere.GuildID.EQ(guildID), + ).OneG(context.Background()) + if err != nil { + return false // assume not muted + } + return mute.ExpiresAt.IsZero() || mute.ExpiresAt.Time.After(time.Now()) +} + // Unmut or mute a user, ignore duration if unmuting // TODO: i don't think we need to track mutes in its own database anymore now with the new scheduled event system func MuteUnmuteUser(config *Config, mute bool, guildID int64, channel *dstate.ChannelState, message *discordgo.Message, author *discordgo.User, reason string, member *dstate.MemberState, duration int, executedByCommandTemplate bool) error {