From 639c813ce7861407b9b1788cc12a5eb4dc3d30bf Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 22 Dec 2019 14:15:56 +0100 Subject: [PATCH] Add per-server per-system proxy enable toggle --- PluralKit.Bot/Commands/CommandTree.cs | 3 +++ PluralKit.Bot/Commands/SystemCommands.cs | 20 +++++++++++++++++ PluralKit.Bot/Services/ProxyService.cs | 6 ++++- PluralKit.Core/Stores.cs | 28 ++++++++++++++++++++---- PluralKit.Core/db_schema.sql | 10 +++++++++ 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/PluralKit.Bot/Commands/CommandTree.cs b/PluralKit.Bot/Commands/CommandTree.cs index 263e99f7..8b138746 100644 --- a/PluralKit.Bot/Commands/CommandTree.cs +++ b/PluralKit.Bot/Commands/CommandTree.cs @@ -17,6 +17,7 @@ namespace PluralKit.Bot.Commands public static Command SystemAvatar = new Command("system avatar", "system avatar [url|@mention]", "Changes your system's avatar"); public static Command SystemDelete = new Command("system delete", "system delete", "Deletes your system"); public static Command SystemTimezone = new Command("system timezone", "system timezone [timezone]", "Changes your system's time zone"); + public static Command SystemProxy = new Command("system proxy", "system proxy [on|off]", "Enables or disables message proxying in a specific server"); public static Command SystemList = new Command("system list", "system [system] list [full]", "Lists a system's members"); public static Command SystemFronter = new Command("system fronter", "system [system] fronter", "Shows a system's fronter(s)"); public static Command SystemFrontHistory = new Command("system fronthistory", "system [system] fronthistory", "Shows a system's front history"); @@ -156,6 +157,8 @@ namespace PluralKit.Bot.Commands await ctx.Execute(SystemDelete, m => m.Delete(ctx)); else if (ctx.Match("timezone", "tz")) await ctx.Execute(SystemTimezone, m => m.SystemTimezone(ctx)); + else if (ctx.Match("proxy")) + await ctx.Execute(SystemProxy, m => m.SystemProxy(ctx)); else if (ctx.Match("list", "l", "members")) { if (ctx.Match("f", "full", "big", "details", "long")) diff --git a/PluralKit.Bot/Commands/SystemCommands.cs b/PluralKit.Bot/Commands/SystemCommands.cs index d4f2e72d..ac4094c0 100644 --- a/PluralKit.Bot/Commands/SystemCommands.cs +++ b/PluralKit.Bot/Commands/SystemCommands.cs @@ -197,6 +197,26 @@ namespace PluralKit.Bot.Commands var frontpercent = await _data.GetFrontBreakdown(system, rangeStart.Value.ToInstant(), now); await ctx.Reply(embed: await _embeds.CreateFrontPercentEmbed(frontpercent, system.Zone)); } + + public async Task SystemProxy(Context ctx) + { + ctx.CheckSystem().CheckGuildContext(); + var gs = await _data.GetSystemGuildSettings(ctx.System, ctx.Guild.Id); + + bool newValue; + if (ctx.Match("on", "enabled", "true", "yes")) newValue = true; + else if (ctx.Match("off", "disabled", "false", "no")) newValue = false; + else if (ctx.HasNext()) throw new PKSyntaxError("You must pass either \"on\" or \"off\"."); + else newValue = !gs.ProxyEnabled; + + gs.ProxyEnabled = newValue; + await _data.SetGuildSystemSettings(ctx.System, ctx.Guild.Id, gs); + + if (newValue) + await ctx.Reply($"Message proxying in this server ({ctx.Guild.Name.EscapeMarkdown()}) is now **enabled** for your system."); + else + await ctx.Reply($"Message proxying in this server ({ctx.Guild.Name.EscapeMarkdown()}) is now **disabled** for your system."); + } public async Task SystemTimezone(Context ctx) { diff --git a/PluralKit.Bot/Services/ProxyService.cs b/PluralKit.Bot/Services/ProxyService.cs index 65e62760..208979ee 100644 --- a/PluralKit.Bot/Services/ProxyService.cs +++ b/PluralKit.Bot/Services/ProxyService.cs @@ -90,11 +90,15 @@ namespace PluralKit.Bot // And make sure the channel's not blacklisted from proxying. var guildCfg = await _data.GetOrCreateGuildConfig(channel.GuildId); if (guildCfg.Blacklist.Contains(channel.Id)) return; + + // Make sure the system hasn't blacklisted the guild either + var systemGuildCfg = await _data.GetSystemGuildSettings(match.System, channel.GuildId); + if (!systemGuildCfg.ProxyEnabled) return; // We know message.Channel can only be ITextChannel as PK doesn't work in DMs/groups // Afterwards we ensure the bot has the right permissions, otherwise bail early if (!await EnsureBotPermissions(channel)) return; - + // Can't proxy a message with no content and no attachment if (match.InnerText.Trim().Length == 0 && message.Attachments.Count == 0) return; diff --git a/PluralKit.Core/Stores.cs b/PluralKit.Core/Stores.cs index 5015ee54..4329f687 100644 --- a/PluralKit.Core/Stores.cs +++ b/PluralKit.Core/Stores.cs @@ -64,11 +64,9 @@ namespace PluralKit { public ISet Blacklist { get; set; } } - public struct ChannelConfig + public class SystemGuildSettings { - public ulong Id { get; set; } - public bool OnList { get; set; } - public bool LogMessages { get; set; } + public bool ProxyEnabled { get; set; } = true; } public interface IDataStore @@ -118,6 +116,9 @@ namespace PluralKit { /// The system to check in. Task> GetConflictingProxies(PKSystem system, ProxyTag tag); + Task GetSystemGuildSettings(PKSystem system, ulong guild); + Task SetGuildSystemSettings(PKSystem system, ulong guild, SystemGuildSettings settings); + /// /// Creates a system, auto-generating its corresponding IDs. /// @@ -384,6 +385,25 @@ namespace PluralKit { }); } + public async Task GetSystemGuildSettings(PKSystem system, ulong guild) + { + using (var conn = await _conn.Obtain()) + return await conn.QuerySingleOrDefaultAsync( + "select * from system_guild where system = @System and guild = @Guild", + new {System = system.Id, Guild = guild}) ?? new SystemGuildSettings(); + } + + public async Task SetGuildSystemSettings(PKSystem system, ulong guild, SystemGuildSettings settings) + { + using (var conn = await _conn.Obtain()) + await conn.ExecuteAsync("insert into system_guild (system, guild, proxy_enabled) values (@System, @Guild, @ProxyEnabled) on conflict (system, guild) do update set proxy_enabled = @ProxyEnabled", new + { + System = system.Id, + Guild = guild, + ProxyEnabled = settings.ProxyEnabled + }); + } + public async Task CreateSystem(string systemName = null) { string hid; do diff --git a/PluralKit.Core/db_schema.sql b/PluralKit.Core/db_schema.sql index e3e8d016..95b7b12d 100644 --- a/PluralKit.Core/db_schema.sql +++ b/PluralKit.Core/db_schema.sql @@ -20,6 +20,16 @@ create table if not exists systems ui_tz text not null default 'UTC' ); +create table if not exists system_guild +( + system serial not null references systems (id) on delete cascade, + guild bigint not null, + + proxy_enabled bool not null default true, + + primary key (system, guild) +); + create table if not exists members ( id serial primary key,