2020-05-01 23:52:52 +00:00
|
|
|
using App.Metrics;
|
|
|
|
|
|
|
|
using Autofac;
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
using Myriad.Cache;
|
|
|
|
using Myriad.Extensions;
|
|
|
|
using Myriad.Gateway;
|
|
|
|
using Myriad.Rest;
|
|
|
|
using Myriad.Rest.Types.Requests;
|
|
|
|
using Myriad.Types;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public class MessageCreated: IEventHandler<MessageCreateEvent>
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
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;
|
2022-01-22 07:47:47 +00:00
|
|
|
private readonly PrivateChannelService _dmCache;
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
public MessageCreated(LastMessageCacheService lastMessageCache, LoggerCleanService loggerClean,
|
|
|
|
IMetrics metrics, ProxyService proxy,
|
|
|
|
CommandTree tree, ILifetimeScope services, IDatabase db, BotConfig config,
|
|
|
|
ModelRepository repo, IDiscordCache cache,
|
2022-01-22 07:47:47 +00:00
|
|
|
Bot bot, Cluster cluster, DiscordApiClient rest, PrivateChannelService dmCache)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_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;
|
2022-01-22 07:47:47 +00:00
|
|
|
_dmCache = dmCache;
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2022-03-30 08:36:22 +00:00
|
|
|
public ulong? ErrorChannelFor(MessageCreateEvent evt, ulong userId) => evt.ChannelId;
|
2021-11-27 02:10:56 +00:00
|
|
|
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;
|
|
|
|
|
2022-01-14 23:39:03 +00:00
|
|
|
public async Task Handle(int shardId, MessageCreateEvent evt)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
2022-01-14 23:39:03 +00:00
|
|
|
if (evt.Author.Id == await _cache.GetOwnUser()) return;
|
2021-11-27 02:10:56 +00:00
|
|
|
if (evt.Type != Message.MessageType.Default && evt.Type != Message.MessageType.Reply) return;
|
|
|
|
if (IsDuplicateMessage(evt)) return;
|
|
|
|
|
2022-06-15 23:28:34 +00:00
|
|
|
var botPermissions = await _cache.PermissionsIn(evt.ChannelId);
|
|
|
|
if (!botPermissions.HasFlag(PermissionSet.SendMessages)) return;
|
2022-03-30 09:11:55 +00:00
|
|
|
|
2022-01-22 07:47:47 +00:00
|
|
|
// spawn off saving the private channel into another thread
|
|
|
|
// it is not a fatal error if this fails, and it shouldn't block message processing
|
|
|
|
_ = _dmCache.TrySavePrivateChannel(evt);
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
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);
|
|
|
|
|
2022-06-15 23:28:34 +00:00
|
|
|
// if the message was not sent by an user account, only try running log cleanup
|
|
|
|
if (evt.Author.Bot || evt.WebhookId != null || evt.Author.System == true)
|
|
|
|
{
|
|
|
|
await TryHandleLogClean(channel, evt);
|
|
|
|
return;
|
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
// Try each handler until we find one that succeeds
|
|
|
|
|
2022-06-15 23:28:34 +00:00
|
|
|
if (await TryHandleCommand(shardId, evt, guild, channel))
|
2021-11-27 02:10:56 +00:00
|
|
|
return;
|
|
|
|
|
2022-06-15 23:28:34 +00:00
|
|
|
await TryHandleProxy(evt, guild, channel, rootChannel.Id, botPermissions);
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 23:28:34 +00:00
|
|
|
private async Task TryHandleLogClean(Channel channel, MessageCreateEvent evt)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
2022-06-22 13:34:59 +00:00
|
|
|
if (evt.GuildId == null) return;
|
2022-06-15 23:28:34 +00:00
|
|
|
if (channel.Type != Channel.ChannelType.GuildText) return;
|
2021-11-27 02:10:56 +00:00
|
|
|
|
2022-06-15 23:28:34 +00:00
|
|
|
var guildSettings = await _repo.GetGuild(evt.GuildId!.Value);
|
|
|
|
|
|
|
|
if (guildSettings.LogCleanupEnabled)
|
|
|
|
await _loggerClean.HandleLoggerBotCleanup(evt);
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 23:28:34 +00:00
|
|
|
private async ValueTask<bool> TryHandleCommand(int shardId, MessageCreateEvent evt, Guild? guild, Channel channel)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
|
|
|
var content = evt.Content;
|
|
|
|
if (content == null) return false;
|
|
|
|
|
2022-01-14 23:39:03 +00:00
|
|
|
var ourUserId = await _cache.GetOwnUser();
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Check for command prefix
|
2022-01-14 23:39:03 +00:00
|
|
|
if (!HasCommandPrefix(content, ourUserId, out var cmdStart) || cmdStart == content.Length)
|
2021-11-27 02:10:56 +00:00
|
|
|
return false;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// 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;
|
2020-06-12 21:13:21 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
try
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2022-06-15 23:28:34 +00:00
|
|
|
var system = await _repo.GetSystemByAccount(evt.Author.Id);
|
|
|
|
var config = system != null ? await _repo.GetSystemConfig(system.Id) : null;
|
|
|
|
await _tree.ExecuteCommand(new Context(_services, shardId, guild, channel, evt, cmdStart, system, config));
|
2020-06-12 21:13:21 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
catch (PKError)
|
2020-06-12 21:13:21 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
// 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.
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
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)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!message.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase)) continue;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
argPos = prefix.Length;
|
2020-05-01 23:52:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Then, check mention prefix (must be the bot user, ofc)
|
|
|
|
argPos = -1;
|
|
|
|
if (DiscordUtils.HasMentionPrefix(message, ref argPos, out var id))
|
|
|
|
return id == currentUserId;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2022-06-15 23:28:34 +00:00
|
|
|
private async ValueTask<bool> TryHandleProxy(MessageCreateEvent evt, Guild guild, Channel channel, ulong rootChannel, PermissionSet botPermissions)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
2022-06-15 23:28:34 +00:00
|
|
|
// 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);
|
2020-08-25 17:32:19 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
try
|
|
|
|
{
|
2022-06-13 18:52:07 +00:00
|
|
|
return await _proxy.HandleIncomingMessage(evt, ctx, guild, channel, true, botPermissions);
|
2020-08-25 17:32:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Catch any failed proxy checks so they get ignored in the global error handler
|
|
|
|
catch (ProxyService.ProxyChecksFailedException) { }
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
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}" });
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
return false;
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
}
|