Add disabling autoproxy per-account

This commit is contained in:
spiral 2020-11-20 18:34:08 -05:00
parent 41247c68a6
commit 37294b68da
8 changed files with 98 additions and 5 deletions

View File

@ -22,7 +22,17 @@ namespace PluralKit.Bot
public async Task AutoproxyRoot(Context ctx) public async Task AutoproxyRoot(Context ctx)
{ {
ctx.CheckSystem().CheckGuildContext(); ctx.CheckSystem();
// check account first
// this is ugly, but someone may want to disable autoproxy in DMs (since this is global)
if (ctx.Match("account", "ac"))
{
await AutoproxyAccount(ctx);
return;
}
ctx.CheckGuildContext();
if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove")) if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove"))
await AutoproxyOff(ctx); await AutoproxyOff(ctx);
@ -122,9 +132,40 @@ namespace PluralKit.Bot
default: throw new ArgumentOutOfRangeException(); default: throw new ArgumentOutOfRangeException();
} }
if (!ctx.MessageContext.AllowAutoproxy)
eb.AddField("\u200b", $"{Emojis.Note} Autoproxy is currently **disabled** for your account (<@{ctx.Author.Id}>). To enable it, use `pk;autoproxy account enable`.");
return eb.Build(); return eb.Build();
} }
private async Task AutoproxyAccount(Context ctx)
{
if (ctx.Match("enable", "on"))
await AutoproxyEnableDisable(ctx, true);
else if (ctx.Match("disable", "off"))
await AutoproxyEnableDisable(ctx, false);
else if (ctx.HasNext())
throw new PKSyntaxError("You must pass either \"on\" or \"off\".");
else
{
var statusString = ctx.MessageContext.AllowAutoproxy ? "enabled" : "disabled";
await ctx.Reply($"Autoproxy is currently **{statusString}** for account <@{ctx.Author.Id}>.", mentions: new IMention[]{});
}
}
private async Task AutoproxyEnableDisable(Context ctx, bool allow)
{
var statusString = allow ? "enabled" : "disabled";
if (ctx.MessageContext.AllowAutoproxy == allow)
{
await ctx.Reply($"{Emojis.Note} Autoproxy is already {statusString} for account <@{ctx.Author.Id}>.", mentions: new IMention[]{});
return;
}
var patch = new AccountPatch { AllowAutoproxy = allow };
await _db.Execute(conn => _repo.UpdateAccount(conn, ctx.Author.Id, patch));
await ctx.Reply($"{Emojis.Success} Autoproxy {statusString} for account <@{ctx.Author.Id}>.", mentions: new IMention[]{});
}
private Task UpdateAutoproxy(Context ctx, AutoproxyMode autoproxyMode, MemberId? autoproxyMember) private Task UpdateAutoproxy(Context ctx, AutoproxyMode autoproxyMode, MemberId? autoproxyMember)
{ {
var patch = new SystemGuildPatch {AutoproxyMode = autoproxyMode, AutoproxyMember = autoproxyMember}; var patch = new SystemGuildPatch {AutoproxyMode = autoproxyMode, AutoproxyMember = autoproxyMember};

View File

@ -137,7 +137,7 @@ namespace PluralKit.Bot
{ {
try try
{ {
return await _proxy.HandleIncomingMessage(shard, evt.Message, ctx, allowAutoproxy: true); return await _proxy.HandleIncomingMessage(shard, evt.Message, ctx, allowAutoproxy: ctx.AllowAutoproxy);
} }
catch (PKError e) catch (PKError e)
{ {

View File

@ -24,5 +24,6 @@ namespace PluralKit.Core
public Instant? LastSwitchTimestamp { get; } public Instant? LastSwitchTimestamp { get; }
public string? SystemTag { get; } public string? SystemTag { get; }
public string? SystemAvatar { get; } public string? SystemAvatar { get; }
public bool AllowAutoproxy { get; }
} }
} }

View File

@ -14,12 +14,13 @@
last_switch_members int[], last_switch_members int[],
last_switch_timestamp timestamp, last_switch_timestamp timestamp,
system_tag text, system_tag text,
system_avatar text system_avatar text,
allow_autoproxy bool
) )
as $$ as $$
-- CTEs to query "static" (accessible only through args) data -- CTEs to query "static" (accessible only through args) data
with with
system as (select systems.* from accounts inner join systems on systems.id = accounts.system where accounts.uid = account_id), system as (select systems.*, allow_autoproxy as account_autoproxy from accounts inner join systems on systems.id = accounts.system where accounts.uid = account_id),
guild as (select * from servers where id = guild_id), guild as (select * from servers where id = guild_id),
last_message as (select * from messages where messages.guild = guild_id and messages.sender = account_id order by mid desc limit 1) last_message as (select * from messages where messages.guild = guild_id and messages.sender = account_id order by mid desc limit 1)
select select
@ -37,7 +38,8 @@ as $$
system_last_switch.members as last_switch_members, system_last_switch.members as last_switch_members,
system_last_switch.timestamp as last_switch_timestamp, system_last_switch.timestamp as last_switch_timestamp,
system.tag as system_tag, system.tag as system_tag,
system.avatar_url as system_avatar system.avatar_url as system_avatar,
system.account_autoproxy as allow_autoproxy
-- We need a "from" clause, so we just use some bogus data that's always present -- We need a "from" clause, so we just use some bogus data that's always present
-- This ensure we always have exactly one row going forward, so we can left join afterwards and still get data -- This ensure we always have exactly one row going forward, so we can left join afterwards and still get data
from (select 1) as _placeholder from (select 1) as _placeholder

View File

@ -1,5 +1,7 @@
-- SCHEMA VERSION 12: <insert date> -- -- SCHEMA VERSION 12: <insert date> --
-- Add disabling front/latch autoproxy per-member -- -- Add disabling front/latch autoproxy per-member --
-- Add disabling autoproxy per-account --
alter table members add column allow_autoproxy bool not null default true; alter table members add column allow_autoproxy bool not null default true;
alter table accounts add column allow_autoproxy bool not null default true;
update info set schema_version = 12; update info set schema_version = 12;

View File

@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;
namespace PluralKit.Core
{
public partial class ModelRepository
{
public async Task UpdateAccount(IPKConnection conn, ulong id, AccountPatch patch)
{
_logger.Information("Updated account {accountId}: {@AccountPatch}", id, patch);
var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("accounts", "uid = @uid"))
.WithConstant("uid", id)
.Build();
await conn.ExecuteAsync(query, pms);
}
}
}

View File

@ -0,0 +1,10 @@
namespace PluralKit.Core
{
public class AccountPatch: PatchObject
{
public Partial<bool> AllowAutoproxy { get; set; }
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
.With("allow_autoproxy", AllowAutoproxy);
}
}

View File

@ -358,6 +358,22 @@ To re-enable front / latch modes for that member, use the following command:
This will *not* disable member mode autoproxy. If you do not wish to autoproxy, please turn off autoproxy instead of setting autoproxy to a specific member. This will *not* disable member mode autoproxy. If you do not wish to autoproxy, please turn off autoproxy instead of setting autoproxy to a specific member.
### Disabling autoproxy per-account
It is possible to fully disable autoproxy for a certain account linked to your system. For example, you might want to do this if a specific member's name is shown on the account.
To disable autoproxy for the current account, use the following command:
pk;autoproxy account disable
To re-enable autoproxy for the current account, use the following command:
pk;autoproxy account enable
::: tip
This subcommand can also be run in DMs.
:::
## Managing switches ## Managing switches
PluralKit allows you to log member switches through the bot. PluralKit allows you to log member switches through the bot.