feat: async cache

this breaks logging bot permissions to Sentry.

we haven't had a need to check those recently (permissions issues were because of broken cache), so this is fine for now
this should be re-added in the future though
This commit is contained in:
spiral
2021-11-17 20:41:02 -05:00
parent 45258d519e
commit e7f36eb31f
24 changed files with 134 additions and 126 deletions

View File

@@ -165,7 +165,7 @@ namespace PluralKit.Bot
if (currentPage < 0) currentPage += pageCount;
// If we can, remove the user's reaction (so they can press again quickly)
if (ctx.BotPermissions.HasFlag(PermissionSet.ManageMessages))
if ((await ctx.BotPermissions).HasFlag(PermissionSet.ManageMessages))
await ctx.Rest.DeleteUserReaction(msg.ChannelId, msg.Id, reaction.Emoji, reaction.UserId);
// Edit the embed with the new page
@@ -179,7 +179,7 @@ namespace PluralKit.Bot
}
// todo: re-check
if (ctx.BotPermissions.HasFlag(PermissionSet.ManageMessages))
if ((await ctx.BotPermissions).HasFlag(PermissionSet.ManageMessages))
await ctx.Rest.DeleteAllReactions(msg.ChannelId, msg.Id);
}
// If we get a "NotFound" error, the message has been deleted and thus not our problem
@@ -292,7 +292,7 @@ namespace PluralKit.Bot
var task = f();
// If we don't have permission to add reactions, don't bother, and just await the task normally.
if (!DiscordUtils.HasReactionPermissions(ctx)) return await task;
if (!await DiscordUtils.HasReactionPermissions(ctx)) return await task;
try
{

View File

@@ -196,10 +196,10 @@ namespace PluralKit.Bot
public static string EventType(this IGatewayEvent evt) =>
evt.GetType().Name.Replace("Event", "");
public static bool HasReactionPermissions(Context ctx)
public static async Task<bool> HasReactionPermissions(Context ctx)
{
var neededPermissions = PermissionSet.AddReactions | PermissionSet.ReadMessageHistory;
return ((ctx.BotPermissions & neededPermissions) == neededPermissions);
return ((await ctx.BotPermissions & neededPermissions) == neededPermissions);
}
public static bool IsValidGuildChannel(Channel channel) =>

View File

@@ -1,7 +1,9 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Myriad.Extensions;
using Myriad.Gateway;
using Myriad.Types;
using Sentry;
@@ -42,8 +44,10 @@ namespace PluralKit.Bot
// Also report information about the bot's permissions in the channel
// We get a lot of permission errors so this'll be useful for determining problems
var perms = _bot.PermissionsIn(evt.ChannelId);
scope.AddBreadcrumb(perms.ToPermissionString(), "permissions");
// todo: re-add this
// var perms = _bot.PermissionsIn(evt.ChannelId);
// scope.AddBreadcrumb(perms.ToPermissionString(), "permissions");
}
public void Enrich(Scope scope, Shard shard, MessageDeleteEvent evt)

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Myriad.Cache;
using Myriad.Extensions;
@@ -20,7 +21,7 @@ namespace PluralKit.Bot
_cache = cache;
}
public ILogEventEnricher GetEnricher(Shard shard, IGatewayEvent evt)
public async Task<ILogEventEnricher> GetEnricher(Shard shard, IGatewayEvent evt)
{
var props = new List<LogEventProperty>
{
@@ -38,9 +39,9 @@ namespace PluralKit.Bot
{
props.Add(new("ChannelId", new ScalarValue(channel.Value)));
if (_cache.TryGetChannel(channel.Value, out _))
if (await _cache.TryGetChannel(channel.Value, out _))
{
var botPermissions = _bot.PermissionsIn(channel.Value);
var botPermissions = await _bot.PermissionsIn(channel.Value);
props.Add(new("BotPermissions", new ScalarValue(botPermissions)));
}
}
@@ -52,7 +53,7 @@ namespace PluralKit.Bot
props.Add(new("UserId", new ScalarValue(user.Value)));
if (evt is MessageCreateEvent mce)
props.Add(new("UserPermissions", new ScalarValue(_cache.PermissionsFor(mce))));
props.Add(new("UserPermissions", new ScalarValue(await _cache.PermissionsFor(mce))));
return new Inner(props);
}