2020-12-22 15:55:13 +00:00
|
|
|
using Myriad.Cache;
|
2020-12-25 12:19:35 +00:00
|
|
|
using Myriad.Rest;
|
2020-12-22 15:55:13 +00:00
|
|
|
using Myriad.Types;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace Myriad.Extensions;
|
|
|
|
|
|
|
|
public static class CacheExtensions
|
2020-12-22 15:55:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public static async Task<Guild> GetGuild(this IDiscordCache cache, ulong guildId)
|
2020-12-22 15:55:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!(await cache.TryGetGuild(guildId) is Guild guild))
|
|
|
|
throw new KeyNotFoundException($"Guild {guildId} not found in cache");
|
|
|
|
return guild;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static async Task<Channel> GetChannel(this IDiscordCache cache, ulong channelId)
|
|
|
|
{
|
|
|
|
if (!(await cache.TryGetChannel(channelId) is Channel channel))
|
|
|
|
throw new KeyNotFoundException($"Channel {channelId} not found in cache");
|
|
|
|
return channel;
|
|
|
|
}
|
2020-12-22 15:55:13 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static async Task<User> GetUser(this IDiscordCache cache, ulong userId)
|
|
|
|
{
|
|
|
|
if (!(await cache.TryGetUser(userId) is User user))
|
|
|
|
throw new KeyNotFoundException($"User {userId} not found in cache");
|
|
|
|
return user;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static async Task<Role> GetRole(this IDiscordCache cache, ulong roleId)
|
|
|
|
{
|
|
|
|
if (!(await cache.TryGetRole(roleId) is Role role))
|
|
|
|
throw new KeyNotFoundException($"Role {roleId} not found in cache");
|
|
|
|
return role;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static async ValueTask<User?> GetOrFetchUser(this IDiscordCache cache, DiscordApiClient rest,
|
|
|
|
ulong userId)
|
|
|
|
{
|
|
|
|
if (await cache.TryGetUser(userId) is User cacheUser)
|
|
|
|
return cacheUser;
|
2020-12-25 12:19:35 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var restUser = await rest.GetUser(userId);
|
|
|
|
if (restUser != null)
|
|
|
|
await cache.SaveUser(restUser);
|
|
|
|
return restUser;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static async ValueTask<Channel?> GetOrFetchChannel(this IDiscordCache cache, DiscordApiClient rest,
|
|
|
|
ulong channelId)
|
|
|
|
{
|
|
|
|
if (await cache.TryGetChannel(channelId) is { } cacheChannel)
|
|
|
|
return cacheChannel;
|
2020-12-25 12:58:45 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var restChannel = await rest.GetChannel(channelId);
|
|
|
|
if (restChannel != null)
|
|
|
|
await cache.SaveChannel(restChannel);
|
|
|
|
return restChannel;
|
|
|
|
}
|
2020-12-25 12:58:45 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static async Task<Channel> GetRootChannel(this IDiscordCache cache, ulong channelOrThread)
|
|
|
|
{
|
|
|
|
var channel = await cache.GetChannel(channelOrThread);
|
|
|
|
if (!channel.IsThread())
|
|
|
|
return channel;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var parent = await cache.GetChannel(channel.ParentId!.Value);
|
|
|
|
return parent;
|
2020-12-22 15:55:13 +00:00
|
|
|
}
|
|
|
|
}
|