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

@@ -54,7 +54,9 @@ namespace PluralKit.Bot
// for now, only return error messages for explicit commands
public ulong? ErrorChannelFor(MessageCreateEvent evt)
{
if (!HasCommandPrefix(evt.Content, _cluster.User?.Id ?? default, out var cmdStart) || cmdStart == evt.Content.Length)
// todo: fix @mention prefix
// it only breaks error reporting so I'm not *too* worried about it, but should be fixed eventually
if (!HasCommandPrefix(evt.Content, default, out var cmdStart) || cmdStart == evt.Content.Length)
return null;
return evt.ChannelId;
@@ -156,7 +158,7 @@ namespace PluralKit.Bot
private async ValueTask<bool> TryHandleProxy(Shard shard, MessageCreateEvent evt, Guild guild, Channel channel, MessageContext ctx)
{
var botPermissions = await _bot.PermissionsIn(channel.Id);
var botPermissions = await _cache.PermissionsIn(channel.Id);
try
{

View File

@@ -45,7 +45,7 @@ namespace PluralKit.Bot
public async Task Handle(Shard shard, MessageUpdateEvent evt)
{
if (evt.Author.Value?.Id == _client.User?.Id) return;
if (evt.Author.Value?.Id == await _cache.GetOwnUser()) return;
// Edit message events sometimes arrive with missing data; double-check it's all there
if (!evt.Content.HasValue || !evt.Author.HasValue || !evt.Member.HasValue)
@@ -67,7 +67,7 @@ namespace PluralKit.Bot
ctx = await _repo.GetMessageContext(evt.Author.Value!.Id, channel.GuildId!.Value, evt.ChannelId);
var equivalentEvt = await GetMessageCreateEvent(evt, lastMessage, channel);
var botPermissions = await _bot.PermissionsIn(channel.Id);
var botPermissions = await _cache.PermissionsIn(channel.Id);
try
{
@@ -112,7 +112,7 @@ namespace PluralKit.Bot
if (referencedMessageId == null)
return null;
var botPermissions = await _bot.PermissionsIn(channelId);
var botPermissions = await _cache.PermissionsIn(channelId);
if (!botPermissions.HasFlag(PermissionSet.ReadMessageHistory))
{
_logger.Warning("Tried to get referenced message in channel {ChannelId} to reply but bot does not have Read Message History",

View File

@@ -54,7 +54,7 @@ namespace PluralKit.Bot
return;
// ignore any reactions added by *us*
if (evt.UserId == _cluster.User?.Id)
if (evt.UserId == await _cache.GetOwnUser())
return;
// Ignore reactions from bots (we can't DM them anyway)
@@ -121,7 +121,7 @@ namespace PluralKit.Bot
private async ValueTask HandleProxyDeleteReaction(MessageReactionAddEvent evt, FullMessage msg)
{
if (!(await _bot.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
if (!(await _cache.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
return;
var system = await _repo.GetSystemByAccount(evt.UserId);
@@ -185,7 +185,7 @@ namespace PluralKit.Bot
private async ValueTask HandlePingReaction(MessageReactionAddEvent evt, FullMessage msg)
{
if (!(await _bot.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
if (!(await _cache.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
return;
// Check if the "pinger" has permission to send messages in this channel
@@ -240,7 +240,7 @@ namespace PluralKit.Bot
private async Task TryRemoveOriginalReaction(MessageReactionAddEvent evt)
{
if ((await _bot.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
if ((await _cache.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
await _rest.DeleteUserReaction(evt.ChannelId, evt.MessageId, evt.Emoji, evt.UserId);
}
}