refactor: don't use 'out' in IDiscordCache methods

this change is required for async cache (.NET doesn't support async ref/out params)
This commit is contained in:
spiral
2021-11-18 13:11:02 -05:00
parent 3ba46ff456
commit 0efaab6d95
11 changed files with 48 additions and 52 deletions

View File

@@ -157,7 +157,7 @@ namespace PluralKit.Bot
if (!MentionUtils.TryParseChannel(ctx.PeekArgument(), out var id))
return null;
if (!await ctx.Cache.TryGetChannel(id, out var channel))
if (!(await ctx.Cache.TryGetChannel(id) is Channel channel))
return null;
if (!DiscordUtils.IsValidGuildChannel(channel))
@@ -167,12 +167,12 @@ namespace PluralKit.Bot
return channel;
}
public static Guild MatchGuild(this Context ctx)
public static async Task<Guild> MatchGuild(this Context ctx)
{
try
{
var id = ulong.Parse(ctx.PeekArgument());
ctx.Cache.TryGetGuild(id, out var guild);
var guild = await ctx.Cache.TryGetGuild(id);
if (guild != null)
ctx.PopArgument();

View File

@@ -110,7 +110,7 @@ namespace PluralKit.Bot
// Resolve all channels from the cache and order by position
var channels = (await Task.WhenAll(blacklist.Blacklist
.Select(id => _cache.GetChannelOrNull(id))))
.Select(id => _cache.TryGetChannel(id))))
.Where(c => c != null)
.OrderBy(c => c.Position)
.ToList();

View File

@@ -436,7 +436,7 @@ namespace PluralKit.Bot
{
ctx.CheckSystem();
var guild = ctx.MatchGuild() ?? ctx.Guild ??
var guild = await ctx.MatchGuild() ?? ctx.Guild ??
throw new PKError("You must run this command in a server or pass a server ID.");
var gs = await _repo.GetSystemGuild(guild.Id, ctx.System.Id);

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 (!await _cache.TryGetUser(evt.UserId, out var user))
if (!(await _cache.TryGetUser(evt.UserId) is User user))
return;
// ignore any reactions added by *us*

View File

@@ -335,10 +335,10 @@ namespace PluralKit.Bot
var roles = memberInfo?.Roles?.ToList();
if (roles != null && roles.Count > 0 && showContent)
{
var rolesString = string.Join(", ", roles
.Select(id =>
var rolesString = string.Join(", ", (await Task.WhenAll(roles
.Select(async id =>
{
_cache.TryGetRole(id, out var role);
var role = await _cache.TryGetRole(id);
if (role != null)
return role;
return new Role()
@@ -346,7 +346,7 @@ namespace PluralKit.Bot
Name = "*(unknown role)*",
Position = 0,
};
})
})))
.OrderByDescending(role => role.Position)
.Select(role => role.Name));
eb.Field(new($"Account roles ({roles.Count})", rolesString.Truncate(1024)));

View File

@@ -93,7 +93,7 @@ namespace PluralKit.Bot
private async Task<Channel?> FindLogChannel(ulong guildId, ulong channelId)
{
// TODO: fetch it directly on cache miss?
if (await _cache.TryGetChannel(channelId, out var channel))
if (await _cache.TryGetChannel(channelId) is Channel channel)
return channel;
// Channel doesn't exist or we don't have permission to access it, let's remove it from the database too

View File

@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using Myriad.Cache;
using Myriad.Extensions;
using Myriad.Gateway;
using Myriad.Types;
using Serilog.Core;
using Serilog.Events;
@@ -39,7 +40,7 @@ namespace PluralKit.Bot
{
props.Add(new("ChannelId", new ScalarValue(channel.Value)));
if (await _cache.TryGetChannel(channel.Value, out _))
if (await _cache.TryGetChannel(channel.Value) != null)
{
var botPermissions = await _bot.PermissionsIn(channel.Value);
props.Add(new("BotPermissions", new ScalarValue(botPermissions)));