From 2c3c46002ab356654ab4132a97961488159c5c25 Mon Sep 17 00:00:00 2001 From: Ske Date: Fri, 21 Jun 2019 13:49:58 +0200 Subject: [PATCH] Add message lookup and log channel setting commands --- PluralKit.Bot/Commands/ModCommands.cs | 42 +++++++++++++++++++++ PluralKit.Bot/Errors.cs | 1 + PluralKit.Bot/Services/EmbedService.cs | 22 ++++++++++- PluralKit.Bot/Services/LogChannelService.cs | 14 +++---- PluralKit.Bot/Services/ProxyService.cs | 9 +++-- PluralKit.Core/Stores.cs | 23 +++++++---- 6 files changed, 92 insertions(+), 19 deletions(-) create mode 100644 PluralKit.Bot/Commands/ModCommands.cs diff --git a/PluralKit.Bot/Commands/ModCommands.cs b/PluralKit.Bot/Commands/ModCommands.cs new file mode 100644 index 00000000..8753bc9e --- /dev/null +++ b/PluralKit.Bot/Commands/ModCommands.cs @@ -0,0 +1,42 @@ +using System.Threading.Tasks; +using Discord; +using Discord.Commands; + +namespace PluralKit.Bot.Commands +{ + public class ModCommands: ModuleBase + { + public LogChannelService LogChannels { get; set; } + public MessageStore Messages { get; set; } + + public EmbedService Embeds { get; set; } + + [Command("log")] + [Remarks("log ")] + [RequireUserPermission(GuildPermission.ManageGuild, ErrorMessage = "You must have the Manage Server permission to use this command.")] + [RequireContext(ContextType.Guild, ErrorMessage = "This command can not be run in a DM.")] + public async Task SetLogChannel(ITextChannel channel = null) + { + await LogChannels.SetLogChannel(Context.Guild, channel); + + if (channel != null) + await Context.Channel.SendMessageAsync($"{Emojis.Success} Proxy logging channel set to #{channel.Name}."); + else + await Context.Channel.SendMessageAsync($"{Emojis.Success} Proxy logging channel cleared."); + } + + [Command("message")] + [Remarks("message ")] + public async Task GetMessage(ulong messageId) + { + var message = await Messages.Get(messageId); + if (message == null) throw Errors.MessageNotFound(messageId); + + await Context.Channel.SendMessageAsync(embed: await Embeds.CreateMessageInfoEmbed(messageId)); + } + + [Command("message")] + [Remarks("message ")] + public async Task GetMessage(IMessage msg) => await GetMessage(msg.Id); + } +} \ No newline at end of file diff --git a/PluralKit.Bot/Errors.cs b/PluralKit.Bot/Errors.cs index 46c737bb..0d2b78b5 100644 --- a/PluralKit.Bot/Errors.cs +++ b/PluralKit.Bot/Errors.cs @@ -64,5 +64,6 @@ namespace PluralKit.Bot { public static PKError NoImportFilePassed => new PKError("You must either pass an URL to a file as a command parameter, or as an attachment to the message containing the command."); public static PKError InvalidImportFile => new PKError("Imported data file invalid. Make sure this is a .json file directly exported from PluralKit or Tupperbox."); public static PKError ImportCancelled => new PKError("Import cancelled."); + public static PKError MessageNotFound(ulong id) => new PKError($"Message with ID '{id}' not found. Are you sure it's a message proxied by PluralKit?"); } } \ No newline at end of file diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index e7d6fb4c..bccfc4bc 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -10,14 +10,16 @@ namespace PluralKit.Bot { private SystemStore _systems; private MemberStore _members; private SwitchStore _switches; + private MessageStore _messages; private IDiscordClient _client; - public EmbedService(SystemStore systems, MemberStore members, IDiscordClient client, SwitchStore switches) + public EmbedService(SystemStore systems, MemberStore members, IDiscordClient client, SwitchStore switches, MessageStore messages) { _systems = systems; _members = members; _client = client; _switches = switches; + _messages = messages; } public async Task CreateSystemEmbed(PKSystem system) { @@ -117,5 +119,23 @@ namespace PluralKit.Bot { .WithDescription(outputStr) .Build(); } + + public async Task CreateMessageInfoEmbed(ulong messageId) + { + var msg = await _messages.Get(messageId); + var channel = (ITextChannel) await _client.GetChannelAsync(msg.Message.Channel); + var serverMsg = await channel.GetMessageAsync(msg.Message.Mid); + + var memberStr = $"{msg.Member.Name} (`{msg.Member.Hid}`)"; + if (msg.Member.Pronouns != null) memberStr += $"\n*(pronouns: **{msg.Member.Pronouns}**)*"; + + return new EmbedBuilder() + .WithAuthor(msg.Member.Name, msg.Member.AvatarUrl) + .WithDescription(serverMsg.Content) + .AddField("System", msg.System.Name != null ? $"{msg.System.Name} (`{msg.System.Hid}`)" : $"`{msg.System.Hid}`", true) + .AddField("Member", memberStr, true) + .WithTimestamp(SnowflakeUtils.FromSnowflake(msg.Message.Mid)) + .Build(); + } } } \ No newline at end of file diff --git a/PluralKit.Bot/Services/LogChannelService.cs b/PluralKit.Bot/Services/LogChannelService.cs index 9f7b55c0..b2e9ccaf 100644 --- a/PluralKit.Bot/Services/LogChannelService.cs +++ b/PluralKit.Bot/Services/LogChannelService.cs @@ -4,12 +4,12 @@ using Dapper; using Discord; namespace PluralKit.Bot { - class ServerDefinition { - public ulong Id; - public ulong LogChannel; + public class ServerDefinition { + public ulong Id { get; set; } + public ulong LogChannel { get; set; } } - class LogChannelService { + public class LogChannelService { private IDiscordClient _client; private IDbConnection _connection; private EmbedService _embed; @@ -30,7 +30,7 @@ namespace PluralKit.Bot { } public async Task GetLogChannel(IGuild guild) { - var server = await _connection.QueryFirstAsync("select * from servers where id = @Id", new { Id = guild.Id }); + var server = await _connection.QueryFirstOrDefaultAsync("select * from servers where id = @Id", new { Id = guild.Id }); if (server == null) return null; return await _client.GetChannelAsync(server.LogChannel) as ITextChannel; } @@ -40,8 +40,8 @@ namespace PluralKit.Bot { Id = guild.Id, LogChannel = newLogChannel.Id }; - - await _connection.ExecuteAsync("insert into servers(id, log_channel) values (@Id, @LogChannel) on conflict (id) do update set log_channel = @LogChannel", def); + + await _connection.QueryAsync("insert into servers (id, log_channel) values (@Id, @LogChannel)", def); } } } \ No newline at end of file diff --git a/PluralKit.Bot/Services/ProxyService.cs b/PluralKit.Bot/Services/ProxyService.cs index b58773f3..06b4d288 100644 --- a/PluralKit.Bot/Services/ProxyService.cs +++ b/PluralKit.Bot/Services/ProxyService.cs @@ -49,7 +49,10 @@ namespace PluralKit.Bot // Sort by specificity (ProxyString length desc = prefix+suffix length desc = inner message asc = more specific proxy first!) var ordered = potentials.OrderByDescending(p => p.Member.ProxyString.Length); - foreach (var potential in ordered) { + foreach (var potential in ordered) + { + if (potential.Member.Prefix == null && potential.Member.Suffix != null) continue; + var prefix = potential.Member.Prefix ?? ""; var suffix = potential.Member.Suffix ?? ""; @@ -62,7 +65,7 @@ namespace PluralKit.Bot } public async Task HandleMessageAsync(IMessage message) { - var results = await _connection.QueryAsync("select members.*, systems.* from members, systems, accounts where members.system = systems.id and accounts.system = systems.id and accounts.uid = @Uid and (members.prefix != null or members.suffix != null)", (member, system) => new ProxyDatabaseResult { Member = member, System = system }, new { Uid = message.Author.Id }); + var results = await _connection.QueryAsync("select members.*, systems.* from members, systems, accounts where members.system = systems.id and accounts.system = systems.id and accounts.uid = @Uid", (member, system) => new ProxyDatabaseResult { Member = member, System = system }, new { Uid = message.Author.Id }); // Find a member with proxy tags matching the message var match = GetProxyTagMatch(message.Content, results); @@ -105,7 +108,7 @@ namespace PluralKit.Bot 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 - if (storedMessage.SenderId != reaction.UserId) return; + if (storedMessage.Message.Sender != reaction.UserId) return; try { // Then, fetch the Discord message and delete that diff --git a/PluralKit.Core/Stores.cs b/PluralKit.Core/Stores.cs index 14cb6483..199dc27f 100644 --- a/PluralKit.Core/Stores.cs +++ b/PluralKit.Core/Stores.cs @@ -114,10 +114,15 @@ namespace PluralKit { } public class MessageStore { - public class StoredMessage { + public struct PKMessage + { public ulong Mid; - public ulong ChannelId; - public ulong SenderId; + public ulong Channel; + public ulong Sender; + } + public class StoredMessage + { + public PKMessage Message; public PKMember Member; public PKSystem System; } @@ -137,11 +142,13 @@ namespace PluralKit { }); } - public async Task Get(ulong id) { - return (await _connection.QueryAsync("select * from messages, members, systems where mid = @Id and messages.member = members.id and systems.id = members.system", (msg, member, system) => { - msg.System = system; - msg.Member = member; - return msg; + public async Task Get(ulong id) + { + return (await _connection.QueryAsync("select messages.*, members.*, systems.* from messages, members, systems where mid = @Id and messages.member = members.id and systems.id = members.system", (msg, member, system) => new StoredMessage + { + Message = msg, + System = system, + Member = member }, new { Id = id })).FirstOrDefault(); }