diff --git a/Myriad/Cache/IDiscordCache.cs b/Myriad/Cache/IDiscordCache.cs index 42a5a45b..095585c3 100644 --- a/Myriad/Cache/IDiscordCache.cs +++ b/Myriad/Cache/IDiscordCache.cs @@ -18,11 +18,11 @@ namespace Myriad.Cache public ValueTask RemoveUser(ulong userId); public ValueTask RemoveRole(ulong guildId, ulong roleId); - public Task TryGetGuild(ulong guildId, out Guild guild); - public Task TryGetChannel(ulong channelId, out Channel channel); - public Task TryGetDmChannel(ulong userId, out Channel channel); - public Task TryGetUser(ulong userId, out User user); - public Task TryGetRole(ulong roleId, out Role role); + public Task TryGetGuild(ulong guildId); + public Task TryGetChannel(ulong channelId); + public Task TryGetDmChannel(ulong userId); + public Task TryGetUser(ulong userId); + public Task TryGetRole(ulong roleId); public IAsyncEnumerable GetAllGuilds(); public Task> GetGuildChannels(ulong guildId); diff --git a/Myriad/Cache/MemoryDiscordCache.cs b/Myriad/Cache/MemoryDiscordCache.cs index 06f920c5..68181783 100644 --- a/Myriad/Cache/MemoryDiscordCache.cs +++ b/Myriad/Cache/MemoryDiscordCache.cs @@ -124,34 +124,36 @@ namespace Myriad.Cache return default; } - public Task TryGetGuild(ulong guildId, out Guild guild) + public Task TryGetGuild(ulong guildId) { - if (_guilds.TryGetValue(guildId, out var cg)) - { - guild = cg.Guild; - return Task.FromResult(true); - } - - guild = null!; - return Task.FromResult(false); + _guilds.TryGetValue(guildId, out var cg); + return Task.FromResult(cg?.Guild); } - public Task TryGetChannel(ulong channelId, out Channel channel) => - Task.FromResult(_channels.TryGetValue(channelId, out channel!)); - - public Task TryGetDmChannel(ulong userId, out Channel channel) + public Task TryGetChannel(ulong channelId) + { + _channels.TryGetValue(channelId, out var channel); + return Task.FromResult(channel); + } + + public Task TryGetDmChannel(ulong userId) { - channel = default!; if (!_dmChannels.TryGetValue(userId, out var channelId)) - return Task.FromResult(false); - return TryGetChannel(channelId, out channel); + return Task.FromResult((Channel?) null); + return TryGetChannel(channelId); } - public Task TryGetUser(ulong userId, out User user) => - Task.FromResult(_users.TryGetValue(userId, out user!)); + public Task TryGetUser(ulong userId) + { + _users.TryGetValue(userId, out var user); + return Task.FromResult(user); + } - public Task TryGetRole(ulong roleId, out Role role) => - Task.FromResult(_roles.TryGetValue(roleId, out role!)); + public Task TryGetRole(ulong roleId) + { + _roles.TryGetValue(roleId, out var role); + return Task.FromResult(role); + } public IAsyncEnumerable GetAllGuilds() { diff --git a/Myriad/Extensions/CacheExtensions.cs b/Myriad/Extensions/CacheExtensions.cs index a5c7343c..30d7458c 100644 --- a/Myriad/Extensions/CacheExtensions.cs +++ b/Myriad/Extensions/CacheExtensions.cs @@ -11,42 +11,35 @@ namespace Myriad.Extensions { public static async Task GetGuild(this IDiscordCache cache, ulong guildId) { - if (!await cache.TryGetGuild(guildId, out var guild)) + if (!(await cache.TryGetGuild(guildId) is Guild guild)) throw new KeyNotFoundException($"Guild {guildId} not found in cache"); return guild; } public static async Task GetChannel(this IDiscordCache cache, ulong channelId) { - if (!await cache.TryGetChannel(channelId, out var channel)) + if (!(await cache.TryGetChannel(channelId) is Channel channel)) throw new KeyNotFoundException($"Channel {channelId} not found in cache"); return channel; } - public static async Task GetChannelOrNull(this IDiscordCache cache, ulong channelId) - { - if (await cache.TryGetChannel(channelId, out var channel)) - return channel; - return null; - } - public static async Task GetUser(this IDiscordCache cache, ulong userId) { - if (!await cache.TryGetUser(userId, out var user)) + if (!(await cache.TryGetUser(userId) is User user)) throw new KeyNotFoundException($"User {userId} not found in cache"); return user; } public static async Task GetRole(this IDiscordCache cache, ulong roleId) { - if (!await cache.TryGetRole(roleId, out var role)) + if (!(await cache.TryGetRole(roleId) is Role role)) throw new KeyNotFoundException($"Role {roleId} not found in cache"); return role; } public static async ValueTask GetOrFetchUser(this IDiscordCache cache, DiscordApiClient rest, ulong userId) { - if (await cache.TryGetUser(userId, out var cacheUser)) + if (await cache.TryGetUser(userId) is User cacheUser) return cacheUser; var restUser = await rest.GetUser(userId); @@ -57,7 +50,7 @@ namespace Myriad.Extensions public static async ValueTask GetOrFetchChannel(this IDiscordCache cache, DiscordApiClient rest, ulong channelId) { - if (await cache.TryGetChannel(channelId, out var cacheChannel)) + if (await cache.TryGetChannel(channelId) is {} cacheChannel) return cacheChannel; var restChannel = await rest.GetChannel(channelId); @@ -68,7 +61,7 @@ namespace Myriad.Extensions public static async Task GetOrCreateDmChannel(this IDiscordCache cache, DiscordApiClient rest, ulong recipientId) { - if (await cache.TryGetDmChannel(recipientId, out var cacheChannel)) + if (await cache.TryGetDmChannel(recipientId) is {} cacheChannel) return cacheChannel; var restChannel = await rest.CreateDm(recipientId); diff --git a/Myriad/Extensions/PermissionExtensions.cs b/Myriad/Extensions/PermissionExtensions.cs index abfc2b7a..3d326f25 100644 --- a/Myriad/Extensions/PermissionExtensions.cs +++ b/Myriad/Extensions/PermissionExtensions.cs @@ -19,7 +19,7 @@ namespace Myriad.Extensions public static async Task PermissionsFor(this IDiscordCache cache, ulong channelId, ulong userId, GuildMemberPartial? member, bool isWebhook = false) { - if (!await cache.TryGetChannel(channelId, out var channel)) + if (!(await cache.TryGetChannel(channelId) is Channel channel)) // todo: handle channel not found better return PermissionSet.Dm; diff --git a/PluralKit.Bot/CommandSystem/ContextEntityArgumentsExt.cs b/PluralKit.Bot/CommandSystem/ContextEntityArgumentsExt.cs index ebfd187c..c5d05553 100644 --- a/PluralKit.Bot/CommandSystem/ContextEntityArgumentsExt.cs +++ b/PluralKit.Bot/CommandSystem/ContextEntityArgumentsExt.cs @@ -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 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(); diff --git a/PluralKit.Bot/Commands/ServerConfig.cs b/PluralKit.Bot/Commands/ServerConfig.cs index 847c445d..b9e08a83 100644 --- a/PluralKit.Bot/Commands/ServerConfig.cs +++ b/PluralKit.Bot/Commands/ServerConfig.cs @@ -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(); diff --git a/PluralKit.Bot/Commands/SystemEdit.cs b/PluralKit.Bot/Commands/SystemEdit.cs index 29354d46..bbf05c89 100644 --- a/PluralKit.Bot/Commands/SystemEdit.cs +++ b/PluralKit.Bot/Commands/SystemEdit.cs @@ -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); diff --git a/PluralKit.Bot/Handlers/ReactionAdded.cs b/PluralKit.Bot/Handlers/ReactionAdded.cs index 761cd149..6e7faa47 100644 --- a/PluralKit.Bot/Handlers/ReactionAdded.cs +++ b/PluralKit.Bot/Handlers/ReactionAdded.cs @@ -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* diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index 7d36b304..b4a64d8b 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -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))); diff --git a/PluralKit.Bot/Services/LogChannelService.cs b/PluralKit.Bot/Services/LogChannelService.cs index b7a7e522..ef23c18e 100644 --- a/PluralKit.Bot/Services/LogChannelService.cs +++ b/PluralKit.Bot/Services/LogChannelService.cs @@ -93,7 +93,7 @@ namespace PluralKit.Bot private async Task 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 diff --git a/PluralKit.Bot/Utils/SerilogGatewayEnricherFactory.cs b/PluralKit.Bot/Utils/SerilogGatewayEnricherFactory.cs index 9c78b6eb..d99e0b63 100644 --- a/PluralKit.Bot/Utils/SerilogGatewayEnricherFactory.cs +++ b/PluralKit.Bot/Utils/SerilogGatewayEnricherFactory.cs @@ -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)));