Add info embed to proxied replies

This commit is contained in:
Ske 2020-12-20 11:38:26 +01:00
parent 8a04ace5c7
commit df243d4220
3 changed files with 63 additions and 7 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using App.Metrics; using App.Metrics;
@ -9,6 +10,8 @@ using DSharpPlus;
using DSharpPlus.Entities; using DSharpPlus.Entities;
using DSharpPlus.Exceptions; using DSharpPlus.Exceptions;
using Humanizer;
using PluralKit.Core; using PluralKit.Core;
using Serilog; using Serilog;
@ -95,16 +98,66 @@ namespace PluralKit.Bot
private async Task ExecuteProxy(DiscordClient shard, IPKConnection conn, DiscordMessage trigger, MessageContext ctx, private async Task ExecuteProxy(DiscordClient shard, IPKConnection conn, DiscordMessage trigger, MessageContext ctx,
ProxyMatch match, bool allowEveryone, bool allowEmbeds) ProxyMatch match, bool allowEveryone, bool allowEmbeds)
{ {
// Create reply embed
var embeds = new List<DiscordEmbed>();
if (trigger.Reference?.Channel?.Id == trigger.ChannelId)
{
var embed = await CreateReplyEmbed(trigger);
if (embed != null)
embeds.Add(embed);
}
// Send the webhook // Send the webhook
var content = match.ProxyContent; var content = match.ProxyContent;
if (!allowEmbeds) content = content.BreakLinkEmbeds(); if (!allowEmbeds) content = content.BreakLinkEmbeds();
var proxyMessage = await _webhookExecutor.ExecuteWebhook(trigger.Channel, FixSingleCharacterName(match.Member.ProxyName(ctx)), var proxyMessage = await _webhookExecutor.ExecuteWebhook(trigger.Channel, FixSingleCharacterName(match.Member.ProxyName(ctx)),
match.Member.ProxyAvatar(ctx), match.Member.ProxyAvatar(ctx),
content, trigger.Attachments, allowEveryone); content, trigger.Attachments, embeds, allowEveryone);
await HandleProxyExecutedActions(shard, conn, ctx, trigger, proxyMessage, match); await HandleProxyExecutedActions(shard, conn, ctx, trigger, proxyMessage, match);
} }
private async Task<DiscordEmbed> CreateReplyEmbed(DiscordMessage trigger)
{
DiscordMessage message;
try
{
message = await trigger.Channel.GetMessageAsync(trigger.Reference.Message.Id);
}
catch (NotFoundException)
{
_logger.Warning("Attempted to fetch reply message {ChannelId}/{MessageId} but it was not found",
trigger.Reference.Channel.Id, trigger.Reference.Message.Id);
return null;
}
catch (UnauthorizedException)
{
_logger.Warning("Attempted to fetch reply message {ChannelId}/{MessageId} but bot was not allowed to",
trigger.Reference.Channel.Id, trigger.Reference.Message.Id);
return null;
}
var content = new StringBuilder();
content.Append("[Reply to ");
if (message.WebhookMessage)
content.Append($"**{message.Author.Username.EscapeMarkdown()}**");
else
content.Append(message.Author.Mention);
content.Append($"]({message.JumpLink}): ");
if (message.Attachments.Count > 0)
content.Append($"{Emojis.Image} ");
if (!string.IsNullOrWhiteSpace(message.Content))
content.Append($"{message.Content.Truncate(100)}");
return new DiscordEmbedBuilder()
.WithDescription(content.ToString())
.Build();
}
private async Task HandleProxyExecutedActions(DiscordClient shard, IPKConnection conn, MessageContext ctx, private async Task HandleProxyExecutedActions(DiscordClient shard, IPKConnection conn, MessageContext ctx,
DiscordMessage triggerMessage, DiscordMessage proxyMessage, DiscordMessage triggerMessage, DiscordMessage proxyMessage,
ProxyMatch match) ProxyMatch match)

View File

