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
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
4 changed files with 50 additions and 19 deletions

View File

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

View File

@ -1,11 +1,15 @@
#nullable enable #nullable enable
using System; using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Myriad.Builders; using Myriad.Builders;
using Myriad.Cache; using Myriad.Cache;
using Myriad.Extensions; using Myriad.Extensions;
using Myriad.Rest; using Myriad.Rest;
using Myriad.Rest.Types;
using Myriad.Rest.Types.Requests;
using Myriad.Rest.Exceptions; using Myriad.Rest.Exceptions;
using Myriad.Types; using Myriad.Types;
@ -52,7 +56,7 @@ namespace PluralKit.Bot
var newContent = ctx.RemainderOrNull().NormalizeLineEndSpacing(); 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) if (originalMsg == null)
throw new PKError("Could not edit message."); throw new PKError("Could not edit message.");
@ -141,6 +145,33 @@ namespace PluralKit.Bot
throw Errors.MessageNotFound(messageId.Value); 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 (isDelete)
{ {
if (message.System.Id != ctx.System.Id) if (message.System.Id != ctx.System.Id)

View File

@ -280,15 +280,7 @@ namespace PluralKit.Bot
var channel = await _cache.GetOrFetchChannel(_rest, msg.Message.Channel); var channel = await _cache.GetOrFetchChannel(_rest, msg.Message.Channel);
var ctx = LookupContext.ByNonOwner; var ctx = LookupContext.ByNonOwner;
Message serverMsg = null; var serverMsg = await _rest.GetMessageOrNull(msg.Message.Channel, msg.Message.Mid);
try
{
serverMsg = await _rest.GetMessage(msg.Message.Channel, msg.Message.Mid);
}
catch (ForbiddenException)
{
// no permission, couldn't fetch, oh well
}
// Need this whole dance to handle cases where: // Need this whole dance to handle cases where:
// - the user is deleted (userInfo == null) // - the user is deleted (userInfo == null)

View File

@ -9,6 +9,7 @@ using Myriad.Builders;
using Myriad.Extensions; using Myriad.Extensions;
using Myriad.Gateway; using Myriad.Gateway;
using Myriad.Rest; using Myriad.Rest;
using Myriad.Rest.Exceptions;
using Myriad.Rest.Types; using Myriad.Rest.Types;
using Myriad.Types; using Myriad.Types;
@ -56,6 +57,19 @@ namespace PluralKit.Bot
} }
} }
public static async Task<Message?> GetMessageOrNull(this DiscordApiClient rest, ulong channelId, ulong messageId)
{
try
{
return await rest.GetMessage(channelId, messageId);
}
catch (ForbiddenException)
{
// no permission, couldn't fetch, oh well
return null;
}
}
public static uint? ToDiscordColor(this string color) public static uint? ToDiscordColor(this string color)
{ {
if (uint.TryParse(color, NumberStyles.HexNumber, null, out var colorInt)) if (uint.TryParse(color, NumberStyles.HexNumber, null, out var colorInt))