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

@@ -70,9 +70,9 @@ namespace PluralKit.Bot
if (evt.Type != Message.MessageType.Default && evt.Type != Message.MessageType.Reply) return;
if (IsDuplicateMessage(evt)) return;
var guild = evt.GuildId != null ? _cache.GetGuild(evt.GuildId.Value) : null;
var channel = _cache.GetChannel(evt.ChannelId);
var rootChannel = _cache.GetRootChannel(evt.ChannelId);
var guild = evt.GuildId != null ? await _cache.GetGuild(evt.GuildId.Value) : null;
var channel = await _cache.GetChannel(evt.ChannelId);
var rootChannel = await _cache.GetRootChannel(evt.ChannelId);
// Log metrics and message info
_metrics.Measure.Meter.Mark(BotMetrics.MessagesReceived);
@@ -98,7 +98,7 @@ namespace PluralKit.Bot
private async ValueTask<bool> TryHandleLogClean(MessageCreateEvent evt, MessageContext ctx)
{
var channel = _cache.GetChannel(evt.ChannelId);
var channel = await _cache.GetChannel(evt.ChannelId);
if (!evt.Author.Bot || channel.Type != Channel.ChannelType.GuildText ||
!ctx.LogCleanupEnabled) return false;
@@ -156,7 +156,7 @@ namespace PluralKit.Bot
private async ValueTask<bool> TryHandleProxy(Shard shard, MessageCreateEvent evt, Guild guild, Channel channel, MessageContext ctx)
{
var botPermissions = _bot.PermissionsIn(channel.Id);
var botPermissions = await _bot.PermissionsIn(channel.Id);
try
{

View File

@@ -51,10 +51,10 @@ namespace PluralKit.Bot
if (!evt.Content.HasValue || !evt.Author.HasValue || !evt.Member.HasValue)
return;
var channel = _cache.GetChannel(evt.ChannelId);
var channel = await _cache.GetChannel(evt.ChannelId);
if (!DiscordUtils.IsValidGuildChannel(channel))
return;
var guild = _cache.GetGuild(channel.GuildId!.Value);
var guild = await _cache.GetGuild(channel.GuildId!.Value);
var lastMessage = _lastMessageCache.GetLastMessage(evt.ChannelId)?.Current;
// Only react to the last message in the channel
@@ -67,7 +67,7 @@ namespace PluralKit.Bot
ctx = await _repo.GetMessageContext(evt.Author.Value!.Id, channel.GuildId!.Value, evt.ChannelId);
var equivalentEvt = await GetMessageCreateEvent(evt, lastMessage, channel);
var botPermissions = _bot.PermissionsIn(channel.Id);
var botPermissions = await _bot.PermissionsIn(channel.Id);
try
{
@@ -112,7 +112,7 @@ namespace PluralKit.Bot
if (referencedMessageId == null)
return null;
var botPermissions = _bot.PermissionsIn(channelId);
var botPermissions = await _bot.PermissionsIn(channelId);
if (!botPermissions.HasFlag(PermissionSet.ReadMessageHistory))
{
_logger.Warning("Tried to get referenced message in channel {ChannelId} to reply but bot does not have Read Message History",

View File

@@ -50,7 +50,7 @@ namespace PluralKit.Bot
{
// Sometimes we get events from users that aren't in the user cache
// We just ignore all of those for now, should be quite rare...
if (!_cache.TryGetUser(evt.UserId, out var user))
if (!await _cache.TryGetUser(evt.UserId, out var user))
return;
// ignore any reactions added by *us*
@@ -60,7 +60,7 @@ namespace PluralKit.Bot
// Ignore reactions from bots (we can't DM them anyway)
if (user.Bot) return;
var channel = _cache.GetChannel(evt.ChannelId);
var channel = await _cache.GetChannel(evt.ChannelId);
// check if it's a command message first
// since this can happen in DMs as well
@@ -121,7 +121,7 @@ namespace PluralKit.Bot
private async ValueTask HandleProxyDeleteReaction(MessageReactionAddEvent evt, FullMessage msg)
{
if (!_bot.PermissionsIn(evt.ChannelId).HasFlag(PermissionSet.ManageMessages))
if (!(await _bot.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
return;
var system = await _repo.GetSystemByAccount(evt.UserId);
@@ -162,7 +162,7 @@ namespace PluralKit.Bot
private async ValueTask HandleQueryReaction(MessageReactionAddEvent evt, FullMessage msg)
{
var guild = _cache.GetGuild(evt.GuildId!.Value);
var guild = await _cache.GetGuild(evt.GuildId!.Value);
// Try to DM the user info about the message
try
@@ -185,14 +185,14 @@ namespace PluralKit.Bot
private async ValueTask HandlePingReaction(MessageReactionAddEvent evt, FullMessage msg)
{
if (!_bot.PermissionsIn(evt.ChannelId).HasFlag(PermissionSet.ManageMessages))
if (!(await _bot.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
return;
// Check if the "pinger" has permission to send messages in this channel
// (if not, PK shouldn't send messages on their behalf)
var member = await _rest.GetGuildMember(evt.GuildId!.Value, evt.UserId);
var requiredPerms = PermissionSet.ViewChannel | PermissionSet.SendMessages;
if (member == null || !_cache.PermissionsFor(evt.ChannelId, member).HasFlag(requiredPerms)) return;
if (member == null || !(await _cache.PermissionsFor(evt.ChannelId, member)).HasFlag(requiredPerms)) return;
if (msg.System.PingsEnabled)
{
@@ -240,7 +240,7 @@ namespace PluralKit.Bot
private async Task TryRemoveOriginalReaction(MessageReactionAddEvent evt)
{
if (_bot.PermissionsIn(evt.ChannelId).HasFlag(PermissionSet.ManageMessages))
if ((await _bot.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
await _rest.DeleteUserReaction(evt.ChannelId, evt.MessageId, evt.Emoji, evt.UserId);
}
}