@ -42,13 +42,13 @@ namespace PluralKit.Bot
_logger = logger.ForContext<WebhookExecutorService>(); _logger = logger.ForContext<WebhookExecutorService>();
} }
public async Task<DiscordMessage> ExecuteWebhook(DiscordChannel channel, string name, string avatarUrl, string content, IReadOnlyList<DiscordAttachment> attachments, bool allowEveryone) public async Task<DiscordMessage> ExecuteWebhook(DiscordChannel channel, string name, string avatarUrl, string content, IReadOnlyList<DiscordAttachment> attachments, IReadOnlyList<DiscordEmbed> embeds, bool allowEveryone)
{ {
_logger.Verbose("Invoking webhook in channel {Channel}", channel.Id); _logger.Verbose("Invoking webhook in channel {Channel}", channel.Id);
// Get a webhook, execute it // Get a webhook, execute it
var webhook = await _webhookCache.GetWebhook(channel); var webhook = await _webhookCache.GetWebhook(channel);
var webhookMessage = await ExecuteWebhookInner(channel, webhook, name, avatarUrl, content, attachments, allowEveryone); var webhookMessage = await ExecuteWebhookInner(channel, webhook, name, avatarUrl, content, attachments, embeds, allowEveryone);
// Log the relevant metrics // Log the relevant metrics
_metrics.Measure.Meter.Mark(BotMetrics.MessagesProxied); _metrics.Measure.Meter.Mark(BotMetrics.MessagesProxied);
@ -58,8 +58,9 @@ namespace PluralKit.Bot
return webhookMessage; return webhookMessage;
} }
private async Task<DiscordMessage> ExecuteWebhookInner(DiscordChannel channel, DiscordWebhook webhook, string name, string avatarUrl, string content, private async Task<DiscordMessage> ExecuteWebhookInner(
IReadOnlyList<DiscordAttachment> attachments, bool allowEveryone, bool hasRetried = false) DiscordChannel channel, DiscordWebhook webhook, string name, string avatarUrl, string content,
IReadOnlyList<DiscordAttachment> attachments, IReadOnlyList<DiscordEmbed> embeds, bool allowEveryone, bool hasRetried = false)
{ {
content = content.Truncate(2000); content = content.Truncate(2000);
@ -69,6 +70,7 @@ namespace PluralKit.Bot
dwb.AddMentions(content.ParseAllMentions(allowEveryone, channel.Guild)); dwb.AddMentions(content.ParseAllMentions(allowEveryone, channel.Guild));
if (!string.IsNullOrWhiteSpace(avatarUrl)) if (!string.IsNullOrWhiteSpace(avatarUrl))
dwb.WithAvatarUrl(avatarUrl); dwb.WithAvatarUrl(avatarUrl);
dwb.AddEmbeds(embeds);
var attachmentChunks = ChunkAttachmentsOrThrow(attachments, 8 * 1024 * 1024); var attachmentChunks = ChunkAttachmentsOrThrow(attachments, 8 * 1024 * 1024);
if (attachmentChunks.Count > 0) if (attachmentChunks.Count > 0)
@ -99,7 +101,7 @@ namespace PluralKit.Bot
_logger.Warning("Error invoking webhook {Webhook} in channel {Channel}", webhook.Id, webhook.ChannelId); _logger.Warning("Error invoking webhook {Webhook} in channel {Channel}", webhook.Id, webhook.ChannelId);
var newWebhook = await _webhookCache.InvalidateAndRefreshWebhook(channel, webhook); var newWebhook = await _webhookCache.InvalidateAndRefreshWebhook(channel, webhook);
return await ExecuteWebhookInner(channel, newWebhook, name, avatarUrl, content, attachments, allowEveryone, hasRetried: true); return await ExecuteWebhookInner(channel, newWebhook, name, avatarUrl, content, attachments, embeds, allowEveryone, hasRetried: true);
} }
throw; throw;

View File

@ -7,5 +7,6 @@
public static readonly string ThumbsUp = "\U0001f44d"; public static readonly string ThumbsUp = "\U0001f44d";
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";
} }
} }