feat: upgrade to .NET 6, refactor everything

This commit is contained in:
spiral
2021-11-26 21:10:56 -05:00
parent d28e99ba43
commit 1918c56937
314 changed files with 27954 additions and 27966 deletions

View File

@@ -1,13 +1,10 @@
using System.Threading.Tasks;
using Myriad.Gateway;
namespace PluralKit.Bot
{
public interface IEventHandler<in T> where T : IGatewayEvent
{
Task Handle(Shard shard, T evt);
namespace PluralKit.Bot;
ulong? ErrorChannelFor(T evt) => null;
}
public interface IEventHandler<in T> where T : IGatewayEvent
{
Task Handle(Shard shard, T evt);
ulong? ErrorChannelFor(T evt) => null;
}

View File

@@ -1,33 +1,30 @@
using System.Threading.Tasks;
using Autofac;
using Myriad.Gateway;
using Myriad.Types;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class InteractionCreated: IEventHandler<InteractionCreateEvent>
{
public class InteractionCreated: IEventHandler<InteractionCreateEvent>
private readonly InteractionDispatchService _interactionDispatch;
private readonly ILifetimeScope _services;
public InteractionCreated(InteractionDispatchService interactionDispatch, ILifetimeScope services)
{
private readonly InteractionDispatchService _interactionDispatch;
private readonly ILifetimeScope _services;
_interactionDispatch = interactionDispatch;
_services = services;
}
public InteractionCreated(InteractionDispatchService interactionDispatch, ILifetimeScope services)
public async Task Handle(Shard shard, InteractionCreateEvent evt)
{
if (evt.Type == Interaction.InteractionType.MessageComponent)
{
_interactionDispatch = interactionDispatch;
_services = services;
}
public async Task Handle(Shard shard, InteractionCreateEvent evt)
{
if (evt.Type == Interaction.InteractionType.MessageComponent)
var customId = evt.Data?.CustomId;
if (customId != null)
{
var customId = evt.Data?.CustomId;
if (customId != null)
{
var ctx = new InteractionContext(evt, _services);
await _interactionDispatch.Dispatch(customId, ctx);
}
var ctx = new InteractionContext(evt, _services);
await _interactionDispatch.Dispatch(customId, ctx);
}
}
}

View File

@@ -1,6 +1,3 @@
using System;
using System.Threading.Tasks;
using App.Metrics;
using Autofac;
@@ -14,171 +11,174 @@ using Myriad.Types;
using PluralKit.Core;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class MessageCreated: IEventHandler<MessageCreateEvent>
{
public class MessageCreated: IEventHandler<MessageCreateEvent>
private readonly Bot _bot;
private readonly IDiscordCache _cache;
private readonly Cluster _cluster;
private readonly BotConfig _config;
private readonly IDatabase _db;
private readonly LastMessageCacheService _lastMessageCache;
private readonly LoggerCleanService _loggerClean;
private readonly IMetrics _metrics;
private readonly ProxyService _proxy;
private readonly ModelRepository _repo;
private readonly DiscordApiClient _rest;
private readonly ILifetimeScope _services;
private readonly CommandTree _tree;
public MessageCreated(LastMessageCacheService lastMessageCache, LoggerCleanService loggerClean,
IMetrics metrics, ProxyService proxy,
CommandTree tree, ILifetimeScope services, IDatabase db, BotConfig config,
ModelRepository repo, IDiscordCache cache,
Bot bot, Cluster cluster, DiscordApiClient rest)
{
private readonly Bot _bot;
private readonly Cluster _cluster;
private readonly CommandTree _tree;
private readonly IDiscordCache _cache;
private readonly LastMessageCacheService _lastMessageCache;
private readonly LoggerCleanService _loggerClean;
private readonly IMetrics _metrics;
private readonly ProxyService _proxy;
private readonly ILifetimeScope _services;
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly BotConfig _config;
private readonly DiscordApiClient _rest;
_lastMessageCache = lastMessageCache;
_loggerClean = loggerClean;
_metrics = metrics;
_proxy = proxy;
_tree = tree;
_services = services;
_db = db;
_config = config;
_repo = repo;
_cache = cache;
_bot = bot;
_cluster = cluster;
_rest = rest;
}
public MessageCreated(LastMessageCacheService lastMessageCache, LoggerCleanService loggerClean, IMetrics metrics, ProxyService proxy,
CommandTree tree, ILifetimeScope services, IDatabase db, BotConfig config, ModelRepository repo, IDiscordCache cache,
Bot bot, Cluster cluster, DiscordApiClient rest)
// for now, only return error messages for explicit commands
public ulong? ErrorChannelFor(MessageCreateEvent evt)
{
// 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;
}
private bool IsDuplicateMessage(Message msg) =>
// We consider a message duplicate if it has the same ID as the previous message that hit the gateway
_lastMessageCache.GetLastMessage(msg.ChannelId)?.Current.Id == msg.Id;
public async Task Handle(Shard shard, MessageCreateEvent evt)
{
if (evt.Author.Id == shard.User?.Id) return;
if (evt.Type != Message.MessageType.Default && evt.Type != Message.MessageType.Reply) return;
if (IsDuplicateMessage(evt)) return;
var guild = evt.GuildId != null ? await _cache.GetGuild(evt.GuildId.Value) : null;
var channel = await _cache.GetChannel(evt.ChannelId);
var rootChannel = await _cache.GetRootChannel(evt.ChannelId);
// Log metrics and message info
_metrics.Measure.Meter.Mark(BotMetrics.MessagesReceived);
_lastMessageCache.AddMessage(evt);
// Get message context from DB (tracking w/ metrics)
MessageContext ctx;
using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
ctx = await _repo.GetMessageContext(evt.Author.Id, evt.GuildId ?? default, rootChannel.Id);
// Try each handler until we find one that succeeds
if (await TryHandleLogClean(evt, ctx))
return;
// Only do command/proxy handling if it's a user account
if (evt.Author.Bot || evt.WebhookId != null || evt.Author.System == true)
return;
if (await TryHandleCommand(shard, evt, guild, channel, ctx))
return;
await TryHandleProxy(shard, evt, guild, channel, ctx);
}
private async ValueTask<bool> TryHandleLogClean(MessageCreateEvent evt, MessageContext ctx)
{
var channel = await _cache.GetChannel(evt.ChannelId);
if (!evt.Author.Bot || channel.Type != Channel.ChannelType.GuildText ||
!ctx.LogCleanupEnabled) return false;
await _loggerClean.HandleLoggerBotCleanup(evt);
return true;
}
private async ValueTask<bool> TryHandleCommand(Shard shard, MessageCreateEvent evt, Guild? guild,
Channel channel, MessageContext ctx)
{
var content = evt.Content;
if (content == null) return false;
// Check for command prefix
if (!HasCommandPrefix(content, shard.User?.Id ?? default, out var cmdStart) || cmdStart == content.Length)
return false;
// Trim leading whitespace from command without actually modifying the string
// This just moves the argPos pointer by however much whitespace is at the start of the post-argPos string
var trimStartLengthDiff =
content.Substring(cmdStart).Length - content.Substring(cmdStart).TrimStart().Length;
cmdStart += trimStartLengthDiff;
try
{
_lastMessageCache = lastMessageCache;
_loggerClean = loggerClean;
_metrics = metrics;
_proxy = proxy;
_tree = tree;
_services = services;
_db = db;
_config = config;
_repo = repo;
_cache = cache;
_bot = bot;
_cluster = cluster;
_rest = rest;
var system = ctx.SystemId != null ? await _repo.GetSystem(ctx.SystemId.Value) : null;
await _tree.ExecuteCommand(new Context(_services, shard, guild, channel, evt, cmdStart, system, ctx));
}
catch (PKError)
{
// Only permission errors will ever bubble this far and be caught here instead of Context.Execute
// so we just catch and ignore these. TODO: this may need to change.
}
// for now, only return error messages for explicit commands
public ulong? ErrorChannelFor(MessageCreateEvent evt)
return true;
}
private bool HasCommandPrefix(string message, ulong currentUserId, out int argPos)
{
// First, try prefixes defined in the config
var prefixes = _config.Prefixes ?? BotConfig.DefaultPrefixes;
foreach (var prefix in prefixes)
{
// 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;
if (!message.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase)) continue;
return evt.ChannelId;
}
private bool IsDuplicateMessage(Message msg) =>
// We consider a message duplicate if it has the same ID as the previous message that hit the gateway
_lastMessageCache.GetLastMessage(msg.ChannelId)?.Current.Id == msg.Id;
public async Task Handle(Shard shard, MessageCreateEvent evt)
{
if (evt.Author.Id == shard.User?.Id) return;
if (evt.Type != Message.MessageType.Default && evt.Type != Message.MessageType.Reply) return;
if (IsDuplicateMessage(evt)) return;
var guild = evt.GuildId != null ? await _cache.GetGuild(evt.GuildId.Value) : null;
var channel = await _cache.GetChannel(evt.ChannelId);
var rootChannel = await _cache.GetRootChannel(evt.ChannelId);
// Log metrics and message info
_metrics.Measure.Meter.Mark(BotMetrics.MessagesReceived);
_lastMessageCache.AddMessage(evt);
// Get message context from DB (tracking w/ metrics)
MessageContext ctx;
using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
ctx = await _repo.GetMessageContext(evt.Author.Id, evt.GuildId ?? default, rootChannel.Id);
// Try each handler until we find one that succeeds
if (await TryHandleLogClean(evt, ctx))
return;
// Only do command/proxy handling if it's a user account
if (evt.Author.Bot || evt.WebhookId != null || evt.Author.System == true)
return;
if (await TryHandleCommand(shard, evt, guild, channel, ctx))
return;
await TryHandleProxy(shard, evt, guild, channel, ctx);
}
private async ValueTask<bool> TryHandleLogClean(MessageCreateEvent evt, MessageContext ctx)
{
var channel = await _cache.GetChannel(evt.ChannelId);
if (!evt.Author.Bot || channel.Type != Channel.ChannelType.GuildText ||
!ctx.LogCleanupEnabled) return false;
await _loggerClean.HandleLoggerBotCleanup(evt);
argPos = prefix.Length;
return true;
}
private async ValueTask<bool> TryHandleCommand(Shard shard, MessageCreateEvent evt, Guild? guild, Channel channel, MessageContext ctx)
// Then, check mention prefix (must be the bot user, ofc)
argPos = -1;
if (DiscordUtils.HasMentionPrefix(message, ref argPos, out var id))
return id == currentUserId;
return false;
}
private async ValueTask<bool> TryHandleProxy(Shard shard, MessageCreateEvent evt, Guild guild, Channel channel,
MessageContext ctx)
{
var botPermissions = await _cache.PermissionsIn(channel.Id);
try
{
var content = evt.Content;
if (content == null) return false;
// Check for command prefix
if (!HasCommandPrefix(content, shard.User?.Id ?? default, out var cmdStart) || cmdStart == content.Length)
return false;
// Trim leading whitespace from command without actually modifying the string
// This just moves the argPos pointer by however much whitespace is at the start of the post-argPos string
var trimStartLengthDiff = content.Substring(cmdStart).Length - content.Substring(cmdStart).TrimStart().Length;
cmdStart += trimStartLengthDiff;
try
{
var system = ctx.SystemId != null ? await _repo.GetSystem(ctx.SystemId.Value) : null;
await _tree.ExecuteCommand(new Context(_services, shard, guild, channel, evt, cmdStart, system, ctx));
}
catch (PKError)
{
// Only permission errors will ever bubble this far and be caught here instead of Context.Execute
// so we just catch and ignore these. TODO: this may need to change.
}
return true;
return await _proxy.HandleIncomingMessage(shard, evt, ctx, guild, channel, ctx.AllowAutoproxy,
botPermissions);
}
private bool HasCommandPrefix(string message, ulong currentUserId, out int argPos)
// Catch any failed proxy checks so they get ignored in the global error handler
catch (ProxyService.ProxyChecksFailedException) { }
catch (PKError e)
{
// First, try prefixes defined in the config
var prefixes = _config.Prefixes ?? BotConfig.DefaultPrefixes;
foreach (var prefix in prefixes)
{
if (!message.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase)) continue;
argPos = prefix.Length;
return true;
}
// Then, check mention prefix (must be the bot user, ofc)
argPos = -1;
if (DiscordUtils.HasMentionPrefix(message, ref argPos, out var id))
return id == currentUserId;
return false;
// User-facing errors, print to the channel properly formatted
if (botPermissions.HasFlag(PermissionSet.SendMessages))
await _rest.CreateMessage(evt.ChannelId,
new MessageRequest { Content = $"{Emojis.Error} {e.Message}" });
}
private async ValueTask<bool> TryHandleProxy(Shard shard, MessageCreateEvent evt, Guild guild, Channel channel, MessageContext ctx)
{
var botPermissions = await _cache.PermissionsIn(channel.Id);
try
{
return await _proxy.HandleIncomingMessage(shard, evt, ctx, guild, channel, allowAutoproxy: ctx.AllowAutoproxy, botPermissions);
}
// Catch any failed proxy checks so they get ignored in the global error handler
catch (ProxyService.ProxyChecksFailedException) { }
catch (PKError e)
{
// User-facing errors, print to the channel properly formatted
if (botPermissions.HasFlag(PermissionSet.SendMessages))
{
await _rest.CreateMessage(evt.ChannelId,
new MessageRequest { Content = $"{Emojis.Error} {e.Message}" });
}
}
return false;
}
return false;
}
}

