feat: add -raw flag to pk;msg (#399)

This commit is contained in:
Katrix
2021-10-29 16:42:10 -04:00
committed by spiral
parent 9a34834ca9
commit b998636cbe
4 changed files with 50 additions and 19 deletions

View File

@@ -245,15 +245,9 @@ namespace PluralKit.Bot
}
// get the message info
var msg = ctx.Message;
try
{
msg = await _rest.GetMessage(channelId.Value, messageId.Value);
}
catch (ForbiddenException)
{
var msg = await _rest.GetMessageOrNull(channelId.Value, messageId.Value);
if (msg == null)
throw new PKError(failedToGetMessage);
}
// if user is fetching a message in a different channel sent by someone else, throw a generic error message
if (msg == null || (msg.Author.Id != ctx.Author.Id && msg.ChannelId != ctx.Channel.Id))

View File

@@ -1,11 +1,15 @@
#nullable enable
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Myriad.Builders;
using Myriad.Cache;
using Myriad.Extensions;
using Myriad.Rest;
using Myriad.Rest.Types;
using Myriad.Rest.Types.Requests;
using Myriad.Rest.Exceptions;
using Myriad.Types;
@@ -52,7 +56,7 @@ namespace PluralKit.Bot
var newContent = ctx.RemainderOrNull().NormalizeLineEndSpacing();
var originalMsg = await _rest.GetMessage(msg.Message.Channel, msg.Message.Mid);
var originalMsg = await _rest.GetMessageOrNull(msg.Message.Channel, msg.Message.Mid);
if (originalMsg == null)
throw new PKError("Could not edit message.");
@@ -141,6 +145,33 @@ namespace PluralKit.Bot
throw Errors.MessageNotFound(messageId.Value);
}
if (ctx.MatchRaw())
{
var discordMessage = await _rest.GetMessageOrNull(message.Message.Channel, message.Message.Mid);
if (discordMessage == null)
throw new PKError("Message deleted or inaccessible.");
var content = discordMessage.Content;
if (content == null || content == "")
{
await ctx.Reply("No message content found in that message.");
return;
}
await ctx.Reply(text: $"```{content}```");
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) });
}
return;
}
if (isDelete)
{
if (message.System.Id != ctx.System.Id)