PluralKit/PluralKit.Bot/Commands/Message.cs

273 lines
9.8 KiB
C#
Raw Normal View History

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;
using Myriad.Builders;
using Myriad.Cache;
using Myriad.Extensions;
using Myriad.Rest;
using Myriad.Rest.Exceptions;
2021-10-29 20:42:10 +00:00
using Myriad.Rest.Types;
using Myriad.Rest.Types.Requests;
using Myriad.Types;
using NodaTime;
using PluralKit.Core;
namespace PluralKit.Bot;
public class ProxiedMessage
{
private static readonly Duration EditTimeout = Duration.FromMinutes(10);
// private readonly IDiscordCache _cache;
private readonly IClock _clock;
private readonly EmbedService _embeds;
private readonly LogChannelService _logChannel;
private readonly DiscordApiClient _rest;
private readonly WebhookExecutorService _webhookExecutor;
public ProxiedMessage(EmbedService embeds, IClock clock,
DiscordApiClient rest,
WebhookExecutorService webhookExecutor, LogChannelService logChannel, IDiscordCache cache)
{
_embeds = embeds;
_clock = clock;
_rest = rest;
_webhookExecutor = webhookExecutor;
_logChannel = logChannel;
// _cache = cache;
}
public async Task EditMessage(Context ctx)
{
var msg = await GetMessageToEdit(ctx);
if (ctx.System.Id != msg.System?.Id)
throw new PKError("Can't edit a message sent by a different system.");
2021-08-27 15:03:47 +00:00
if (!ctx.HasNext())
throw new PKSyntaxError("You need to include the message to edit in.");
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");
// Grab the original message content and new message content
var originalContent = originalMsg.Content;
var newContent = ctx.RemainderOrNull().NormalizeLineEndSpacing();
// 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}";
else if (append) newContent = originalContent +" "+ newContent;
else if (prepend) newContent = newContent +" "+ originalContent;
if (newContent.Length > 2000)
throw new PKError("PluralKit cannot proxy messages over 2000 characters in length.");
try
{
var editedMsg =
await _webhookExecutor.EditWebhookMessage(msg.Message.Channel, msg.Message.Mid, newContent);
2021-08-27 15:03:47 +00:00
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);
2021-05-07 16:35:09 +00:00
await _logChannel.LogMessage(ctx.MessageContext, msg.Message, ctx.Message, editedMsg,
originalMsg!.Content!);
}
catch (NotFoundException)
{
throw new PKError("Could not edit message.");
}
}
private async Task<FullMessage> GetMessageToEdit(Context ctx)
{
// todo: is it correct to get a connection here?
await using var conn = await ctx.Database.Obtain();
FullMessage? msg = null;
var (referencedMessage, _) = ctx.MatchMessage(false);
if (referencedMessage != null)
{
msg = await ctx.Repository.GetMessage(conn, referencedMessage.Value);
if (msg == null)
throw new PKError("This is not a message proxied by PluralKit.");
}
if (msg == null)
{
if (ctx.Guild == null)
throw new PKSyntaxError("You must use a message link to edit messages in DMs.");
var recent = await FindRecentMessage(ctx);
if (recent == null)
throw new PKSyntaxError("Could not find a recent message to edit.");
msg = await ctx.Repository.GetMessage(conn, recent.Mid);
if (msg == null)
throw new PKSyntaxError("Could not find a recent message to edit.");
}
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.";
var channel = await _rest.GetChannelOrNull(msg.Message.Channel);
if (channel == null)
throw new PKError(error);
if (!await ctx.CheckPermissionsInGuildChannel(channel,
PermissionSet.ViewChannel | PermissionSet.SendMessages
))
throw new PKError(error);
}
return msg;
}
2021-08-27 15:03:47 +00:00
private async Task<PKMessage?> FindRecentMessage(Context ctx)
{
var lastMessage = await ctx.Repository.GetLastMessage(ctx.Guild.Id, ctx.Channel.Id, ctx.Author.Id);
if (lastMessage == null)
return null;
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-08-27 15:03:47 +00:00
var isDelete = ctx.Match("delete") || ctx.MatchFlag("delete");
var message = await ctx.Database.Execute(c => ctx.Repository.GetMessage(c, messageId.Value));
if (message == null)
{
if (isDelete)
{
await DeleteCommandMessage(ctx, messageId.Value);
return;
}
2021-08-27 15:03:47 +00:00
throw Errors.MessageNotFound(messageId.Value);
}
var showContent = true;
var noShowContentError = "Message deleted or inaccessible.";
var channel = await _rest.GetChannelOrNull(message.Message.Channel);
if (channel == null)
showContent = false;
else if (!await ctx.CheckPermissionsInGuildChannel(channel, PermissionSet.ViewChannel))
showContent = false;
if (ctx.MatchRaw())
{
var discordMessage = await _rest.GetMessageOrNull(message.Message.Channel, message.Message.Mid);
if (discordMessage == null || !showContent)
throw new PKError(noShowContentError);
var content = discordMessage.Content;
if (content == null || content == "")
2021-10-29 20:42:10 +00:00
{
await ctx.Reply("No message content found in that message.");
2021-10-29 20:42:10 +00:00
return;
}
await ctx.Reply($"```{content}```");
2021-09-22 17:48:34 +00:00
if (Regex.IsMatch(content, "```.*```", RegexOptions.Singleline))
{
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) });
}
return;
}
if (isDelete)
{
if (!showContent)
throw new PKError(noShowContentError);
if (message.System?.Id != ctx.System.Id && message.Message.Sender != ctx.Author.Id)
throw new PKError("You can only delete your own messages.");
await ctx.Rest.DeleteMessage(message.Message.Channel, message.Message.Mid);
if (ctx.Channel.Id == message.Message.Channel)
await ctx.Rest.DeleteMessage(ctx.Message);
else
await ctx.Rest.CreateReaction(ctx.Message.ChannelId, ctx.Message.Id,
new Emoji { Name = Emojis.Success });
return;
}
if (ctx.Match("author") || ctx.MatchFlag("author"))
{
var user = await _rest.GetUser(message.Message.Sender);
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)
{
var message = await ctx.Repository.GetCommandMessage(messageId);
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-08-27 15:03:47 +00:00
}