View File

@@ -1,67 +1,63 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Myriad.Gateway;
using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot
namespace PluralKit.Bot;
// Double duty :)
public class MessageDeleted: IEventHandler<MessageDeleteEvent>, IEventHandler<MessageDeleteBulkEvent>
{
// Double duty :)
public class MessageDeleted: IEventHandler<MessageDeleteEvent>, IEventHandler<MessageDeleteBulkEvent>
private static readonly TimeSpan MessageDeleteDelay = TimeSpan.FromSeconds(15);
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly ILogger _logger;
private readonly LastMessageCacheService _lastMessage;
public MessageDeleted(ILogger logger, IDatabase db, ModelRepository repo, LastMessageCacheService lastMessage)
{
private static readonly TimeSpan MessageDeleteDelay = TimeSpan.FromSeconds(15);
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly ILogger _logger;
private readonly LastMessageCacheService _lastMessage;
public MessageDeleted(ILogger logger, IDatabase db, ModelRepository repo, LastMessageCacheService lastMessage)
{
_db = db;
_repo = repo;
_lastMessage = lastMessage;
_logger = logger.ForContext<MessageDeleted>();
}
public Task Handle(Shard shard, MessageDeleteEvent evt)
{
// Delete deleted webhook messages from the data store
// Most of the data in the given message is wrong/missing, so always delete just to be sure.
async Task Inner()
{
await Task.Delay(MessageDeleteDelay);
await _repo.DeleteMessage(evt.Id);
}
_lastMessage.HandleMessageDeletion(evt.ChannelId, evt.Id);
// Fork a task to delete the message after a short delay
// to allow for lookups to happen for a little while after deletion
_ = Inner();
return Task.CompletedTask;
}
public Task Handle(Shard shard, MessageDeleteBulkEvent evt)
{
// Same as above, but bulk
async Task Inner()
{
await Task.Delay(MessageDeleteDelay);
_logger.Information("Bulk deleting {Count} messages in channel {Channel}",
evt.Ids.Length, evt.ChannelId);
await _repo.DeleteMessagesBulk(evt.Ids);
}
_lastMessage.HandleMessageDeletion(evt.ChannelId, evt.Ids.ToList());
_ = Inner();
return Task.CompletedTask;
}
_db = db;
_repo = repo;
_lastMessage = lastMessage;
_logger = logger.ForContext<MessageDeleted>();
}
public Task Handle(Shard shard, MessageDeleteEvent evt)
{
// Delete deleted webhook messages from the data store
// Most of the data in the given message is wrong/missing, so always delete just to be sure.
async Task Inner()
{
await Task.Delay(MessageDeleteDelay);
await _repo.DeleteMessage(evt.Id);
}
_lastMessage.HandleMessageDeletion(evt.ChannelId, evt.Id);
// Fork a task to delete the message after a short delay
// to allow for lookups to happen for a little while after deletion
_ = Inner();
return Task.CompletedTask;
}
public Task Handle(Shard shard, MessageDeleteBulkEvent evt)
{
// Same as above, but bulk
async Task Inner()
{
await Task.Delay(MessageDeleteDelay);
_logger.Information("Bulk deleting {Count} messages in channel {Channel}",
evt.Ids.Length, evt.ChannelId);
await _repo.DeleteMessagesBulk(evt.Ids);
}
_lastMessage.HandleMessageDeletion(evt.ChannelId, evt.Ids.ToList());
_ = Inner();
return Task.CompletedTask;
}
}

