Avoid making an extra request to fetch message body in webhooks
This commit is contained in:
parent
eb749d0c37
commit
3a6e061e44
@ -44,13 +44,14 @@ namespace PluralKit.Bot {
|
|||||||
return eb.Build();
|
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
|
// TODO: pronouns in ?-reacted response using this card
|
||||||
|
var timestamp = SnowflakeUtils.FromSnowflake(messageId);
|
||||||
return new EmbedBuilder()
|
return new EmbedBuilder()
|
||||||
.WithAuthor($"#{message.Channel.Name}: {member.Name}", member.AvatarUrl)
|
.WithAuthor($"#{channel.Name}: {member.Name}", member.AvatarUrl)
|
||||||
.WithDescription(message.Content)
|
.WithDescription(content)
|
||||||
.WithFooter($"System ID: {system.Hid} | Member ID: {member.Hid} | Sender: {sender.Username}#{sender.Discriminator} ({sender.Id}) | Message ID: {message.Id}")
|
.WithFooter($"System ID: {system.Hid} | Member ID: {member.Hid} | Sender: {sender.Username}#{sender.Discriminator} ({sender.Id}) | Message ID: {messageId}")
|
||||||
.WithTimestamp(message.Timestamp)
|
.WithTimestamp(timestamp)
|
||||||
.Build();
|
.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,12 +21,14 @@ namespace PluralKit.Bot {
|
|||||||
this._embed = embed;
|
this._embed = embed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task LogMessage(PKSystem system, PKMember member, IMessage message, IUser sender) {
|
public async Task LogMessage(PKSystem system, PKMember member, ulong messageId, IGuildChannel originalChannel, IUser sender, string content) {
|
||||||
var channel = await GetLogChannel((message.Channel as IGuildChannel).Guild);
|
var logChannel = await GetLogChannel(originalChannel.Guild);
|
||||||
if (channel == null) return;
|
if (logChannel == null) return;
|
||||||
|
|
||||||
var embed = _embed.CreateLoggedMessageEmbed(system, member, message, sender);
|
var embed = _embed.CreateLoggedMessageEmbed(system, member, messageId, sender, content, originalChannel);
|
||||||
await channel.SendMessageAsync(text: message.GetJumpUrl(), embed: embed);
|
|
||||||
|
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) {
|
public async Task<ITextChannel> GetLogChannel(IGuild guild) {
|
||||||
|
@ -98,11 +98,11 @@ namespace PluralKit.Bot
|
|||||||
|
|
||||||
// Fetch a webhook for this channel, and send the proxied message
|
// Fetch a webhook for this channel, and send the proxied message
|
||||||
var webhook = await _webhookCache.GetWebhook(message.Channel as ITextChannel);
|
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)
|
// 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 _messageStorage.Store(message.Author.Id, hookMessageId, message.Channel.Id, match.Member);
|
||||||
await _logger.LogMessage(match.System, match.Member, hookMessage, message.Author);
|
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
|
// Wait a second or so before deleting the original message
|
||||||
await Task.Delay(1000);
|
await Task.Delay(1000);
|
||||||
@ -131,7 +131,7 @@ namespace PluralKit.Bot
|
|||||||
return true;
|
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);
|
username = FixClyde(username);
|
||||||
|
|
||||||
@ -159,8 +159,9 @@ namespace PluralKit.Bot
|
|||||||
messageId = await client.SendMessageAsync(text, username: username, avatarUrl: avatarUrl);
|
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
|
// TODO: figure out a way to return the full message object (without doing a GetMessageAsync call, which
|
||||||
return await webhook.Channel.GetMessageAsync(messageId);
|
// doesn't work if there's no permission to)
|
||||||
|
return messageId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task HandleReactionAddedAsync(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction)
|
public Task HandleReactionAddedAsync(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction)
|
||||||
|
Loading…
Reference in New Issue
Block a user