Avoid making an extra request to fetch message body in webhooks

This commit is contained in:
Ske 2019-07-15 17:53:01 +02:00
parent eb749d0c37
commit 3a6e061e44
3 changed files with 20 additions and 16 deletions

View File

@ -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();
}

View File

@ -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<ITextChannel> GetLogChannel(IGuild guild) {

View File

@ -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<IMessage> ExecuteWebhook(IWebhook webhook, string text, string username, string avatarUrl, IAttachment attachment)
private async Task<ulong> 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<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction)