Reformat replies

This commit is contained in:
Ske 2020-12-20 16:58:52 +01:00
parent 97f5ab94c1
commit 4e5d02e1a4
2 changed files with 27 additions and 20 deletions

View File

@ -102,7 +102,8 @@ namespace PluralKit.Bot
var embeds = new List<DiscordEmbed>(); var embeds = new List<DiscordEmbed>();
if (trigger.Reference?.Channel?.Id == trigger.ChannelId) if (trigger.Reference?.Channel?.Id == trigger.ChannelId)
{ {
var embed = await CreateReplyEmbed(trigger); var repliedTo = await FetchReplyOriginalMessage(trigger.Reference);
var embed = await CreateReplyEmbed(repliedTo);
if (embed != null) if (embed != null)
embeds.Add(embed); embeds.Add(embed);
} }
@ -117,43 +118,48 @@ namespace PluralKit.Bot
await HandleProxyExecutedActions(shard, conn, ctx, trigger, proxyMessage, match); await HandleProxyExecutedActions(shard, conn, ctx, trigger, proxyMessage, match);
} }
private async Task<DiscordEmbed> CreateReplyEmbed(DiscordMessage trigger) private async Task<DiscordMessage> FetchReplyOriginalMessage(DiscordMessageReference reference)
{ {
DiscordMessage message;
try try
{ {
message = await trigger.Channel.GetMessageAsync(trigger.Reference.Message.Id); return await reference.Channel.GetMessageAsync(reference.Message.Id);
} }
catch (NotFoundException) catch (NotFoundException)
{ {
_logger.Warning("Attempted to fetch reply message {ChannelId}/{MessageId} but it was not found", _logger.Warning("Attempted to fetch reply message {ChannelId}/{MessageId} but it was not found",
trigger.Reference.Channel.Id, trigger.Reference.Message.Id); reference.Channel.Id, reference.Message.Id);
return null;
} }
catch (UnauthorizedException) catch (UnauthorizedException)
{ {
_logger.Warning("Attempted to fetch reply message {ChannelId}/{MessageId} but bot was not allowed to", _logger.Warning("Attempted to fetch reply message {ChannelId}/{MessageId} but bot was not allowed to",
trigger.Reference.Channel.Id, trigger.Reference.Message.Id); reference.Channel.Id, reference.Message.Id);
}
return null; return null;
} }
private async Task<DiscordEmbed> CreateReplyEmbed(DiscordMessage original)
{
var content = new StringBuilder(); var content = new StringBuilder();
content.Append($"[Reply to]({message.JumpLink}) ");
if (message.WebhookMessage) var hasContent = !string.IsNullOrWhiteSpace(original.Content);
content.Append($"**[{message.Author.Username.EscapeMarkdown()}]({message.JumpLink})**"); if (hasContent)
{
content.Append($"**[Reply to:]({original.JumpLink})** ");
content.Append($"{original.Content.Truncate(100)}");
if (original.Attachments.Count > 0)
content.Append($" {Emojis.Paperclip}");
}
else else
content.Append(message.Author.Mention); {
content.Append($"*[(click to see attachment)]({original.JumpLink})*");
}
content.Append(": "); var username = (original.Author as DiscordMember)?.Nickname ?? original.Author.Username;
if (message.Attachments.Count > 0)
content.Append($"{Emojis.Image} ");
if (!string.IsNullOrWhiteSpace(message.Content))
content.Append($"{message.Content.Truncate(100)}");
return new DiscordEmbedBuilder() return new DiscordEmbedBuilder()
// unicodes: [three-per-em space] [left arrow emoji] [force emoji presentation]
.WithAuthor($"{username}\u2004\u21a9\ufe0f", iconUrl: original.Author.AvatarUrl)
.WithDescription(content.ToString()) .WithDescription(content.ToString())
.Build(); .Build();
} }

View File

@ -8,5 +8,6 @@
public static readonly string RedQuestion = "\u2753"; public static readonly string RedQuestion = "\u2753";
public static readonly string Bell = "\U0001F514"; public static readonly string Bell = "\U0001F514";
public static readonly string Image = "\U0001F5BC"; public static readonly string Image = "\U0001F5BC";
public static readonly string Paperclip = "\U0001F4CE";
} }
} }