View File

@@ -1,6 +1,3 @@
using System;
using System.Threading.Tasks;
using App.Metrics;
using Myriad.Cache;
@@ -13,114 +10,117 @@ using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot;
namespace PluralKit.Bot
public class MessageEdited: IEventHandler<MessageUpdateEvent>
{
public class MessageEdited: IEventHandler<MessageUpdateEvent>
private readonly Bot _bot;
private readonly IDiscordCache _cache;
private readonly Cluster _client;
private readonly IDatabase _db;
private readonly LastMessageCacheService _lastMessageCache;
private readonly ILogger _logger;
private readonly IMetrics _metrics;
private readonly ProxyService _proxy;
private readonly ModelRepository _repo;
private readonly DiscordApiClient _rest;
public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db,
IMetrics metrics, ModelRepository repo, Cluster client, IDiscordCache cache, Bot bot,
DiscordApiClient rest, ILogger logger)
{
private readonly LastMessageCacheService _lastMessageCache;
private readonly ProxyService _proxy;
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly IMetrics _metrics;
private readonly Cluster _client;
private readonly IDiscordCache _cache;
private readonly Bot _bot;
private readonly DiscordApiClient _rest;
private readonly ILogger _logger;
_lastMessageCache = lastMessageCache;
_proxy = proxy;
_db = db;
_metrics = metrics;
_repo = repo;
_client = client;
_cache = cache;
_bot = bot;
_rest = rest;
_logger = logger.ForContext<MessageEdited>();
}
public MessageEdited(LastMessageCacheService lastMessageCache, ProxyService proxy, IDatabase db, IMetrics metrics, ModelRepository repo, Cluster client, IDiscordCache cache, Bot bot, DiscordApiClient rest, ILogger logger)
public async Task Handle(Shard shard, MessageUpdateEvent evt)
{
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)
return;
var channel = await _cache.GetChannel(evt.ChannelId);
if (!DiscordUtils.IsValidGuildChannel(channel))
return;
var guild = await _cache.GetGuild(channel.GuildId!.Value);
var lastMessage = _lastMessageCache.GetLastMessage(evt.ChannelId)?.Current;
// Only react to the last message in the channel
if (lastMessage?.Id != evt.Id)
return;
// Just run the normal message handling code, with a flag to disable autoproxying
MessageContext ctx;
using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
ctx = await _repo.GetMessageContext(evt.Author.Value!.Id, channel.GuildId!.Value, evt.ChannelId);
var equivalentEvt = await GetMessageCreateEvent(evt, lastMessage, channel);
var botPermissions = await _cache.PermissionsIn(channel.Id);
try
{
_lastMessageCache = lastMessageCache;
_proxy = proxy;
_db = db;
_metrics = metrics;
_repo = repo;
_client = client;
_cache = cache;
_bot = bot;
_rest = rest;
_logger = logger.ForContext<MessageEdited>();
await _proxy.HandleIncomingMessage(shard, equivalentEvt, ctx, allowAutoproxy: false, guild: guild,
channel: channel, botPermissions: botPermissions);
}
// Catch any failed proxy checks so they get ignored in the global error handler
catch (ProxyService.ProxyChecksFailedException) { }
}
private async Task<MessageCreateEvent> GetMessageCreateEvent(MessageUpdateEvent evt, CachedMessage lastMessage,
Channel channel)
{
var referencedMessage = await GetReferencedMessage(evt.ChannelId, lastMessage.ReferencedMessage);
var messageReference = lastMessage.ReferencedMessage != null
? new Message.Reference(channel.GuildId, evt.ChannelId, lastMessage.ReferencedMessage.Value)
: null;
var messageType = lastMessage.ReferencedMessage != null
? Message.MessageType.Reply
: Message.MessageType.Default;
// TODO: is this missing anything?
var equivalentEvt = new MessageCreateEvent
{
Id = evt.Id,
ChannelId = evt.ChannelId,
GuildId = channel.GuildId,
Author = evt.Author.Value,
Member = evt.Member.Value,
Content = evt.Content.Value,
Attachments = evt.Attachments.Value ?? Array.Empty<Message.Attachment>(),
MessageReference = messageReference,
ReferencedMessage = referencedMessage,
Type = messageType,
};
return equivalentEvt;
}
private async Task<Message?> GetReferencedMessage(ulong channelId, ulong? referencedMessageId)
{
if (referencedMessageId == null)
return null;
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",
channelId
);
return null;
}
public async Task Handle(Shard shard, MessageUpdateEvent evt)
{
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)
return;
var channel = await _cache.GetChannel(evt.ChannelId);
if (!DiscordUtils.IsValidGuildChannel(channel))
return;
var guild = await _cache.GetGuild(channel.GuildId!.Value);
var lastMessage = _lastMessageCache.GetLastMessage(evt.ChannelId)?.Current;
// Only react to the last message in the channel
if (lastMessage?.Id != evt.Id)
return;
// Just run the normal message handling code, with a flag to disable autoproxying
MessageContext ctx;
using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
ctx = await _repo.GetMessageContext(evt.Author.Value!.Id, channel.GuildId!.Value, evt.ChannelId);
var equivalentEvt = await GetMessageCreateEvent(evt, lastMessage, channel);
var botPermissions = await _cache.PermissionsIn(channel.Id);
try
{
await _proxy.HandleIncomingMessage(shard, equivalentEvt, ctx, allowAutoproxy: false, guild: guild,
channel: channel, botPermissions: botPermissions);
}
// Catch any failed proxy checks so they get ignored in the global error handler
catch (ProxyService.ProxyChecksFailedException) { }
}
private async Task<MessageCreateEvent> GetMessageCreateEvent(MessageUpdateEvent evt, CachedMessage lastMessage, Channel channel)
{
var referencedMessage = await GetReferencedMessage(evt.ChannelId, lastMessage.ReferencedMessage);
var messageReference = lastMessage.ReferencedMessage != null
? new Message.Reference(channel.GuildId, evt.ChannelId, lastMessage.ReferencedMessage.Value)
: null;
var messageType = lastMessage.ReferencedMessage != null
? Message.MessageType.Reply
: Message.MessageType.Default;
// TODO: is this missing anything?
var equivalentEvt = new MessageCreateEvent
{
Id = evt.Id,
ChannelId = evt.ChannelId,
GuildId = channel.GuildId,
Author = evt.Author.Value,
Member = evt.Member.Value,
Content = evt.Content.Value,
Attachments = evt.Attachments.Value ?? Array.Empty<Message.Attachment>(),
MessageReference = messageReference,
ReferencedMessage = referencedMessage,
Type = messageType,
};
return equivalentEvt;
}
private async Task<Message?> GetReferencedMessage(ulong channelId, ulong? referencedMessageId)
{
if (referencedMessageId == null)
return null;
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",
channelId);
return null;
}
return await _rest.GetMessage(channelId, referencedMessageId.Value);
}
return await _rest.GetMessage(channelId, referencedMessageId.Value);
}
}

