Skip to content

Commit

Permalink
Attempt to skip mute role check on member leave if they are not cached
Browse files Browse the repository at this point in the history
  • Loading branch information
FloatingMilkshake committed Nov 27, 2024
1 parent 463b6b2 commit 7f5906d
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions Events/MemberEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,43 @@ public static async Task GuildMemberRemoved(DiscordClient client, GuildMemberRem
if (e.Guild.Id != cfgjson.ServerID)
return;

var muteRole = await e.Guild.GetRoleAsync(cfgjson.MutedRole);
// Attempt to check if member is cached
bool isMemberCached = client.Guilds[e.Guild.Id].Members.ContainsKey(e.Member.Id);

DiscordRole tqsMuteRole = default;
if (cfgjson.TqsMutedRole != 0)
tqsMuteRole = await e.Guild.GetRoleAsync(cfgjson.TqsMutedRole);
if (isMemberCached)
{
// Only check mute role against db entry if we know the member's roles are accurate.
// If the member is not cached, we will think they have no roles when they might actually be muted!
// Then we would be falsely removing their mute entry.

var userMute = await db.HashGetAsync("mutes", e.Member.Id);
var muteRole = await e.Guild.GetRoleAsync(cfgjson.MutedRole);

if (!userMute.IsNull && !e.Member.Roles.Contains(muteRole) & !e.Member.Roles.Contains(tqsMuteRole))
db.HashDeleteAsync("mutes", e.Member.Id);
DiscordRole tqsMuteRole = default;
if (cfgjson.TqsMutedRole != 0)
tqsMuteRole = await e.Guild.GetRoleAsync(cfgjson.TqsMutedRole);

if ((e.Member.Roles.Contains(muteRole) || e.Member.Roles.Contains(tqsMuteRole)) && userMute.IsNull)
{
MemberPunishment newMute = new()
var userMute = await db.HashGetAsync("mutes", e.Member.Id);

if (!userMute.IsNull && !e.Member.Roles.Contains(muteRole) & !e.Member.Roles.Contains(tqsMuteRole))
db.HashDeleteAsync("mutes", e.Member.Id);

if ((e.Member.Roles.Contains(muteRole) || e.Member.Roles.Contains(tqsMuteRole)) && userMute.IsNull)
{
MemberId = e.Member.Id,
ModId = discord.CurrentUser.Id,
ServerId = e.Guild.Id,
ExpireTime = null,
ActionTime = DateTime.Now
};

db.HashSetAsync("mutes", e.Member.Id, JsonConvert.SerializeObject(newMute));
}
MemberPunishment newMute = new()
{
MemberId = e.Member.Id,
ModId = discord.CurrentUser.Id,
ServerId = e.Guild.Id,
ExpireTime = null,
ActionTime = DateTime.Now
};

db.HashSetAsync("mutes", e.Member.Id, JsonConvert.SerializeObject(newMute));
}

if (!userMute.IsNull && !e.Member.Roles.Contains(muteRole) && !e.Member.Roles.Contains(tqsMuteRole))
db.HashDeleteAsync("mutes", e.Member.Id);
if (!userMute.IsNull && !e.Member.Roles.Contains(muteRole) && !e.Member.Roles.Contains(tqsMuteRole))
db.HashDeleteAsync("mutes", e.Member.Id);
}

string rolesStr = "None";

Expand Down

0 comments on commit 7f5906d

Please sign in to comment.