From 3a6e061e44cbc1e77979fd613e5f61f243c2ba48 Mon Sep 17 00:00:00 2001 From: Ske Date: Mon, 15 Jul 2019 17:53:01 +0200 Subject: [PATCH] Avoid making an extra request to fetch message body in webhooks --- PluralKit.Bot/Services/EmbedService.cs | 11 ++++++----- PluralKit.Bot/Services/LogChannelService.cs | 12 +++++++----- PluralKit.Bot/Services/ProxyService.cs | 13 +++++++------ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index 9e5c0367..78c56858 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -44,13 +44,14 @@ namespace PluralKit.Bot { return eb.Build(); } - public Embed CreateLoggedMessageEmbed(PKSystem system, PKMember member, IMessage message, IUser sender) { + public Embed CreateLoggedMessageEmbed(PKSystem system, PKMember member, ulong messageId, IUser sender, string content, IGuildChannel channel) { // TODO: pronouns in ?-reacted response using this card + var timestamp = SnowflakeUtils.FromSnowflake(messageId); return new EmbedBuilder() - .WithAuthor($"#{message.Channel.Name}: {member.Name}", member.AvatarUrl) - .WithDescription(message.Content) - .WithFooter($"System ID: {system.Hid} | Member ID: {member.Hid} | Sender: {sender.Username}#{sender.Discriminator} ({sender.Id}) | Message ID: {message.Id}") - .WithTimestamp(message.Timestamp) + .WithAuthor($"#{channel.Name}: {member.Name}", member.AvatarUrl) + .WithDescription(content) + .WithFooter($"System ID: {system.Hid} | Member ID: {member.Hid} | Sender: {sender.Username}#{sender.Discriminator} ({sender.Id}) | Message ID: {messageId}") + .WithTimestamp(timestamp) .Build(); } diff --git a/PluralKit.Bot/Services/LogChannelService.cs b/PluralKit.Bot/Services/LogChannelService.cs index 8d2ca896..fbcd36ac 100644 --- a/PluralKit.Bot/Services/LogChannelService.cs +++ b/PluralKit.Bot/Services/LogChannelService.cs @@ -21,12 +21,14 @@ namespace PluralKit.Bot { this._embed = embed; } - public async Task LogMessage(PKSystem system, PKMember member, IMessage message, IUser sender) { - var channel = await GetLogChannel((message.Channel as IGuildChannel).Guild); - if (channel == null) return; + public async Task LogMessage(PKSystem system, PKMember member, ulong messageId, IGuildChannel originalChannel, IUser sender, string content) { + var logChannel = await GetLogChannel(originalChannel.Guild); + if (logChannel == null) return; - var embed = _embed.CreateLoggedMessageEmbed(system, member, message, sender); - await channel.SendMessageAsync(text: message.GetJumpUrl(), embed: embed); + var embed = _embed.CreateLoggedMessageEmbed(system, member, messageId, sender, content, originalChannel); + + var url = $"https://discordapp.com/channels/{originalChannel.GuildId}/{originalChannel.Id}/{messageId}"; + await logChannel.SendMessageAsync(text: url, embed: embed); } public async Task GetLogChannel(IGuild guild) { diff --git a/PluralKit.Bot/Services/ProxyService.cs b/PluralKit.Bot/Services/ProxyService.cs index 54080cc3..0c099224 100644 --- a/PluralKit.Bot/Services/ProxyService.cs +++ b/PluralKit.Bot/Services/ProxyService.cs @@ -98,11 +98,11 @@ namespace PluralKit.Bot // Fetch a webhook for this channel, and send the proxied message var webhook = await _webhookCache.GetWebhook(message.Channel as ITextChannel); - var hookMessage = await ExecuteWebhook(webhook, match.InnerText, match.ProxyName, match.Member.AvatarUrl, message.Attachments.FirstOrDefault()); + var hookMessageId = await ExecuteWebhook(webhook, match.InnerText, match.ProxyName, match.Member.AvatarUrl, message.Attachments.FirstOrDefault()); // Store the message in the database, and log it in the log channel (if applicable) - await _messageStorage.Store(message.Author.Id, hookMessage.Id, hookMessage.Channel.Id, match.Member); - await _logger.LogMessage(match.System, match.Member, hookMessage, message.Author); + await _messageStorage.Store(message.Author.Id, hookMessageId, message.Channel.Id, match.Member); + await _logger.LogMessage(match.System, match.Member, hookMessageId, message.Channel as IGuildChannel, message.Author, match.InnerText); // Wait a second or so before deleting the original message await Task.Delay(1000); @@ -131,7 +131,7 @@ namespace PluralKit.Bot return true; } - private async Task ExecuteWebhook(IWebhook webhook, string text, string username, string avatarUrl, IAttachment attachment) + private async Task ExecuteWebhook(IWebhook webhook, string text, string username, string avatarUrl, IAttachment attachment) { username = FixClyde(username); @@ -159,8 +159,9 @@ namespace PluralKit.Bot messageId = await client.SendMessageAsync(text, username: username, avatarUrl: avatarUrl); } - // TODO: SendMessageAsync should return a full object(??), see if there's a way to avoid the extra server call here - return await webhook.Channel.GetMessageAsync(messageId); + // TODO: figure out a way to return the full message object (without doing a GetMessageAsync call, which + // doesn't work if there's no permission to) + return messageId; } public Task HandleReactionAddedAsync(Cacheable message, ISocketMessageChannel channel, SocketReaction reaction)