Add message querying by ? reaction

This commit is contained in:
Ske 2019-06-21 14:13:56 +02:00
parent 93fff14053
commit 53037f7d52
3 changed files with 38 additions and 15 deletions

View File

@ -32,7 +32,7 @@ namespace PluralKit.Bot.Commands
var message = await Messages.Get(messageId); var message = await Messages.Get(messageId);
if (message == null) throw Errors.MessageNotFound(messageId); if (message == null) throw Errors.MessageNotFound(messageId);
await Context.Channel.SendMessageAsync(embed: await Embeds.CreateMessageInfoEmbed(messageId)); await Context.Channel.SendMessageAsync(embed: await Embeds.CreateMessageInfoEmbed(message));
} }
[Command("message")] [Command("message")]

View File

@ -120,9 +120,8 @@ namespace PluralKit.Bot {
.Build(); .Build();
} }
public async Task<Embed> CreateMessageInfoEmbed(ulong messageId) public async Task<Embed> CreateMessageInfoEmbed(MessageStore.StoredMessage msg)
{ {
var msg = await _messages.Get(messageId);
var channel = (ITextChannel) await _client.GetChannelAsync(msg.Message.Channel); var channel = (ITextChannel) await _client.GetChannelAsync(msg.Message.Channel);
var serverMsg = await channel.GetMessageAsync(msg.Message.Mid); var serverMsg = await channel.GetMessageAsync(msg.Message.Mid);
@ -131,7 +130,7 @@ namespace PluralKit.Bot {
return new EmbedBuilder() return new EmbedBuilder()
.WithAuthor(msg.Member.Name, msg.Member.AvatarUrl) .WithAuthor(msg.Member.Name, msg.Member.AvatarUrl)
.WithDescription(serverMsg.Content) .WithDescription(serverMsg?.Content ?? "*(message contents deleted or inaccessible)*")
.AddField("System", msg.System.Name != null ? $"{msg.System.Name} (`{msg.System.Hid}`)" : $"`{msg.System.Hid}`", true) .AddField("System", msg.System.Name != null ? $"{msg.System.Name} (`{msg.System.Hid}`)" : $"`{msg.System.Hid}`", true)
.AddField("Member", memberStr, true) .AddField("Member", memberStr, true)
.WithTimestamp(SnowflakeUtils.FromSnowflake(msg.Message.Mid)) .WithTimestamp(SnowflakeUtils.FromSnowflake(msg.Message.Mid))

View File

@ -33,15 +33,16 @@ namespace PluralKit.Bot
private LogChannelService _logger; private LogChannelService _logger;
private WebhookCacheService _webhookCache; private WebhookCacheService _webhookCache;
private MessageStore _messageStorage; private MessageStore _messageStorage;
private EmbedService _embeds;
public ProxyService(IDiscordClient client, WebhookCacheService webhookCache, IDbConnection connection, LogChannelService logger, MessageStore messageStorage) public ProxyService(IDiscordClient client, WebhookCacheService webhookCache, IDbConnection connection, LogChannelService logger, MessageStore messageStorage, EmbedService embeds)
{ {
this._client = client; _client = client;
this._webhookCache = webhookCache; _webhookCache = webhookCache;
this._connection = connection; _connection = connection;
this._logger = logger; _logger = logger;
this._messageStorage = messageStorage; _messageStorage = messageStorage;
_embeds = embeds;
} }
private ProxyMatch GetProxyTagMatch(string message, IEnumerable<ProxyDatabaseResult> potentials) { private ProxyMatch GetProxyTagMatch(string message, IEnumerable<ProxyDatabaseResult> potentials) {
@ -98,17 +99,40 @@ namespace PluralKit.Bot
return await webhook.Channel.GetMessageAsync(messageId); return await webhook.Channel.GetMessageAsync(messageId);
} }
public async Task HandleReactionAddedAsync(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction) public Task HandleReactionAddedAsync(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction)
{ {
// Make sure it's the right emoji (red X) // Dispatch on emoji
if (reaction.Emote.Name != "\u274C") return; switch (reaction.Emote.Name)
{
case "\u274C": // Red X
return HandleMessageDeletionByReaction(message, reaction.UserId);
case "\u2753": // Red question mark
case "\u2754": // White question mark
return HandleMessageQueryByReaction(message, reaction.UserId);
default:
return Task.CompletedTask;
}
}
private async Task HandleMessageQueryByReaction(Cacheable<IUserMessage, ulong> message, ulong userWhoReacted)
{
var user = await _client.GetUserAsync(userWhoReacted);
if (user == null) return;
var msg = await _messageStorage.Get(message.Id);
if (msg == null) return;
await user.SendMessageAsync(embed: await _embeds.CreateMessageInfoEmbed(msg));
}
public async Task HandleMessageDeletionByReaction(Cacheable<IUserMessage, ulong> message, ulong userWhoReacted)
{
// Find the message in the database // Find the message in the database
var storedMessage = await _messageStorage.Get(message.Id); var storedMessage = await _messageStorage.Get(message.Id);
if (storedMessage == null) return; // (if we can't, that's ok, no worries) if (storedMessage == null) return; // (if we can't, that's ok, no worries)
// Make sure it's the actual sender of that message deleting the message // Make sure it's the actual sender of that message deleting the message
if (storedMessage.Message.Sender != reaction.UserId) return; if (storedMessage.Message.Sender != userWhoReacted) return;
try { try {
// Then, fetch the Discord message and delete that // Then, fetch the Discord message and delete that