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:
@@ -9,44 +9,44 @@ namespace Myriad.Extensions
|
||||
{
|
||||
public static class CacheExtensions
|
||||
{
|
||||
public static Guild GetGuild(this IDiscordCache cache, ulong guildId)
|
||||
public static async Task<Guild> GetGuild(this IDiscordCache cache, ulong guildId)
|
||||
{
|
||||
if (!cache.TryGetGuild(guildId, out var guild))
|
||||
if (!await cache.TryGetGuild(guildId, out var guild))
|
||||
throw new KeyNotFoundException($"Guild {guildId} not found in cache");
|
||||
return guild;
|
||||
}
|
||||
|
||||
public static Channel GetChannel(this IDiscordCache cache, ulong channelId)
|
||||
public static async Task<Channel> GetChannel(this IDiscordCache cache, ulong channelId)
|
||||
{
|
||||
if (!cache.TryGetChannel(channelId, out var channel))
|
||||
if (!await cache.TryGetChannel(channelId, out var channel))
|
||||
throw new KeyNotFoundException($"Channel {channelId} not found in cache");
|
||||
return channel;
|
||||
}
|
||||
|
||||
public static Channel? GetChannelOrNull(this IDiscordCache cache, ulong channelId)
|
||||
public static async Task<Channel?> GetChannelOrNull(this IDiscordCache cache, ulong channelId)
|
||||
{
|
||||
if (cache.TryGetChannel(channelId, out var channel))
|
||||
if (await cache.TryGetChannel(channelId, out var channel))
|
||||
return channel;
|
||||
return null;
|
||||
}
|
||||
|
||||
public static User GetUser(this IDiscordCache cache, ulong userId)
|
||||
public static async Task<User> GetUser(this IDiscordCache cache, ulong userId)
|
||||
{
|
||||
if (!cache.TryGetUser(userId, out var user))
|
||||
if (!await cache.TryGetUser(userId, out var user))
|
||||
throw new KeyNotFoundException($"User {userId} not found in cache");
|
||||
return user;
|
||||
}
|
||||
|
||||
public static Role GetRole(this IDiscordCache cache, ulong roleId)
|
||||
public static async Task<Role> GetRole(this IDiscordCache cache, ulong roleId)
|
||||
{
|
||||
if (!cache.TryGetRole(roleId, out var role))
|
||||
if (!await cache.TryGetRole(roleId, out var role))
|
||||
throw new KeyNotFoundException($"Role {roleId} not found in cache");
|
||||
return role;
|
||||
}
|
||||
|
||||
public static async ValueTask<User?> GetOrFetchUser(this IDiscordCache cache, DiscordApiClient rest, ulong userId)
|
||||
{
|
||||
if (cache.TryGetUser(userId, out var cacheUser))
|
||||
if (await cache.TryGetUser(userId, out var cacheUser))
|
||||
return cacheUser;
|
||||
|
||||
var restUser = await rest.GetUser(userId);
|
||||
@@ -57,7 +57,7 @@ namespace Myriad.Extensions
|
||||
|
||||
public static async ValueTask<Channel?> GetOrFetchChannel(this IDiscordCache cache, DiscordApiClient rest, ulong channelId)
|
||||
{
|
||||
if (cache.TryGetChannel(channelId, out var cacheChannel))
|
||||
if (await cache.TryGetChannel(channelId, out var cacheChannel))
|
||||
return cacheChannel;
|
||||
|
||||
var restChannel = await rest.GetChannel(channelId);
|
||||
@@ -68,7 +68,7 @@ namespace Myriad.Extensions
|
||||
|
||||
public static async Task<Channel> GetOrCreateDmChannel(this IDiscordCache cache, DiscordApiClient rest, ulong recipientId)
|
||||
{
|
||||
if (cache.TryGetDmChannel(recipientId, out var cacheChannel))
|
||||
if (await cache.TryGetDmChannel(recipientId, out var cacheChannel))
|
||||
return cacheChannel;
|
||||
|
||||
var restChannel = await rest.CreateDm(recipientId);
|
||||
@@ -76,13 +76,13 @@ namespace Myriad.Extensions
|
||||
return restChannel;
|
||||
}
|
||||
|
||||
public static Channel GetRootChannel(this IDiscordCache cache, ulong channelOrThread)
|
||||
public static async Task<Channel> GetRootChannel(this IDiscordCache cache, ulong channelOrThread)
|
||||
{
|
||||
var channel = cache.GetChannel(channelOrThread);
|
||||
var channel = await cache.GetChannel(channelOrThread);
|
||||
if (!channel.IsThread())
|
||||
return channel;
|
||||
|
||||
var parent = cache.GetChannel(channel.ParentId!.Value);
|
||||
var parent = await cache.GetChannel(channel.ParentId!.Value);
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Myriad.Cache;
|
||||
using Myriad.Gateway;
|
||||
@@ -10,24 +11,24 @@ namespace Myriad.Extensions
|
||||
{
|
||||
public static class PermissionExtensions
|
||||
{
|
||||
public static PermissionSet PermissionsFor(this IDiscordCache cache, MessageCreateEvent message) =>
|
||||
public static Task<PermissionSet> PermissionsFor(this IDiscordCache cache, MessageCreateEvent message) =>
|
||||
PermissionsFor(cache, message.ChannelId, message.Author.Id, message.Member, isWebhook: message.WebhookId != null);
|
||||
|
||||
public static PermissionSet PermissionsFor(this IDiscordCache cache, ulong channelId, GuildMember member) =>
|
||||
public static Task<PermissionSet> PermissionsFor(this IDiscordCache cache, ulong channelId, GuildMember member) =>
|
||||
PermissionsFor(cache, channelId, member.User.Id, member);
|
||||
|
||||
public static PermissionSet PermissionsFor(this IDiscordCache cache, ulong channelId, ulong userId, GuildMemberPartial? member, bool isWebhook = false)
|
||||
public static async Task<PermissionSet> PermissionsFor(this IDiscordCache cache, ulong channelId, ulong userId, GuildMemberPartial? member, bool isWebhook = false)
|
||||
{
|
||||
if (!cache.TryGetChannel(channelId, out var channel))
|
||||
if (!await cache.TryGetChannel(channelId, out var channel))
|
||||
// todo: handle channel not found better
|
||||
return PermissionSet.Dm;
|
||||
|
||||
if (channel.GuildId == null)
|
||||
return PermissionSet.Dm;
|
||||
|
||||
var rootChannel = cache.GetRootChannel(channelId);
|
||||
var rootChannel = await cache.GetRootChannel(channelId);
|
||||
|
||||
var guild = cache.GetGuild(channel.GuildId.Value);
|
||||
var guild = await cache.GetGuild(channel.GuildId.Value);
|
||||
|
||||
if (isWebhook)
|
||||
return EveryonePermissions(guild);
|
||||
@@ -38,12 +39,12 @@ namespace Myriad.Extensions
|
||||
public static PermissionSet EveryonePermissions(this Guild guild) =>
|
||||
guild.Roles.FirstOrDefault(r => r.Id == guild.Id)?.Permissions ?? PermissionSet.Dm;
|
||||
|
||||
public static PermissionSet EveryonePermissions(this IDiscordCache cache, Channel channel)
|
||||
public static async Task<PermissionSet> EveryonePermissions(this IDiscordCache cache, Channel channel)
|
||||
{
|
||||
if (channel.Type == Channel.ChannelType.Dm)
|
||||
return PermissionSet.Dm;
|
||||
|
||||
var defaultPermissions = cache.GetGuild(channel.GuildId!.Value).EveryonePermissions();
|
||||
var defaultPermissions = (await cache.GetGuild(channel.GuildId!.Value)).EveryonePermissions();
|
||||
var overwrite = channel.PermissionOverwrites?.FirstOrDefault(r => r.Id == channel.GuildId);
|
||||
if (overwrite == null)
|
||||
return defaultPermissions;
|
||||
|
Reference in New Issue
Block a user