2020-05-01 23:52:52 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
2020-12-22 12:15:26 +00:00
|
|
|
public class MessageCreated: IEventHandler<MessageCreateEvent>
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-12-22 12:15:26 +00:00
|
|
|
private readonly Bot _bot;
|
2020-05-01 23:52:52 +00:00
|
|
|
private readonly CommandTree _tree;
|
2020-12-22 12:15:26 +00:00
|
|
|
private readonly IDiscordCache _cache;
|
2020-05-01 23:52:52 +00:00
|
|
|
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-29 11:46:27 +00:00
|
|
|
private readonly ModelRepository _repo;
|
2020-08-25 17:32:19 +00:00
|
|
|
private readonly BotConfig _config;
|
2020-12-22 12:15:26 +00:00
|
|
|
private readonly DiscordApiClient _rest;
|
2020-06-12 21:13:21 +00:00
|
|
|
|
|
|
|
public MessageCreated(LastMessageCacheService lastMessageCache, LoggerCleanService loggerClean,
|
2020-12-22 12:15:26 +00:00
|
|
|
IMetrics metrics, ProxyService proxy,
|
|
|
|
CommandTree tree, ILifetimeScope services, IDatabase db, BotConfig config, ModelRepository repo, IDiscordCache cache, Bot bot, DiscordApiClient rest)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
|
|
|
_lastMessageCache = lastMessageCache;
|
|
|
|
_loggerClean = loggerClean;
|
|
|
|
_metrics = metrics;
|
|
|
|
_proxy = proxy;
|
|
|
|
_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-08-29 11:46:27 +00:00
|
|
|
_repo = repo;
|
2020-12-22 12:15:26 +00:00
|
|
|
_cache = cache;
|
|
|
|
_bot = bot;
|
|
|
|
_rest = rest;
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
public ulong? ErrorChannelFor(MessageCreateEvent evt) => evt.ChannelId;
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
private bool IsDuplicateMessage(Message msg) =>
|
2020-06-12 21:13:21 +00:00
|
|
|
// We consider a message duplicate if it has the same ID as the previous message that hit the gateway
|
2020-12-22 12:15:26 +00:00
|
|
|
_lastMessageCache.GetLastMessage(msg.ChannelId) == msg.Id;
|
2020-06-12 21:13:21 +00:00
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
public async Task Handle(Shard shard, MessageCreateEvent evt)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-12-22 12:15:26 +00:00
|
|
|
if (evt.Author.Id == shard.User?.Id) return;
|
|
|
|
if (evt.Type != Message.MessageType.Default) return;
|
|
|
|
if (IsDuplicateMessage(evt)) return;
|
|
|
|
|
2020-12-22 15:55:13 +00:00
|
|
|
var guild = evt.GuildId != null ? _cache.GetGuild(evt.GuildId.Value) : null;
|
|
|
|
var channel = _cache.GetChannel(evt.ChannelId);
|
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);
|
2020-12-22 12:15:26 +00:00
|
|
|
_lastMessageCache.AddMessage(evt.ChannelId, evt.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))
|
2020-12-22 12:15:26 +00:00
|
|
|
ctx = await _repo.GetMessageContext(conn, evt.Author.Id, evt.GuildId ?? default, evt.ChannelId);
|
|
|
|
|
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
|
2020-12-22 12:15:26 +00:00
|
|
|
if (evt.Author.Bot || evt.WebhookId != null || evt.Author.System == true)
|
2020-06-25 23:49:51 +00:00
|
|
|
return;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
|
|
|
if (await TryHandleCommand(shard, evt, guild, channel, ctx))
|
2020-06-25 23:49:51 +00:00
|
|
|
return;
|
2020-12-22 12:15:26 +00:00
|
|
|
await TryHandleProxy(shard, evt, guild, channel, ctx);
|
2020-06-12 21:13:21 +00:00
|
|
|
}
|
2020-05-01 23:52:52 +00:00
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
private async ValueTask<bool> TryHandleLogClean(MessageCreateEvent evt, MessageContext ctx)
|
2020-06-12 21:13:21 +00:00
|
|
|
{
|
2020-12-22 15:55:13 +00:00
|
|
|
var channel = _cache.GetChannel(evt.ChannelId);
|
|
|
|
if (!evt.Author.Bot || channel.Type != Channel.ChannelType.GuildText ||
|
2020-06-12 21:13:21 +00:00
|
|
|
!ctx.LogCleanupEnabled) return false;
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
await _loggerClean.HandleLoggerBotCleanup(evt);
|
2020-06-12 21:13:21 +00:00
|
|
|
return true;
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
private async ValueTask<bool> TryHandleCommand(Shard shard, MessageCreateEvent evt, Guild? guild, Channel channel, MessageContext ctx)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-12-22 12:15:26 +00:00
|
|
|
var content = evt.Content;
|
2020-06-12 21:13:21 +00:00
|
|
|
if (content == null) return false;
|
|
|
|
|
2020-08-25 17:32:19 +00:00
|
|
|
// Check for command prefix
|
2020-12-22 12:15:26 +00:00
|
|
|
if (!HasCommandPrefix(content, shard.User?.Id ?? default, out var cmdStart))
|
2020-08-25 17:32:19 +00:00
|
|
|
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-08-29 11:46:27 +00:00
|
|
|
var system = ctx.SystemId != null ? await _db.Execute(c => _repo.GetSystem(c, ctx.SystemId.Value)) : null;
|
2020-12-23 01:19:02 +00:00
|
|
|
await _tree.ExecuteCommand(new Context(_services, shard, guild, channel, evt, cmdStart, system, ctx, _bot.PermissionsIn(channel.Id)));
|
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-12-22 12:15:26 +00:00
|
|
|
private bool HasCommandPrefix(string message, ulong currentUserId, out int argPos)
|
2020-08-25 17:32:19 +00:00
|
|
|
{
|
|
|
|
// 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))
|
2020-12-22 12:15:26 +00:00
|
|
|
return id == currentUserId;
|
2020-08-25 17:32:19 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
private async ValueTask<bool> TryHandleProxy(Shard shard, MessageCreateEvent evt, Guild guild, Channel channel, MessageContext ctx)
|
2020-05-01 23:52:52 +00:00
|
|
|
{
|
2020-12-23 01:19:02 +00:00
|
|
|
var botPermissions = _bot.PermissionsIn(channel.Id);
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2020-05-01 23:52:52 +00:00
|
|
|
try
|
|
|
|
{
|
2020-12-22 12:15:26 +00:00
|
|
|
return await _proxy.HandleIncomingMessage(shard, evt, ctx, guild, channel, allowAutoproxy: ctx.AllowAutoproxy, botPermissions);
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
catch (PKError e)
|
|
|
|
{
|
|
|
|
// User-facing errors, print to the channel properly formatted
|
2020-12-22 12:15:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-12 21:13:21 +00:00
|
|
|
return false;
|
2020-05-01 23:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|