2021-08-27 15:03:47 +00:00
|
|
|
#nullable enable
|
2021-10-29 20:42:10 +00:00
|
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
2021-05-03 10:33:30 +00:00
|
|
|
|
2021-08-25 18:36:13 +00:00
|
|
|
using Myriad.Builders;
|
2021-07-28 06:23:01 +00:00
|
|
|
using Myriad.Cache;
|
|
|
|
using Myriad.Extensions;
|
2021-05-03 10:33:30 +00:00
|
|
|
using Myriad.Rest;
|
2021-11-27 02:10:56 +00:00
|
|
|
using Myriad.Rest.Exceptions;
|
2021-10-29 20:42:10 +00:00
|
|
|
using Myriad.Rest.Types;
|
|
|
|
using Myriad.Rest.Types.Requests;
|
2021-05-03 10:33:30 +00:00
|
|
|
using Myriad.Types;
|
|
|
|
|
|
|
|
using NodaTime;
|
|
|
|
|
2022-05-08 21:10:07 +00:00
|
|
|
using App.Metrics;
|
|
|
|
|
2021-05-03 10:33:30 +00:00
|
|
|
using PluralKit.Core;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public class ProxiedMessage
|
2021-05-03 10:33:30 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private static readonly Duration EditTimeout = Duration.FromMinutes(10);
|
2022-05-08 21:10:07 +00:00
|
|
|
private static readonly Duration ReproxyTimeout = Duration.FromMinutes(1);
|
2022-03-30 08:36:22 +00:00
|
|
|
|
2022-03-10 01:06:53 +00:00
|
|
|
// private readonly IDiscordCache _cache;
|
2022-05-08 21:10:07 +00:00
|
|
|
private readonly ModelRepository _repo;
|
|
|
|
private readonly IMetrics _metrics;
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
private readonly EmbedService _embeds;
|
|
|
|
private readonly LogChannelService _logChannel;
|
|
|
|
private readonly DiscordApiClient _rest;
|
|
|
|
private readonly WebhookExecutorService _webhookExecutor;
|
2022-05-08 21:10:07 +00:00
|
|
|
private readonly ProxyService _proxy;
|
2021-11-27 02:10:56 +00:00
|
|
|
|
2022-05-08 21:10:07 +00:00
|
|
|
public ProxiedMessage(EmbedService embeds,
|
|
|
|
DiscordApiClient rest, IMetrics metrics, ModelRepository repo, ProxyService proxy,
|
2021-11-27 02:10:56 +00:00
|
|
|
WebhookExecutorService webhookExecutor, LogChannelService logChannel, IDiscordCache cache)
|
2021-05-03 10:33:30 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_embeds = embeds;
|
|
|
|
_rest = rest;
|
|
|
|
_webhookExecutor = webhookExecutor;
|
2022-05-08 21:10:07 +00:00
|
|
|
_repo = repo;
|
2021-11-27 02:10:56 +00:00
|
|
|
_logChannel = logChannel;
|
2022-03-10 01:06:53 +00:00
|
|
|
// _cache = cache;
|
2022-05-08 21:10:07 +00:00
|
|
|
_metrics = metrics;
|
|
|
|
_proxy = proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task ReproxyMessage(Context ctx)
|
|
|
|
{
|
|
|
|
var msg = await GetMessageToEdit(ctx, ReproxyTimeout, true);
|
|
|
|
|
|
|
|
if (ctx.System.Id != msg.System?.Id)
|
|
|
|
throw new PKError("Can't reproxy a message sent by a different system.");
|
|
|
|
|
|
|
|
// Get target member ID
|
|
|
|
var target = await ctx.MatchMember(restrictToSystem: ctx.System.Id);
|
|
|
|
if (target == null)
|
|
|
|
throw new PKError("Could not find a member to reproxy the message with.");
|
|
|
|
|
|
|
|
// Fetch members and get the ProxyMember for `target`
|
|
|
|
List <ProxyMember> members;
|
|
|
|
using (_metrics.Measure.Timer.Time(BotMetrics.ProxyMembersQueryTime))
|
|
|
|
members = (await _repo.GetProxyMembers(ctx.Author.Id, msg.Message.Guild!.Value)).ToList();
|
|
|
|
var match = members.Find(x => x.Id == target.Id);
|
|
|
|
if (match == null)
|
|
|
|
throw new PKError("Could not find a member to reproxy the message with.");
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await _proxy.ExecuteReproxy(ctx.Message, msg.Message, match);
|
|
|
|
|
|
|
|
if (ctx.Guild == null)
|
|
|
|
await _rest.CreateReaction(ctx.Channel.Id, ctx.Message.Id, new Emoji { Name = Emojis.Success });
|
|
|
|
if ((await ctx.BotPermissions).HasFlag(PermissionSet.ManageMessages))
|
|
|
|
await _rest.DeleteMessage(ctx.Channel.Id, ctx.Message.Id);
|
|
|
|
}
|
|
|
|
catch (NotFoundException)
|
|
|
|
{
|
|
|
|
throw new PKError("Could not reproxy message.");
|
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2021-05-03 10:33:30 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task EditMessage(Context ctx)
|
|
|
|
{
|
2022-05-08 21:10:07 +00:00
|
|
|
var msg = await GetMessageToEdit(ctx, EditTimeout, false);
|
2022-01-07 19:06:37 +00:00
|
|
|
|
2022-01-11 14:43:55 +00:00
|
|
|
if (ctx.System.Id != msg.System?.Id)
|
2021-11-27 02:10:56 +00:00
|
|
|
throw new PKError("Can't edit a message sent by a different system.");
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2022-01-21 22:10:00 +00:00
|
|
|
if (!ctx.HasNext())
|
|
|
|
throw new PKSyntaxError("You need to include the message to edit in.");
|
|
|
|
|
2022-04-07 07:43:46 +00:00
|
|
|
var originalMsg = await _rest.GetMessageOrNull(msg.Message.Channel, msg.Message.Mid);
|
|
|
|
if (originalMsg == null)
|
|
|
|
throw new PKError("Could not edit message.");
|
|
|
|
|
|
|
|
// Check if we should append or prepend
|
|
|
|
var append = ctx.MatchFlag("append");
|
|
|
|
var prepend = ctx.MatchFlag("prepend");
|
2022-04-07 07:47:38 +00:00
|
|
|
|
2022-04-07 07:43:46 +00:00
|
|
|
// Grab the original message content and new message content
|
|
|
|
var originalContent = originalMsg.Content;
|
2021-11-27 02:10:56 +00:00
|
|
|
var newContent = ctx.RemainderOrNull().NormalizeLineEndSpacing();
|
2022-04-07 07:47:38 +00:00
|
|
|
|
2022-04-07 07:43:46 +00:00
|
|
|
// Append or prepend the new content to the original message content if needed.
|
|
|
|
// If no flag is supplied, the new contents will completly overwrite the old contents
|
|
|
|
// If both flags are specified. the message will be prepended AND appended
|
|
|
|
if (append && prepend) newContent = $"{newContent} {originalContent} {newContent}";
|
2022-04-07 07:47:38 +00:00
|
|
|
else if (append) newContent = originalContent + " " + newContent;
|
|
|
|
else if (prepend) newContent = newContent + " " + originalContent;
|
2021-05-03 10:33:30 +00:00
|
|
|
|
2022-01-21 23:23:58 +00:00
|
|
|
if (newContent.Length > 2000)
|
|
|
|
throw new PKError("PluralKit cannot proxy messages over 2000 characters in length.");
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var editedMsg =
|
|
|
|
await _webhookExecutor.EditWebhookMessage(msg.Message.Channel, msg.Message.Mid, newContent);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (ctx.Guild == null)
|
|
|
|
await _rest.CreateReaction(ctx.Channel.Id, ctx.Message.Id, new Emoji { Name = Emojis.Success });
|
2021-07-18 00:39:12 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if ((await ctx.BotPermissions).HasFlag(PermissionSet.ManageMessages))
|
|
|
|
await _rest.DeleteMessage(ctx.Channel.Id, ctx.Message.Id);
|
2021-05-07 16:35:09 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
await _logChannel.LogMessage(ctx.MessageContext, msg.Message, ctx.Message, editedMsg,
|
|
|
|
originalMsg!.Content!);
|
2021-05-03 10:33:30 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
catch (NotFoundException)
|
2021-05-03 10:33:30 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
throw new PKError("Could not edit message.");
|
|
|
|
}
|
|
|
|
}
|
2021-07-27 15:39:37 +00:00
|
|
|
|
2022-05-08 21:10:07 +00:00
|
|
|
private async Task<FullMessage> GetMessageToEdit(Context ctx, Duration timeout, bool isReproxy)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
2022-05-08 21:10:07 +00:00
|
|
|
var editType = isReproxy ? "reproxy" : "edit";
|
|
|
|
var editTypeAction = isReproxy ? "reproxied" : "edited";
|
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
// todo: is it correct to get a connection here?
|
|
|
|
await using var conn = await ctx.Database.Obtain();
|
2021-11-27 02:10:56 +00:00
|
|
|
FullMessage? msg = null;
|
2021-05-03 10:33:30 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var (referencedMessage, _) = ctx.MatchMessage(false);
|
|
|
|
if (referencedMessage != null)
|
|
|
|
{
|
2022-01-22 08:05:01 +00:00
|
|
|
msg = await ctx.Repository.GetMessage(conn, referencedMessage.Value);
|
2021-07-27 15:39:37 +00:00
|
|
|
if (msg == null)
|
2021-11-27 02:10:56 +00:00
|
|
|
throw new PKError("This is not a message proxied by PluralKit.");
|
|
|
|
}
|
2021-05-07 21:31:43 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (msg == null)
|
|
|
|
{
|
|
|
|
if (ctx.Guild == null)
|
2022-05-08 21:10:07 +00:00
|
|
|
throw new PKSyntaxError($"You must use a message link to {editType} messages in DMs.");
|
2021-07-27 15:39:37 +00:00
|
|
|
|
2022-05-08 21:10:07 +00:00
|
|
|
var recent = await FindRecentMessage(ctx, timeout);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (recent == null)
|
2022-05-08 21:10:07 +00:00
|
|
|
throw new PKSyntaxError($"Could not find a recent message to {editType}.");
|
2021-07-27 15:39:37 +00:00
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
msg = await ctx.Repository.GetMessage(conn, recent.Mid);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (msg == null)
|
2022-05-08 21:10:07 +00:00
|
|
|
throw new PKSyntaxError($"Could not find a recent message to {editType}.");
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2021-11-11 04:46:16 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (msg.Message.Channel != ctx.Channel.Id)
|
|
|
|
{
|
|
|
|
var error =
|
|
|
|
"The channel where the message was sent does not exist anymore, or you are missing permissions to access it.";
|
2021-11-11 04:46:16 +00:00
|
|
|
|
2022-03-10 01:06:53 +00:00
|
|
|
var channel = await _rest.GetChannelOrNull(msg.Message.Channel);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (channel == null)
|
|
|
|
throw new PKError(error);
|
|
|
|
|
|
|
|
if (!await ctx.CheckPermissionsInGuildChannel(channel,
|
2021-11-11 04:46:16 +00:00
|
|
|
PermissionSet.ViewChannel | PermissionSet.SendMessages
|
|
|
|
))
|
2021-11-27 02:10:56 +00:00
|
|
|
throw new PKError(error);
|
2021-05-03 10:33:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 21:10:07 +00:00
|
|
|
var msgTimestamp = DiscordUtils.SnowflakeToInstant(msg.Message.Mid);
|
|
|
|
if (isReproxy && SystemClock.Instance.GetCurrentInstant() - msgTimestamp > timeout)
|
|
|
|
throw new PKError($"The message is too old to be {editTypeAction}.");
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return msg;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2022-05-08 21:10:07 +00:00
|
|
|
private async Task<PKMessage?> FindRecentMessage(Context ctx, Duration timeout)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
2022-01-22 08:05:01 +00:00
|
|
|
var lastMessage = await ctx.Repository.GetLastMessage(ctx.Guild.Id, ctx.Channel.Id, ctx.Author.Id);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (lastMessage == null)
|
|
|
|
return null;
|
2021-05-03 10:33:30 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var timestamp = DiscordUtils.SnowflakeToInstant(lastMessage.Mid);
|
2022-05-08 21:10:07 +00:00
|
|
|
if (SystemClock.Instance.GetCurrentInstant() - timestamp > timeout)
|
2021-11-27 02:10:56 +00:00
|
|
|
return null;
|
|
|
|
|
|
|
|
return lastMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task GetMessage(Context ctx)
|
|
|
|
{
|
|
|
|
var (messageId, _) = ctx.MatchMessage(true);
|
|
|
|
if (messageId == null)
|
|
|
|
{
|
|
|
|
if (!ctx.HasNext())
|
|
|
|
throw new PKSyntaxError("You must pass a message ID or link.");
|
|
|
|
throw new PKSyntaxError($"Could not parse {ctx.PeekArgument().AsCode()} as a message ID or link.");
|
2021-05-03 10:33:30 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var isDelete = ctx.Match("delete") || ctx.MatchFlag("delete");
|
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
var message = await ctx.Database.Execute(c => ctx.Repository.GetMessage(c, messageId.Value));
|
2021-11-27 02:10:56 +00:00
|
|
|
if (message == null)
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
if (isDelete)
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
await DeleteCommandMessage(ctx, messageId.Value);
|
|
|
|
return;
|
2021-08-25 18:36:13 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
throw Errors.MessageNotFound(messageId.Value);
|
|
|
|
}
|
2021-09-27 02:49:43 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var showContent = true;
|
|
|
|
var noShowContentError = "Message deleted or inaccessible.";
|
2021-08-25 18:36:13 +00:00
|
|
|
|
2022-03-10 01:06:53 +00:00
|
|
|
var channel = await _rest.GetChannelOrNull(message.Message.Channel);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (channel == null)
|
|
|
|
showContent = false;
|
|
|
|
else if (!await ctx.CheckPermissionsInGuildChannel(channel, PermissionSet.ViewChannel))
|
|
|
|
showContent = false;
|
2021-11-11 04:46:16 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (ctx.MatchRaw())
|
|
|
|
{
|
|
|
|
var discordMessage = await _rest.GetMessageOrNull(message.Message.Channel, message.Message.Mid);
|
|
|
|
if (discordMessage == null || !showContent)
|
|
|
|
throw new PKError(noShowContentError);
|
2021-11-11 04:46:16 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var content = discordMessage.Content;
|
|
|
|
if (content == null || content == "")
|
2021-10-29 20:42:10 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Reply("No message content found in that message.");
|
2021-10-29 20:42:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Reply($"```{content}```");
|
2021-09-22 17:48:34 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (Regex.IsMatch(content, "```.*```", RegexOptions.Singleline))
|
2021-08-25 18:36:13 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
|
|
|
|
await ctx.Rest.CreateMessage(
|
|
|
|
ctx.Channel.Id,
|
|
|
|
new MessageRequest
|
|
|
|
{
|
|
|
|
Content = $"{Emojis.Warn} Message contains codeblocks, raw source sent as an attachment."
|
|
|
|
},
|
|
|
|
new[] { new MultipartFile("message.txt", stream, null) });
|
2021-08-25 18:36:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return;
|
2021-08-25 18:36:13 +00:00
|
|
|
}
|
2021-09-27 02:49:43 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (isDelete)
|
2021-09-27 02:49:43 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!showContent)
|
|
|
|
throw new PKError(noShowContentError);
|
2021-09-27 02:49:43 +00:00
|
|
|
|
2022-01-11 14:43:55 +00:00
|
|
|
if (message.System?.Id != ctx.System.Id && message.Message.Sender != ctx.Author.Id)
|
2021-11-27 02:10:56 +00:00
|
|
|
throw new PKError("You can only delete your own messages.");
|
2021-09-27 02:49:43 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Rest.DeleteMessage(message.Message.Channel, message.Message.Mid);
|
2021-09-27 02:49:43 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (ctx.Channel.Id == message.Message.Channel)
|
2021-09-27 02:49:43 +00:00
|
|
|
await ctx.Rest.DeleteMessage(ctx.Message);
|
|
|
|
else
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Rest.CreateReaction(ctx.Message.ChannelId, ctx.Message.Id,
|
|
|
|
new Emoji { Name = Emojis.Success });
|
|
|
|
|
|
|
|
return;
|
2021-09-27 02:49:43 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
if (ctx.Match("author") || ctx.MatchFlag("author"))
|
|
|
|
{
|
2022-03-10 01:06:53 +00:00
|
|
|
var user = await _rest.GetUser(message.Message.Sender);
|
2021-11-27 02:10:56 +00:00
|
|
|
var eb = new EmbedBuilder()
|
|
|
|
.Author(new Embed.EmbedAuthor(
|
|
|
|
user != null
|
|
|
|
? $"{user.Username}#{user.Discriminator}"
|
|
|
|
: $"Deleted user ${message.Message.Sender}",
|
|
|
|
IconUrl: user != null ? user.AvatarUrl() : null))
|
|
|
|
.Description(message.Message.Sender.ToString());
|
|
|
|
|
|
|
|
await ctx.Reply(
|
|
|
|
user != null ? $"{user.Mention()} ({user.Id})" : $"*(deleted user {message.Message.Sender})*",
|
|
|
|
eb.Build());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await ctx.Reply(embed: await _embeds.CreateMessageInfoEmbed(message, showContent));
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task DeleteCommandMessage(Context ctx, ulong messageId)
|
|
|
|
{
|
2022-01-22 08:05:01 +00:00
|
|
|
var message = await ctx.Repository.GetCommandMessage(messageId);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (message == null)
|
|
|
|
throw Errors.MessageNotFound(messageId);
|
|
|
|
|
|
|
|
if (message.AuthorId != ctx.Author.Id)
|
|
|
|
throw new PKError("You can only delete command messages queried by this account.");
|
|
|
|
|
|
|
|
await ctx.Rest.DeleteMessage(message.ChannelId, message.MessageId);
|
|
|
|
|
|
|
|
if (ctx.Guild != null)
|
|
|
|
await ctx.Rest.DeleteMessage(ctx.Message);
|
|
|
|
else
|
|
|
|
await ctx.Rest.CreateReaction(ctx.Message.ChannelId, ctx.Message.Id, new Emoji { Name = Emojis.Success });
|
2021-05-03 10:33:30 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
}
|