feat: fetch from REST instead of cache for cross-cluster lookups

This commit is contained in:
spiral
2022-03-09 20:06:53 -05:00
parent d0ad7abb03
commit ae543b9c18
7 changed files with 80 additions and 19 deletions

View File

@@ -81,24 +81,42 @@ public class LogChannelService
if (logChannel == null || logChannel.Type != Channel.ChannelType.GuildText) return null;
// Check bot permissions
var perms = await _cache.PermissionsIn(logChannel.Id);
var perms = await GetPermissionsInLogChannel(logChannel);
if (!perms.HasFlag(PermissionSet.SendMessages | PermissionSet.EmbedLinks))
{
_logger.Information(
"Does not have permission to log proxy, ignoring (channel: {ChannelId}, guild: {GuildId}, bot permissions: {BotPermissions})",
ctx.LogChannel.Value, trigger.GuildId!.Value, perms);
logChannel.Id, trigger.GuildId!.Value, perms);
return null;
}
return logChannel.Id;
}
// todo: move this somewhere else
private async Task<PermissionSet> GetPermissionsInLogChannel(Channel channel)
{
var guild = await _cache.TryGetGuild(channel.GuildId.Value);
if (guild == null)
guild = await _rest.GetGuild(channel.GuildId.Value);
var guildMember = await _cache.TryGetSelfMember(channel.GuildId.Value);
if (guildMember == null)
guildMember = await _rest.GetGuildMember(channel.GuildId.Value, await _cache.GetOwnUser());
var perms = PermissionExtensions.PermissionsFor(guild, channel, await _cache.GetOwnUser(), guildMember);
return perms;
}
private async Task<Channel?> FindLogChannel(ulong guildId, ulong channelId)
{
// TODO: fetch it directly on cache miss?
if (await _cache.TryGetChannel(channelId) is Channel channel)
return channel;
if (await _rest.GetChannelOrNull(channelId) is Channel restChannel)
return restChannel;
// Channel doesn't exist or we don't have permission to access it, let's remove it from the database too
_logger.Warning(
"Attempted to fetch missing log channel {LogChannel} for guild {Guild}, removing from database",