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

@@ -14,8 +14,6 @@ namespace PluralKit.Bot;
public class Checks
{
private readonly BotConfig _botConfig;
// this must ONLY be used to get the bot's user ID
private readonly IDiscordCache _cache;
private readonly ProxyMatcher _matcher;
private readonly ProxyService _proxy;
private readonly DiscordApiClient _rest;
@@ -28,10 +26,9 @@ public class Checks
};
// todo: make sure everything uses the minimum amount of REST calls necessary
public Checks(DiscordApiClient rest, IDiscordCache cache, BotConfig botConfig, ProxyService proxy, ProxyMatcher matcher)
public Checks(DiscordApiClient rest, BotConfig botConfig, ProxyService proxy, ProxyMatcher matcher)
{
_rest = rest;
_cache = cache;
_botConfig = botConfig;
_proxy = proxy;
_matcher = matcher;
@@ -69,14 +66,14 @@ public class Checks
throw Errors.GuildNotFound(guildId);
}
var guildMember = await _rest.GetGuildMember(guild.Id, await _cache.GetOwnUser());
var guildMember = await _rest.GetGuildMember(guild.Id, _botConfig.ClientId);
// Loop through every channel and group them by sets of permissions missing
var permissionsMissing = new Dictionary<ulong, List<Channel>>();
var hiddenChannels = false;
foreach (var channel in await _rest.GetGuildChannels(guild.Id))
{
var botPermissions = PermissionExtensions.PermissionsFor(guild, channel, await _cache.GetOwnUser(), guildMember);
var botPermissions = PermissionExtensions.PermissionsFor(guild, channel, _botConfig.ClientId, guildMember);
var userPermissions = PermissionExtensions.PermissionsFor(guild, channel, ctx.Author.Id, senderGuildUser);
if ((userPermissions & PermissionSet.ViewChannel) == 0)
@@ -152,12 +149,12 @@ public class Checks
if (guild == null)
throw new PKError(error);
var guildMember = await _rest.GetGuildMember(channel.GuildId.Value, await _cache.GetOwnUser());
var guildMember = await _rest.GetGuildMember(channel.GuildId.Value, _botConfig.ClientId);
if (!await ctx.CheckPermissionsInGuildChannel(channel, PermissionSet.ViewChannel))
throw new PKError(error);
var botPermissions = PermissionExtensions.PermissionsFor(guild, channel, await _cache.GetOwnUser(), guildMember);
var botPermissions = PermissionExtensions.PermissionsFor(guild, channel, _botConfig.ClientId, guildMember);
// We use a bitfield so we can set individual permission bits
ulong missingPermissions = 0;

View File

@@ -18,26 +18,22 @@ namespace PluralKit.Bot;
public class Misc
{
private readonly BotConfig _botConfig;
private readonly IDiscordCache _cache;
private readonly CpuStatService _cpu;
private readonly IMetrics _metrics;
private readonly ShardInfoService _shards;
private readonly ModelRepository _repo;
public Misc(BotConfig botConfig, IMetrics metrics, CpuStatService cpu, ModelRepository repo, ShardInfoService shards, IDiscordCache cache)
public Misc(BotConfig botConfig, IMetrics metrics, CpuStatService cpu, ModelRepository repo, ShardInfoService shards)
{
_botConfig = botConfig;
_metrics = metrics;
_cpu = cpu;
_repo = repo;
_shards = shards;
_cache = cache;
}
public async Task Invite(Context ctx)
{
var clientId = _botConfig.ClientId ?? await _cache.GetOwnUser();
var permissions =
PermissionSet.AddReactions |
PermissionSet.AttachFiles |
@@ -48,7 +44,7 @@ public class Misc
PermissionSet.SendMessages;
var invite =
$"https://discord.com/oauth2/authorize?client_id={clientId}&scope=bot%20applications.commands&permissions={(ulong)permissions}";
$"https://discord.com/oauth2/authorize?client_id={_botConfig.ClientId}&scope=bot%20applications.commands&permissions={(ulong)permissions}";
var botName = _botConfig.IsBetaBot ? "PluralKit Beta" : "PluralKit";
await ctx.Reply($"{Emojis.Success} Use this link to add {botName} to your server:\n<{invite}>");