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

@@ -15,6 +15,7 @@ namespace PluralKit.Bot;
public class LogChannelService
{
private readonly Bot _bot;
private readonly BotConfig _config;
private readonly IDiscordCache _cache;
private readonly IDatabase _db;
private readonly EmbedService _embed;
@@ -23,7 +24,7 @@ public class LogChannelService
private readonly DiscordApiClient _rest;
public LogChannelService(EmbedService embed, ILogger logger, IDatabase db, ModelRepository repo,
IDiscordCache cache, DiscordApiClient rest, Bot bot)
IDiscordCache cache, DiscordApiClient rest, Bot bot, BotConfig config)
{
_embed = embed;
_db = db;
@@ -31,6 +32,7 @@ public class LogChannelService
_cache = cache;
_rest = rest;
_bot = bot;
_config = config;
_logger = logger.ForContext<LogChannelService>();
}
@@ -97,9 +99,9 @@ public class LogChannelService
var guildMember = await _cache.TryGetSelfMember(channel.GuildId.Value);
if (guildMember == null)
guildMember = await _rest.GetGuildMember(channel.GuildId.Value, await _cache.GetOwnUser());
guildMember = await _rest.GetGuildMember(channel.GuildId.Value, _config.ClientId);
var perms = PermissionExtensions.PermissionsFor(guild, channel, await _cache.GetOwnUser(), guildMember);
var perms = PermissionExtensions.PermissionsFor(guild, channel, _config.ClientId, guildMember);
return perms;
}

View File

@@ -17,16 +17,18 @@ public class WebhookCacheService
private readonly IDiscordCache _cache;
private readonly ILogger _logger;
private readonly IMetrics _metrics;
private readonly BotConfig _config;
private readonly DiscordApiClient _rest;
private readonly ConcurrentDictionary<ulong, Lazy<Task<Webhook>>> _webhooks;
public WebhookCacheService(ILogger logger, IMetrics metrics, DiscordApiClient rest, IDiscordCache cache)
public WebhookCacheService(ILogger logger, IMetrics metrics, DiscordApiClient rest, IDiscordCache cache, BotConfig config)
{
_metrics = metrics;
_rest = rest;
_cache = cache;
_config = config;
_logger = logger.ForContext<WebhookCacheService>();
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<Webhook>>>();
}
@@ -86,8 +88,7 @@ public class WebhookCacheService
var webhooks = await FetchChannelWebhooks(channelId);
// If the channel has a webhook created by PK, just return that one
var ourUserId = await _cache.GetOwnUser();
var ourWebhook = webhooks.FirstOrDefault(hook => IsWebhookMine(ourUserId, hook));
var ourWebhook = webhooks.FirstOrDefault(hook => IsWebhookMine(hook));
if (ourWebhook != null)
return ourWebhook;
@@ -122,5 +123,5 @@ public class WebhookCacheService
return await _rest.CreateWebhook(channelId, new CreateWebhookRequest(WebhookName));
}
private bool IsWebhookMine(ulong userId, Webhook arg) => arg.User?.Id == userId && arg.Name == WebhookName;
private bool IsWebhookMine(Webhook arg) => arg.User?.Id == _config.ClientId && arg.Name == WebhookName;
}