feat: cache Discord DM channels in database
This commit is contained in:
@@ -20,7 +20,6 @@ public interface IDiscordCache
|
||||
public Task<ulong> GetOwnUser();
|
||||
public Task<Guild?> TryGetGuild(ulong guildId);
|
||||
public Task<Channel?> TryGetChannel(ulong channelId);
|
||||
public Task<Channel?> TryGetDmChannel(ulong userId);
|
||||
public Task<User?> TryGetUser(ulong userId);
|
||||
public Task<GuildMemberPartial?> TryGetSelfMember(ulong guildId);
|
||||
public Task<Role?> TryGetRole(ulong roleId);
|
||||
|
@@ -7,7 +7,6 @@ namespace Myriad.Cache;
|
||||
public class MemoryDiscordCache: IDiscordCache
|
||||
{
|
||||
private readonly ConcurrentDictionary<ulong, Channel> _channels = new();
|
||||
private readonly ConcurrentDictionary<ulong, ulong> _dmChannels = new();
|
||||
private readonly ConcurrentDictionary<ulong, GuildMemberPartial> _guildMembers = new();
|
||||
private readonly ConcurrentDictionary<ulong, CachedGuild> _guilds = new();
|
||||
private readonly ConcurrentDictionary<ulong, Role> _roles = new();
|
||||
@@ -35,10 +34,7 @@ public class MemoryDiscordCache: IDiscordCache
|
||||
|
||||
if (channel.Recipients != null)
|
||||
foreach (var recipient in channel.Recipients)
|
||||
{
|
||||
_dmChannels[recipient.Id] = channel.Id;
|
||||
await SaveUser(recipient);
|
||||
}
|
||||
}
|
||||
|
||||
public ValueTask SaveOwnUser(ulong userId)
|
||||
@@ -140,13 +136,6 @@ public class MemoryDiscordCache: IDiscordCache
|
||||
return Task.FromResult(channel);
|
||||
}
|
||||
|
||||
public Task<Channel?> TryGetDmChannel(ulong userId)
|
||||
{
|
||||
if (!_dmChannels.TryGetValue(userId, out var channelId))
|
||||
return Task.FromResult((Channel?)null);
|
||||
return TryGetChannel(channelId);
|
||||
}
|
||||
|
||||
public Task<User?> TryGetUser(ulong userId)
|
||||
{
|
||||
_users.TryGetValue(userId, out var user);
|
||||
|
@@ -58,17 +58,6 @@ public static class CacheExtensions
|
||||
return restChannel;
|
||||
}
|
||||
|
||||
public static async Task<Channel> GetOrCreateDmChannel(this IDiscordCache cache, DiscordApiClient rest,
|
||||
ulong recipientId)
|
||||
{
|
||||
if (await cache.TryGetDmChannel(recipientId) is { } cacheChannel)
|
||||
return cacheChannel;
|
||||
|
||||
var restChannel = await rest.CreateDm(recipientId);
|
||||
await cache.SaveChannel(restChannel);
|
||||
return restChannel;
|
||||
}
|
||||
|
||||
public static async Task<Channel> GetRootChannel(this IDiscordCache cache, ulong channelOrThread)
|
||||
{
|
||||
var channel = await cache.GetChannel(channelOrThread);
|
||||
|
Reference in New Issue
Block a user