Add per-server per-system proxy enable toggle

This commit is contained in:
Ske 2019-12-22 14:15:56 +01:00
parent 857b0488b9
commit 639c813ce7
5 changed files with 62 additions and 5 deletions

View File

@ -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<SystemCommands>(SystemDelete, m => m.Delete(ctx));
else if (ctx.Match("timezone", "tz"))
await ctx.Execute<SystemCommands>(SystemTimezone, m => m.SystemTimezone(ctx));
else if (ctx.Match("proxy"))
await ctx.Execute<SystemCommands>(SystemProxy, m => m.SystemProxy(ctx));
else if (ctx.Match("list", "l", "members"))
{
if (ctx.Match("f", "full", "big", "details", "long"))

View File

@ -198,6 +198,26 @@ namespace PluralKit.Bot.Commands
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)
{
if (ctx.System == null) throw Errors.NoSystemError;

View File

@ -91,6 +91,10 @@ namespace PluralKit.Bot
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;

View File

@ -64,11 +64,9 @@ namespace PluralKit {
public ISet<ulong> 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 {
/// <param name="system">The system to check in.</param>
Task<IEnumerable<PKMember>> GetConflictingProxies(PKSystem system, ProxyTag tag);
Task<SystemGuildSettings> GetSystemGuildSettings(PKSystem system, ulong guild);
Task SetGuildSystemSettings(PKSystem system, ulong guild, SystemGuildSettings settings);
/// <summary>
/// Creates a system, auto-generating its corresponding IDs.
/// </summary>
@ -384,6 +385,25 @@ namespace PluralKit {
});
}
public async Task<SystemGuildSettings> GetSystemGuildSettings(PKSystem system, ulong guild)
{
using (var conn = await _conn.Obtain())
return await conn.QuerySingleOrDefaultAsync<SystemGuildSettings>(
"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<PKSystem> CreateSystem(string systemName = null) {
string hid;
do

View File

@ -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,