refactor: cache own user ID in IDiscordCache

- remove Cluster.User
- remove Cluster.Application (it was only being used as an alternative to Cluster.User for some reason)
- move Bot.PermissionsIn to DiscordCacheExtensions
This commit is contained in:
spiral
2021-11-21 19:42:35 -05:00
parent 24ac0725af
commit 7a5ba8246e
17 changed files with 61 additions and 40 deletions

View File

@@ -78,7 +78,7 @@ namespace PluralKit.Bot
if (logChannel == null || logChannel.Type != Channel.ChannelType.GuildText) return null;
// Check bot permissions
var perms = await _bot.PermissionsIn(logChannel.Id);
var perms = await _cache.PermissionsIn(logChannel.Id);
if (!perms.HasFlag(PermissionSet.SendMessages | PermissionSet.EmbedLinks))
{
_logger.Information(

View File

@@ -89,7 +89,7 @@ namespace PluralKit.Bot
var channel = await _cache.GetChannel(msg.ChannelId);
if (channel.Type != Channel.ChannelType.GuildText) return;
if (!(await _bot.PermissionsIn(channel.Id)).HasFlag(PermissionSet.ManageMessages)) return;
if (!(await _cache.PermissionsIn(channel.Id)).HasFlag(PermissionSet.ManageMessages)) return;
// If this message is from a *webhook*, check if the name matches one of the bots we know
// TODO: do we need to do a deeper webhook origin check, or would that be too hard on the rate limit?

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
using App.Metrics;
using Myriad.Gateway;
using Myriad.Cache;
using Myriad.Rest;
using Myriad.Rest.Types.Requests;
using Myriad.Types;
@@ -24,13 +24,13 @@ namespace PluralKit.Bot
private readonly IMetrics _metrics;
private readonly ILogger _logger;
private readonly Cluster _cluster;
private readonly IDiscordCache _cache;
public WebhookCacheService(ILogger logger, IMetrics metrics, DiscordApiClient rest, Cluster cluster)
public WebhookCacheService(ILogger logger, IMetrics metrics, DiscordApiClient rest, IDiscordCache cache)
{
_metrics = metrics;
_rest = rest;
_cluster = cluster;
_cache = cache;
_logger = logger.ForContext<WebhookCacheService>();
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<Webhook>>>();
}
@@ -86,7 +86,8 @@ namespace PluralKit.Bot
var webhooks = await FetchChannelWebhooks(channelId);
// If the channel has a webhook created by PK, just return that one
var ourWebhook = webhooks.FirstOrDefault(IsWebhookMine);
var ourUserId = await _cache.GetOwnUser();
var ourWebhook = webhooks.FirstOrDefault(hook => IsWebhookMine(ourUserId, hook));
if (ourWebhook != null)
return ourWebhook;
@@ -120,7 +121,7 @@ namespace PluralKit.Bot
return await _rest.CreateWebhook(channelId, new CreateWebhookRequest(WebhookName));
}
private bool IsWebhookMine(Webhook arg) => arg.User?.Id == _cluster.User?.Id && arg.Name == WebhookName;
private bool IsWebhookMine(ulong userId, Webhook arg) => arg.User?.Id == userId && arg.Name == WebhookName;
public int CacheSize => _webhooks.Count;
}