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;
|
|
|
|
|
|
|
|
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-03-30 08:36:22 +00:00
|
|
|
|
2022-03-10 01:06:53 +00:00
|
|
|
// private readonly IDiscordCache _cache;
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly IClock _clock;
|
|
|
|
|
|
|
|
private readonly EmbedService _embeds;
|
|
|
|
private readonly LogChannelService _logChannel;
|
|
|
|
private readonly DiscordApiClient _rest;
|
|
|
|
private readonly WebhookExecutorService _webhookExecutor;
|
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
public ProxiedMessage(EmbedService embeds, IClock clock,
|
2021-11-27 02:10:56 +00:00
|
|
|
DiscordApiClient rest,
|
|
|
|
WebhookExecutorService webhookExecutor, LogChannelService logChannel, IDiscordCache cache)
|
2021-05-03 10:33:30 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_embeds = embeds;
|
|
|
|
_clock = clock;
|
|
|
|
_rest = rest;
|
|
|
|
_webhookExecutor = webhookExecutor;
|
|
|
|
_logChannel = logChannel;
|
2022-03-10 01:06:53 +00:00
|
|
|
// _cache = cache;
|
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-01-07 19:06:37 +00:00
|
|
|
var msg = await GetMessageToEdit(ctx);
|
|
|
|
|
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.");
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var newContent = ctx.RemainderOrNull().NormalizeLineEndSpacing();
|
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
|
|
|
var originalMsg = await _rest.GetMessageOrNull(msg.Message.Channel, msg.Message.Mid);
|
|
|
|
if (originalMsg == null)
|
|
|
|
throw new PKError("Could not edit message.");
|
2021-05-07 16:35:09 +00:00
|
|
|
|
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
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private async Task<FullMessage> GetMessageToEdit(Context ctx)
|
|
|
|
{
|
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-01-21 22:10:00 +00:00
|
|
|
throw new PKSyntaxError("You must use a message link to edit messages in DMs.");
|
2021-07-27 15:39:37 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var recent = await FindRecentMessage(ctx);
|
|
|
|
if (recent == null)
|
2022-01-21 22:10:00 +00:00
|
|
|
throw new PKSyntaxError("Could not find a recent message to edit.");
|
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-01-21 22:10:00 +00:00
|
|
|
throw new PKSyntaxError("Could not find a recent message to edit.");
|
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
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return msg;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private async Task<PKMessage?> FindRecentMessage(Context ctx)
|
|
|
|
{
|
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);
|
|
|
|
if (_clock.GetCurrentInstant() - timestamp > EditTimeout)
|
|
|
|
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
|
|
|
}
|