2020-05-01 23:52:52 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
using App.Metrics;
|
|
|
|
|
|
|
|
using Autofac;
|
|
|
|
|
|
|
|
using DSharpPlus;
|
|
|
|
using DSharpPlus.Entities;
|
|
|
|
using DSharpPlus.EventArgs;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
public class MessageCreated: IEventHandler<MessageCreateEventArgs>
|
|
|
|
{
|
|
|
|
private readonly CommandTree _tree;
|
|
|
|
private readonly DiscordShardedClient _client;
|
|
|
|
private readonly LastMessageCacheService _lastMessageCache;
|
|
|
|
private readonly LoggerCleanService _loggerClean;
|
|
|
|
private readonly IMetrics _metrics;
|
|
|
|
private readonly ProxyService _proxy;
|
|
|
|
private readonly ILifetimeScope _services;
|
2020-06-13 17:36:43 +00:00
|
|
|
private readonly IDatabase _db;
|
2020-08-25 17:32:19 +00:00
|
|
|
private readonly BotConfig _config;
|
2020-06-12 21:13:21 +00:00
|
|
|
|
|
|
|
public MessageCreated(LastMessageCacheService lastMessageCache, LoggerCleanService loggerClean,
|
|
|
|
IMetrics metrics, ProxyService proxy, DiscordShardedClient client,
|
2020-08-25 17:32:19 +00:00
|
|
|
CommandTree tree, ILifetimeScope services, IDatabase db, BotConfig config)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
|
|
|
_lastMessageCache = lastMessageCache;
|
|
|
|
_loggerClean = loggerClean;
|
|
|
|
_metrics = metrics;
|
|
|
|
_proxy = proxy;
|
|
|
|
_client = client;
|
|
|
|
_tree = tree;
|
|
|
|
_services = services;
|
2020-06-12 21:13:21 +00:00
|
|
|
_db = db;
|
2020-08-25 17:32:19 +00:00
|
|
|
_config = config;
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public DiscordChannel ErrorChannelFor(MessageCreateEventArgs evt) => evt.Channel;
|
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
private bool IsDuplicateMessage(DiscordMessage evt) =>
|
|
|
|
// We consider a message duplicate if it has the same ID as the previous message that hit the gateway
|
|
|
|
_lastMessageCache.GetLastMessage(evt.ChannelId) == evt.Id;
|
|
|
|
|
2020-05-01 23:52:52 +00:00
|
|
|
public async Task Handle(MessageCreateEventArgs evt)
|
|
|
|
{
|
2020-06-12 21:13:21 +00:00
|
|
|
if (evt.Message.MessageType != MessageType.Default) return;
|
|
|
|
if (IsDuplicateMessage(evt.Message)) return;
|
2020-05-06 21:48:45 +00:00
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
// Log metrics and message info
|
|
|
|
_metrics.Measure.Meter.Mark(BotMetrics.MessagesReceived);
|
|
|
|
_lastMessageCache.AddMessage(evt.Channel.Id, evt.Message.Id);
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2020-06-14 20:19:12 +00:00
|
|
|
// Get message context from DB (tracking w/ metrics)
|
|
|
|
MessageContext ctx;
|
|
|
|
await using (var conn = await _db.Obtain())
|
|
|
|
using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
|
|
|
|
ctx = await conn.QueryMessageContext(evt.Author.Id, evt.Channel.GuildId, evt.Channel.Id);
|
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
// Try each handler until we find one that succeeds
|
2020-06-25 23:49:51 +00:00
|
|
|
if (await TryHandleLogClean(evt, ctx))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Only do command/proxy handling if it's a user account
|
|
|
|
if (evt.Message.Author.IsBot || evt.Message.WebhookMessage || evt.Message.Author.IsSystem == true)
|
|
|
|
return;
|
|
|
|
if (await TryHandleCommand(evt, ctx))
|
|
|
|
return;
|
|
|
|
await TryHandleProxy(evt, ctx);
|
2020-06-12 21:13:21 +00:00
|
|
|
}
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
private async ValueTask<bool> TryHandleLogClean(MessageCreateEventArgs evt, MessageContext ctx)
|
|
|
|
{
|
|
|
|
if (!evt.Message.Author.IsBot || evt.Message.Channel.Type != ChannelType.Text ||
|
|
|
|
!ctx.LogCleanupEnabled) return false;
|
|
|
|
|
|
|
|
await _loggerClean.HandleLoggerBotCleanup(evt.Message);
|
|
|
|
return true;
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
private async ValueTask<bool> TryHandleCommand(MessageCreateEventArgs evt, MessageContext ctx)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-06-12 21:13:21 +00:00
|
|
|
var content = evt.Message.Content;
|
|
|
|
if (content == null) return false;
|
|
|
|
|
2020-08-25 17:32:19 +00:00
|
|
|
// Check for command prefix
|
|
|
|
if (!HasCommandPrefix(content, out var cmdStart))
|
|
|
|
return false;
|
2020-06-12 21:13:21 +00:00
|
|
|
|
2020-08-25 17:32:19 +00:00
|
|
|
// Trim leading whitespace from command without actually modifying the string
|
2020-05-01 23:52:52 +00:00
|
|
|
// This just moves the argPos pointer by however much whitespace is at the start of the post-argPos string
|
2020-08-25 17:32:19 +00:00
|
|
|
var trimStartLengthDiff = content.Substring(cmdStart).Length - content.Substring(cmdStart).TrimStart().Length;
|
|
|
|
cmdStart += trimStartLengthDiff;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-06-13 17:42:04 +00:00
|
|
|
var system = ctx.SystemId != null ? await _db.Execute(c => c.QuerySystem(ctx.SystemId.Value)) : null;
|
2020-08-25 17:32:19 +00:00
|
|
|
await _tree.ExecuteCommand(new Context(_services, evt.Client, evt.Message, cmdStart, system, ctx));
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-25 17:32:19 +00:00
|
|
|
private bool HasCommandPrefix(string message, out int argPos)
|
|
|
|
{
|
|
|
|
// 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 == _client.CurrentUser.Id;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
private async ValueTask<bool> TryHandleProxy(MessageCreateEventArgs evt, MessageContext ctx)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-06-12 21:13:21 +00:00
|
|
|
return await _proxy.HandleIncomingMessage(evt.Message, ctx, allowAutoproxy: true);
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
catch (PKError e)
|
|
|
|
{
|
|
|
|
// User-facing errors, print to the channel properly formatted
|
2020-06-12 21:13:21 +00:00
|
|
|
var msg = evt.Message;
|
2020-05-02 13:29:36 +00:00
|
|
|
if (msg.Channel.Guild == null || msg.Channel.BotHasAllPermissions(Permissions.SendMessages))
|
2020-06-20 15:33:10 +00:00
|
|
|
await msg.Channel.SendMessageFixedAsync($"{Emojis.Error} {e.Message}");
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
return false;
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|