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

@@ -87,8 +87,8 @@ namespace PluralKit.Bot
var missingEmojiPermissions = false;
foreach (var channel in await _rest.GetGuildChannels(guild.Id))
{
var botPermissions = _bot.PermissionsIn(channel.Id);
var webhookPermissions = _cache.EveryonePermissions(channel);
var botPermissions = await _bot.PermissionsIn(channel.Id);
var webhookPermissions = await _cache.EveryonePermissions(channel);
var userPermissions = PermissionExtensions.PermissionsFor(guild, channel, ctx.Author.Id, senderGuildUser);
if ((userPermissions & PermissionSet.ViewChannel) == 0)
@@ -176,8 +176,8 @@ namespace PluralKit.Bot
if (!await ctx.CheckPermissionsInGuildChannel(channel, PermissionSet.ViewChannel))
throw new PKError(error);
var botPermissions = _bot.PermissionsIn(channel.Id);
var webhookPermissions = _cache.EveryonePermissions(channel);
var botPermissions = await _bot.PermissionsIn(channel.Id);
var webhookPermissions = await _cache.EveryonePermissions(channel);
// We use a bitfield so we can set individual permission bits
ulong missingPermissions = 0;
@@ -249,7 +249,7 @@ namespace PluralKit.Bot
throw new PKError("You can only check your own messages.");
// get the channel info
var channel = _cache.GetChannel(channelId.Value);
var channel = await _cache.GetChannel(channelId.Value);
if (channel == null)
throw new PKError("Unable to get the channel associated with this message.");

View File

@@ -67,7 +67,7 @@ namespace PluralKit.Bot
if (ctx.Guild == null)
await _rest.CreateReaction(ctx.Channel.Id, ctx.Message.Id, new() { Name = Emojis.Success });
if (ctx.BotPermissions.HasFlag(PermissionSet.ManageMessages))
if ((await ctx.BotPermissions).HasFlag(PermissionSet.ManageMessages))
await _rest.DeleteMessage(ctx.Channel.Id, ctx.Message.Id);
await _logChannel.LogMessage(ctx.MessageContext, msg.Message, ctx.Message, editedMsg, originalMsg!.Content!);
@@ -109,7 +109,7 @@ namespace PluralKit.Bot
{
var error = "The channel where the message was sent does not exist anymore, or you are missing permissions to access it.";
var channel = _cache.GetChannel(msg.Message.Channel);
var channel = await _cache.GetChannel(msg.Message.Channel);
if (channel == null)
throw new PKError(error);
@@ -162,7 +162,7 @@ namespace PluralKit.Bot
var showContent = true;
var noShowContentError = "Message deleted or inaccessible.";
var channel = _cache.GetChannel(message.Message.Channel);
var channel = await _cache.GetChannel(message.Message.Channel);
if (channel == null)
showContent = false;
else if (!await ctx.CheckPermissionsInGuildChannel(channel, PermissionSet.ViewChannel))

View File

@@ -30,7 +30,7 @@ namespace PluralKit.Bot
public async Task SetLogChannel(Context ctx)
{
ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");
await ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");
var settings = await _repo.GetGuild(ctx.Guild.Id);
if (await ctx.MatchClear("the server log channel"))
@@ -59,7 +59,7 @@ namespace PluralKit.Bot
if (channel.Type != Channel.ChannelType.GuildText)
throw new PKError("PluralKit cannot log messages to this type of channel.");
var perms = _bot.PermissionsIn(channel.Id);
var perms = await _bot.PermissionsIn(channel.Id);
if (!perms.HasFlag(PermissionSet.SendMessages))
throw new PKError("PluralKit is missing **Send Messages** permissions in the new log channel.");
if (!perms.HasFlag(PermissionSet.EmbedLinks))
@@ -71,11 +71,11 @@ namespace PluralKit.Bot
public async Task SetLogEnabled(Context ctx, bool enable)
{
ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");
await ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");
var affectedChannels = new List<Channel>();
if (ctx.Match("all"))
affectedChannels = _cache.GetGuildChannels(ctx.Guild.Id).Where(x => x.Type == Channel.ChannelType.GuildText).ToList();
affectedChannels = (await _cache.GetGuildChannels(ctx.Guild.Id)).Where(x => x.Type == Channel.ChannelType.GuildText).ToList();
else if (!ctx.HasNext()) throw new PKSyntaxError("You must pass one or more #channels.");
else while (ctx.HasNext())
{
@@ -104,13 +104,13 @@ namespace PluralKit.Bot
public async Task ShowBlacklisted(Context ctx)
{
ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");
await ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");
var blacklist = await _repo.GetGuild(ctx.Guild.Id);
// Resolve all channels from the cache and order by position
var channels = blacklist.Blacklist
.Select(id => _cache.GetChannelOrNull(id))
var channels = (await Task.WhenAll(blacklist.Blacklist
.Select(id => _cache.GetChannelOrNull(id))))
.Where(c => c != null)
.OrderBy(c => c.Position)
.ToList();
@@ -124,10 +124,10 @@ namespace PluralKit.Bot
await ctx.Paginate(channels.ToAsyncEnumerable(), channels.Count, 25,
$"Blacklisted channels for {ctx.Guild.Name}",
null,
(eb, l) =>
async (eb, l) =>
{
string CategoryName(ulong? id) =>
id != null ? _cache.GetChannel(id.Value).Name : "(no category)";
async Task<string> CategoryName(ulong? id) =>
id != null ? (await _cache.GetChannel(id.Value)).Name : "(no category)";
ulong? lastCategory = null;
@@ -136,7 +136,7 @@ namespace PluralKit.Bot
{
if (lastCategory != channel!.ParentId && fieldValue.Length > 0)
{
eb.Field(new(CategoryName(lastCategory), fieldValue.ToString()));
eb.Field(new(await CategoryName(lastCategory), fieldValue.ToString()));
fieldValue.Clear();
}
else fieldValue.Append("\n");
@@ -145,19 +145,17 @@ namespace PluralKit.Bot
lastCategory = channel.ParentId;
}
eb.Field(new(CategoryName(lastCategory), fieldValue.ToString()));
return Task.CompletedTask;
eb.Field(new(await CategoryName(lastCategory), fieldValue.ToString()));
});
}
public async Task SetBlacklisted(Context ctx, bool shouldAdd)
{
ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");
await ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");
var affectedChannels = new List<Channel>();
if (ctx.Match("all"))
affectedChannels = _cache.GetGuildChannels(ctx.Guild.Id).Where(x => x.Type == Channel.ChannelType.GuildText).ToList();
affectedChannels = (await _cache.GetGuildChannels(ctx.Guild.Id)).Where(x => x.Type == Channel.ChannelType.GuildText).ToList();
else if (!ctx.HasNext()) throw new PKSyntaxError("You must pass one or more #channels.");
else while (ctx.HasNext())
{
@@ -182,7 +180,7 @@ namespace PluralKit.Bot
public async Task SetLogCleanup(Context ctx)
{
ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");
await ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");
var botList = string.Join(", ", _cleanService.Bots.Select(b => b.Name).OrderBy(x => x.ToLowerInvariant()));