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