Fixed Reaction Roles not working properly with animated emojis, closes #369

This commit is contained in:
Kwoth
2022-07-28 08:30:28 +02:00
parent bedba98130
commit a190a3d933

View File

@@ -58,7 +58,7 @@ public sealed class ReactionRolesService : IReadyExecutor, INService, IReactionR
} }
private async Task<(IGuildUser, IRole)> GetUserAndRoleAsync( private async Task<(IGuildUser, IRole)> GetUserAndRoleAsync(
SocketReaction r, ulong userId,
ReactionRoleV2 rero) ReactionRoleV2 rero)
{ {
var guild = _client.GetGuild(rero.GuildId); var guild = _client.GetGuild(rero.GuildId);
@@ -67,8 +67,8 @@ public sealed class ReactionRolesService : IReadyExecutor, INService, IReactionR
if (role is null) if (role is null)
return default; return default;
var user = guild.GetUser(r.UserId) as IGuildUser var user = guild.GetUser(userId) as IGuildUser
?? await _client.Rest.GetGuildUserAsync(guild.Id, r.UserId); ?? await _client.Rest.GetGuildUserAsync(guild.Id, userId);
if (user is null) if (user is null)
return default; return default;
@@ -77,20 +77,23 @@ public sealed class ReactionRolesService : IReadyExecutor, INService, IReactionR
} }
private Task ClientOnReactionRemoved( private Task ClientOnReactionRemoved(
Cacheable<IUserMessage, ulong> msg, Cacheable<IUserMessage, ulong> cmsg,
Cacheable<IMessageChannel, ulong> ch, Cacheable<IMessageChannel, ulong> ch,
SocketReaction r) SocketReaction r)
{ {
if (!_cache.TryGetValue(msg.Id, out var reros)) if (!_cache.TryGetValue(cmsg.Id, out var reros))
return Task.CompletedTask; return Task.CompletedTask;
_ = Task.Run(async () => _ = Task.Run(async () =>
{ {
var rero = reros.FirstOrDefault(x => x.Emote == r.Emote.Name || x.Emote == r.Emote.ToString()); var emote = await GetFixedEmoteAsync(cmsg, r.Emote);
var rero = reros.FirstOrDefault(x => x.Emote == emote.Name
|| x.Emote == emote.ToString());
if (rero is null) if (rero is null)
return; return;
var (user, role) = await GetUserAndRoleAsync(r, rero); var (user, role) = await GetUserAndRoleAsync(r.UserId, rero);
if (user.IsBot) if (user.IsBot)
return; return;
@@ -112,6 +115,24 @@ public sealed class ReactionRolesService : IReadyExecutor, INService, IReactionR
return Task.CompletedTask; return Task.CompletedTask;
} }
// had to add this because for some reason, reactionremoved event's reaction doesn't have IsAnimated set,
// causing the .ToString() to be wrong on animated custom emotes
private async Task<IEmote> GetFixedEmoteAsync(
Cacheable<IUserMessage, ulong> cmsg,
IEmote inputEmote)
{
// this should only run for emote
if (inputEmote is not Emote e)
return inputEmote;
// try to get the message and pull
var msg = await cmsg.GetOrDownloadAsync();
var emote = msg.Reactions.Keys.FirstOrDefault(x => e.Equals(x));
return emote ?? inputEmote;
}
private Task ClientOnReactionAdded( private Task ClientOnReactionAdded(
Cacheable<IUserMessage, ulong> msg, Cacheable<IUserMessage, ulong> msg,
Cacheable<IMessageChannel, ulong> ch, Cacheable<IMessageChannel, ulong> ch,
@@ -126,7 +147,7 @@ public sealed class ReactionRolesService : IReadyExecutor, INService, IReactionR
if (rero is null) if (rero is null)
return; return;
var (user, role) = await GetUserAndRoleAsync(r, rero); var (user, role) = await GetUserAndRoleAsync(r.UserId, rero);
if (user.IsBot) if (user.IsBot)
return; return;