refactor(bot): remove saving own user ID from ready event, rely on ID in config

This commit is contained in:
spiral
2022-09-06 09:52:37 +00:00
parent aeb6411b6c
commit 9303dbb91e
17 changed files with 51 additions and 69 deletions

View File

@@ -10,8 +10,6 @@ public static class DiscordCacheExtensions
{
switch (evt)
{
case ReadyEvent ready:
return cache.SaveOwnUser(ready.User.Id);
case GuildCreateEvent gc:
return cache.SaveGuildCreate(gc);
case GuildUpdateEvent gu:
@@ -108,7 +106,7 @@ public static class DiscordCacheExtensions
if (channel.GuildId != null)
{
var userId = await cache.GetOwnUser();
var userId = cache.GetOwnUser();
var member = await cache.TryGetSelfMember(channel.GuildId.Value);
return await cache.PermissionsFor(channelId, userId, member);
}

View File

@@ -4,7 +4,6 @@ namespace Myriad.Cache;
public interface IDiscordCache
{
public ValueTask SaveOwnUser(ulong userId);
public ValueTask SaveGuild(Guild guild);
public ValueTask SaveChannel(Channel channel);
public ValueTask SaveUser(User user);
@@ -17,7 +16,7 @@ public interface IDiscordCache
public ValueTask RemoveUser(ulong userId);
public ValueTask RemoveRole(ulong guildId, ulong roleId);
public Task<ulong> GetOwnUser();
internal ulong GetOwnUser();
public Task<Guild?> TryGetGuild(ulong guildId);
public Task<Channel?> TryGetChannel(ulong channelId);
public Task<User?> TryGetUser(ulong userId);

View File

@@ -11,7 +11,12 @@ public class MemoryDiscordCache: IDiscordCache
private readonly ConcurrentDictionary<ulong, CachedGuild> _guilds = new();
private readonly ConcurrentDictionary<ulong, Role> _roles = new();
private readonly ConcurrentDictionary<ulong, User> _users = new();
private ulong? _ownUserId { get; set; }
private readonly ulong _ownUserId;
public MemoryDiscordCache(ulong ownUserId)
{
_ownUserId = ownUserId;
}
public ValueTask SaveGuild(Guild guild)
{
@@ -48,15 +53,6 @@ public class MemoryDiscordCache: IDiscordCache
await SaveUser(recipient);
}
public ValueTask SaveOwnUser(ulong userId)
{
// this (hopefully) never changes at runtime, so we skip out on re-assigning it
if (_ownUserId == null)
_ownUserId = userId;
return default;
}
public ValueTask SaveUser(User user)
{
_users[user.Id] = user;
@@ -127,7 +123,7 @@ public class MemoryDiscordCache: IDiscordCache
return default;
}
public Task<ulong> GetOwnUser() => Task.FromResult(_ownUserId!.Value);
public ulong GetOwnUser() => _ownUserId;
public ValueTask RemoveRole(ulong guildId, ulong roleId)
{

View File

@@ -13,18 +13,18 @@ namespace Myriad.Cache;
public class RedisDiscordCache: IDiscordCache
{
private readonly ILogger _logger;
public RedisDiscordCache(ILogger logger)
private readonly ulong _ownUserId;
public RedisDiscordCache(ILogger logger, ulong ownUserId)
{
_logger = logger;
_ownUserId = ownUserId;
}
private ConnectionMultiplexer _redis { get; set; }
private ulong _ownUserId { get; set; }
public async Task InitAsync(string addr, ulong ownUserId)
public async Task InitAsync(string addr)
{
_redis = await ConnectionMultiplexer.ConnectAsync(addr);
_ownUserId = ownUserId;
}
private IDatabase db => _redis.GetDatabase().WithKeyPrefix("discord:");
@@ -78,12 +78,6 @@ public class RedisDiscordCache: IDiscordCache
await SaveUser(recipient);
}
public ValueTask SaveOwnUser(ulong userId)
{
// we get the own user ID in InitAsync, so no need to save it here
return default;
}
public async ValueTask SaveUser(User user)
{
_logger.Verbose("Saving user {UserId} to redis", user.Id);
@@ -161,8 +155,7 @@ public class RedisDiscordCache: IDiscordCache
public async ValueTask RemoveUser(ulong userId)
=> await db.HashDeleteAsync("users", userId);
// todo: try getting this from redis if we don't have it yet
public Task<ulong> GetOwnUser() => Task.FromResult(_ownUserId);
public ulong GetOwnUser() => _ownUserId;
public async ValueTask RemoveRole(ulong guildId, ulong roleId)
{