View File

@@ -1,6 +1,3 @@
using System.Threading.Tasks;
using Myriad.Builders;
using Myriad.Cache;
using Myriad.Extensions;
using Myriad.Gateway;
@@ -14,234 +11,242 @@ using PluralKit.Core;
using Serilog;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class ReactionAdded: IEventHandler<MessageReactionAddEvent>
{
public class ReactionAdded: IEventHandler<MessageReactionAddEvent>
private readonly Bot _bot;
private readonly IDiscordCache _cache;
private readonly Cluster _cluster;
private readonly CommandMessageService _commandMessageService;
private readonly IDatabase _db;
private readonly EmbedService _embeds;
private readonly ILogger _logger;
private readonly ModelRepository _repo;
private readonly DiscordApiClient _rest;
public ReactionAdded(ILogger logger, IDatabase db, ModelRepository repo,
CommandMessageService commandMessageService, IDiscordCache cache, Bot bot, Cluster cluster,
DiscordApiClient rest, EmbedService embeds)
{
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly CommandMessageService _commandMessageService;
private readonly ILogger _logger;
private readonly IDiscordCache _cache;
private readonly EmbedService _embeds;
private readonly Bot _bot;
private readonly Cluster _cluster;
private readonly DiscordApiClient _rest;
_db = db;
_repo = repo;
_commandMessageService = commandMessageService;
_cache = cache;
_bot = bot;
_cluster = cluster;
_rest = rest;
_embeds = embeds;
_logger = logger.ForContext<ReactionAdded>();
}
public ReactionAdded(ILogger logger, IDatabase db, ModelRepository repo, CommandMessageService commandMessageService, IDiscordCache cache, Bot bot, Cluster cluster, DiscordApiClient rest, EmbedService embeds)
public async Task Handle(Shard shard, MessageReactionAddEvent evt)
{
await TryHandleProxyMessageReactions(evt);
}
private async ValueTask TryHandleProxyMessageReactions(MessageReactionAddEvent evt)
{
// Sometimes we get events from users that aren't in the user cache
// We just ignore all of those for now, should be quite rare...
if (!(await _cache.TryGetUser(evt.UserId) is User user))
return;
// ignore any reactions added by *us*
if (evt.UserId == await _cache.GetOwnUser())
return;
// Ignore reactions from bots (we can't DM them anyway)
if (user.Bot) return;
var channel = await _cache.GetChannel(evt.ChannelId);
// check if it's a command message first
// since this can happen in DMs as well
if (evt.Emoji.Name == "\u274c")
{
_db = db;
_repo = repo;
_commandMessageService = commandMessageService;
_cache = cache;
_bot = bot;
_cluster = cluster;
_rest = rest;
_embeds = embeds;
_logger = logger.ForContext<ReactionAdded>();
}
public async Task Handle(Shard shard, MessageReactionAddEvent evt)
{
await TryHandleProxyMessageReactions(evt);
}
private async ValueTask TryHandleProxyMessageReactions(MessageReactionAddEvent evt)
{
// Sometimes we get events from users that aren't in the user cache
// We just ignore all of those for now, should be quite rare...
if (!(await _cache.TryGetUser(evt.UserId) is User user))
return;
// ignore any reactions added by *us*
if (evt.UserId == await _cache.GetOwnUser())
return;
// Ignore reactions from bots (we can't DM them anyway)
if (user.Bot) return;
var channel = await _cache.GetChannel(evt.ChannelId);
// check if it's a command message first
// since this can happen in DMs as well
if (evt.Emoji.Name == "\u274c")
// in DMs, allow deleting any PK message
if (channel.GuildId == null)
{
// in DMs, allow deleting any PK message
if (channel.GuildId == null)
await HandleCommandDeleteReaction(evt, null);
return;
}
var commandMsg = await _commandMessageService.GetCommandMessage(evt.MessageId);
if (commandMsg != null)
{
await HandleCommandDeleteReaction(evt, commandMsg);
return;
}
}
// Proxied messages only exist in guild text channels, so skip checking if we're elsewhere
if (!DiscordUtils.IsValidGuildChannel(channel)) return;
switch (evt.Emoji.Name)
{
// Message deletion
case "\u274C": // Red X
{
await HandleCommandDeleteReaction(evt, null);
return;
var msg = await _db.Execute(c => _repo.GetMessage(c, evt.MessageId));
if (msg != null)
await HandleProxyDeleteReaction(evt, msg);
break;
}
case "\u2753": // Red question mark
case "\u2754": // White question mark
{
var msg = await _db.Execute(c => _repo.GetMessage(c, evt.MessageId));
if (msg != null)
await HandleQueryReaction(evt, msg);
break;
}
var commandMsg = await _commandMessageService.GetCommandMessage(evt.MessageId);
if (commandMsg != null)
case "\U0001F514": // Bell
case "\U0001F6CE": // Bellhop bell
case "\U0001F3D3": // Ping pong paddle (lol)
case "\u23F0": // Alarm clock
case "\u2757": // Exclamation mark
{
await HandleCommandDeleteReaction(evt, commandMsg);
return;
var msg = await _db.Execute(c => _repo.GetMessage(c, evt.MessageId));
if (msg != null)
await HandlePingReaction(evt, msg);
break;
}
}
}
}
// Proxied messages only exist in guild text channels, so skip checking if we're elsewhere
if (!DiscordUtils.IsValidGuildChannel(channel)) return;
private async ValueTask HandleProxyDeleteReaction(MessageReactionAddEvent evt, FullMessage msg)
{
if (!(await _cache.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
return;
switch (evt.Emoji.Name)
{
// Message deletion
case "\u274C": // Red X
{
var msg = await _db.Execute(c => _repo.GetMessage(c, evt.MessageId));
if (msg != null)
await HandleProxyDeleteReaction(evt, msg);
var system = await _repo.GetSystemByAccount(evt.UserId);
break;
}
case "\u2753": // Red question mark
case "\u2754": // White question mark
{
var msg = await _db.Execute(c => _repo.GetMessage(c, evt.MessageId));
if (msg != null)
await HandleQueryReaction(evt, msg);
// Can only delete your own message
if (msg.System.Id != system?.Id) return;
break;
}
case "\U0001F514": // Bell
case "\U0001F6CE": // Bellhop bell
case "\U0001F3D3": // Ping pong paddle (lol)
case "\u23F0": // Alarm clock
case "\u2757": // Exclamation mark
{
var msg = await _db.Execute(c => _repo.GetMessage(c, evt.MessageId));
if (msg != null)
await HandlePingReaction(evt, msg);
break;
}
}
try
{
await _rest.DeleteMessage(evt.ChannelId, evt.MessageId);
}
catch (NotFoundException)
{
// Message was deleted by something/someone else before we got to it
}
private async ValueTask HandleProxyDeleteReaction(MessageReactionAddEvent evt, FullMessage msg)
await _repo.DeleteMessage(evt.MessageId);
}
private async ValueTask HandleCommandDeleteReaction(MessageReactionAddEvent evt, CommandMessage? msg)
{
// Can only delete your own message
// (except in DMs, where msg will be null)
// todo: don't try to delete the user's messages
if (msg != null && msg.AuthorId != evt.UserId)
return;
try
{
if (!(await _cache.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
return;
var system = await _repo.GetSystemByAccount(evt.UserId);
// Can only delete your own message
if (msg.System.Id != system?.Id) return;
try
{
await _rest.DeleteMessage(evt.ChannelId, evt.MessageId);
}
catch (NotFoundException)
{
// Message was deleted by something/someone else before we got to it
}
await _repo.DeleteMessage(evt.MessageId);
await _rest.DeleteMessage(evt.ChannelId, evt.MessageId);
}
catch (NotFoundException)
{
// Message was deleted by something/someone else before we got to it
}
private async ValueTask HandleCommandDeleteReaction(MessageReactionAddEvent evt, CommandMessage? msg)
// No need to delete database row here, it'll get deleted by the once-per-minute scheduled task.
}
private async ValueTask HandleQueryReaction(MessageReactionAddEvent evt, FullMessage msg)
{
var guild = await _cache.GetGuild(evt.GuildId!.Value);
// Try to DM the user info about the message
try
{
// Can only delete your own message
// (except in DMs, where msg will be null)
if (msg != null && msg.AuthorId != evt.UserId)
return;
try
var dm = await _cache.GetOrCreateDmChannel(_rest, evt.UserId);
await _rest.CreateMessage(dm.Id, new MessageRequest
{
await _rest.DeleteMessage(evt.ChannelId, evt.MessageId);
}
catch (NotFoundException)
{
// Message was deleted by something/someone else before we got to it
}
Embed = await _embeds.CreateMemberEmbed(
msg.System,
msg.Member,
guild,
LookupContext.ByNonOwner
)
});
// No need to delete database row here, it'll get deleted by the once-per-minute scheduled task.
await _rest.CreateMessage(
dm.Id,
new MessageRequest { Embed = await _embeds.CreateMessageInfoEmbed(msg, true) }
);
}
catch (ForbiddenException) { } // No permissions to DM, can't check for this :(
private async ValueTask HandleQueryReaction(MessageReactionAddEvent evt, FullMessage msg)
{
var guild = await _cache.GetGuild(evt.GuildId!.Value);
await TryRemoveOriginalReaction(evt);
}
// Try to DM the user info about the message
private async ValueTask HandlePingReaction(MessageReactionAddEvent evt, FullMessage msg)
{
if (!(await _cache.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
return;
// Check if the "pinger" has permission to send messages in this channel
// (if not, PK shouldn't send messages on their behalf)
var member = await _rest.GetGuildMember(evt.GuildId!.Value, evt.UserId);
var requiredPerms = PermissionSet.ViewChannel | PermissionSet.SendMessages;
if (member == null || !(await _cache.PermissionsFor(evt.ChannelId, member)).HasFlag(requiredPerms)) return;
if (msg.System.PingsEnabled)
// If the system has pings enabled, go ahead
await _rest.CreateMessage(evt.ChannelId, new MessageRequest
{
Content = $"Psst, **{msg.Member.DisplayName()}** (<@{msg.Message.Sender}>), you have been pinged by <@{evt.UserId}>.",
Components = new[]
{
new MessageComponent
{
Type = ComponentType.ActionRow,
Components = new[]
{
new MessageComponent
{
Style = ButtonStyle.Link,
Type = ComponentType.Button,
Label = "Jump",
Url = evt.JumpLink()
}
}
}
},
AllowedMentions = new AllowedMentions { Users = new[] { msg.Message.Sender } }
});
else
// If not, tell them in DMs (if we can)
try
{
var dm = await _cache.GetOrCreateDmChannel(_rest, evt.UserId);
await _rest.CreateMessage(dm.Id, new MessageRequest
{
Embed = await _embeds.CreateMemberEmbed(msg.System, msg.Member, guild, LookupContext.ByNonOwner)
});
await _rest.CreateMessage(dm.Id, new MessageRequest
{
Embed = await _embeds.CreateMessageInfoEmbed(msg, true)
});
}
catch (ForbiddenException) { } // No permissions to DM, can't check for this :(
await TryRemoveOriginalReaction(evt);
}
private async ValueTask HandlePingReaction(MessageReactionAddEvent evt, FullMessage msg)
{
if (!(await _cache.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
return;
// Check if the "pinger" has permission to send messages in this channel
// (if not, PK shouldn't send messages on their behalf)
var member = await _rest.GetGuildMember(evt.GuildId!.Value, evt.UserId);
var requiredPerms = PermissionSet.ViewChannel | PermissionSet.SendMessages;
if (member == null || !(await _cache.PermissionsFor(evt.ChannelId, member)).HasFlag(requiredPerms)) return;
if (msg.System.PingsEnabled)
{
// If the system has pings enabled, go ahead
await _rest.CreateMessage(evt.ChannelId, new()
{
Content = $"Psst, **{msg.Member.DisplayName()}** (<@{msg.Message.Sender}>), you have been pinged by <@{evt.UserId}>.",
Components = new[]
await _rest.CreateMessage(dm.Id,
new MessageRequest
{
new MessageComponent
{
Type = ComponentType.ActionRow,
Components = new[]
{
new MessageComponent
{
Style = ButtonStyle.Link,
Type = ComponentType.Button,
Label = "Jump",
Url = evt.JumpLink()
}
}
}
},
AllowedMentions = new AllowedMentions { Users = new[] { msg.Message.Sender } }
});
}
else
{
// If not, tell them in DMs (if we can)
try
{
var dm = await _cache.GetOrCreateDmChannel(_rest, evt.UserId);
await _rest.CreateMessage(dm.Id, new MessageRequest
{
Content = $"{Emojis.Error} {msg.Member.DisplayName()}'s system has disabled reaction pings. If you want to mention them anyway, you can copy/paste the following message:"
Content =
$"{Emojis.Error} {msg.Member.DisplayName()}'s system has disabled reaction pings. If you want to mention them anyway, you can copy/paste the following message:"
});
await _rest.CreateMessage(dm.Id, new MessageRequest { Content = $"<@{msg.Message.Sender}>".AsCode() });
}
catch (ForbiddenException) { }
await _rest.CreateMessage(
dm.Id,
new MessageRequest { Content = $"<@{msg.Message.Sender}>".AsCode() }
);
}
catch (ForbiddenException) { }
await TryRemoveOriginalReaction(evt);
}
await TryRemoveOriginalReaction(evt);
}
private async Task TryRemoveOriginalReaction(MessageReactionAddEvent evt)
{
if ((await _cache.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
await _rest.DeleteUserReaction(evt.ChannelId, evt.MessageId, evt.Emoji, evt.UserId);
}
private async Task TryRemoveOriginalReaction(MessageReactionAddEvent evt)
{
if ((await _cache.PermissionsIn(evt.ChannelId)).HasFlag(PermissionSet.ManageMessages))
await _rest.DeleteUserReaction(evt.ChannelId, evt.MessageId, evt.Emoji, evt.UserId);
}
}