Add configurable autoproxy latch timeout
This commit is contained in:
parent
37294b68da
commit
8e5fb6520b
@ -25,12 +25,17 @@ namespace PluralKit.Bot
|
|||||||
ctx.CheckSystem();
|
ctx.CheckSystem();
|
||||||
|
|
||||||
// check account first
|
// check account first
|
||||||
// this is ugly, but someone may want to disable autoproxy in DMs (since this is global)
|
// this is ugly, but these global options should be available in DMs
|
||||||
if (ctx.Match("account", "ac"))
|
if (ctx.Match("account", "ac"))
|
||||||
{
|
{
|
||||||
await AutoproxyAccount(ctx);
|
await AutoproxyAccount(ctx);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
else if (ctx.Match("timeout", "tm"))
|
||||||
|
{
|
||||||
|
await AutoproxyTimeout(ctx);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ctx.CheckGuildContext();
|
ctx.CheckGuildContext();
|
||||||
|
|
||||||
@ -138,6 +143,35 @@ namespace PluralKit.Bot
|
|||||||
return eb.Build();
|
return eb.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task AutoproxyTimeout(Context ctx)
|
||||||
|
{
|
||||||
|
if (!ctx.HasNext())
|
||||||
|
{
|
||||||
|
if (ctx.System.LatchTimeout == -1)
|
||||||
|
await ctx.Reply($"You do not have a custom autoproxy timeout duration set. The default latch timeout duration is {PluralKit.Bot.ProxyMatcher.DefaultLatchExpiryTime} hour(s).");
|
||||||
|
else if (ctx.System.LatchTimeout == 0)
|
||||||
|
await ctx.Reply("Latch timeout is currently **disabled** for your system. Latch mode autoproxy will never timeout.");
|
||||||
|
else
|
||||||
|
await ctx.Reply($"The current latch timeout duration for your system is {ctx.System.LatchTimeout} hour(s).");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo: somehow parse a more human-friendly date format
|
||||||
|
int newTimeout;
|
||||||
|
if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove")) newTimeout = 0;
|
||||||
|
else if (ctx.Match("reset", "default")) newTimeout = -1;
|
||||||
|
else if (!int.TryParse(ctx.RemainderOrNull(), out newTimeout)) throw new PKError("Duration must be an integer.");
|
||||||
|
|
||||||
|
await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, new SystemPatch{LatchTimeout = newTimeout}));
|
||||||
|
|
||||||
|
if (newTimeout == -1)
|
||||||
|
await ctx.Reply($"{Emojis.Success} Latch timeout reset to default ({PluralKit.Bot.ProxyMatcher.DefaultLatchExpiryTime} hours).");
|
||||||
|
else if (newTimeout == 0)
|
||||||
|
await ctx.Reply($"{Emojis.Success} Latch timeout disabled. Latch mode autoproxy will never timeout.");
|
||||||
|
else
|
||||||
|
await ctx.Reply($"{Emojis.Success} Latch timeout set to {newTimeout} hours.");
|
||||||
|
}
|
||||||
|
|
||||||
private async Task AutoproxyAccount(Context ctx)
|
private async Task AutoproxyAccount(Context ctx)
|
||||||
{
|
{
|
||||||
if (ctx.Match("enable", "on"))
|
if (ctx.Match("enable", "on"))
|
||||||
|
@ -10,7 +10,7 @@ namespace PluralKit.Bot
|
|||||||
public class ProxyMatcher
|
public class ProxyMatcher
|
||||||
{
|
{
|
||||||
private static readonly char AutoproxyEscapeCharacter = '\\';
|
private static readonly char AutoproxyEscapeCharacter = '\\';
|
||||||
private static readonly Duration LatchExpiryTime = Duration.FromHours(6);
|
public static readonly int DefaultLatchExpiryTime = 6;
|
||||||
|
|
||||||
private readonly IClock _clock;
|
private readonly IClock _clock;
|
||||||
private readonly ProxyTagParser _parser;
|
private readonly ProxyTagParser _parser;
|
||||||
@ -56,7 +56,7 @@ namespace PluralKit.Bot
|
|||||||
AutoproxyMode.Front when ctx.LastSwitchMembers.Length > 0 =>
|
AutoproxyMode.Front when ctx.LastSwitchMembers.Length > 0 =>
|
||||||
members.FirstOrDefault(m => m.Id == ctx.LastSwitchMembers[0]),
|
members.FirstOrDefault(m => m.Id == ctx.LastSwitchMembers[0]),
|
||||||
|
|
||||||
AutoproxyMode.Latch when ctx.LastMessageMember != null && !IsLatchExpired(ctx.LastMessage) =>
|
AutoproxyMode.Latch when ctx.LastMessageMember != null && !IsLatchExpired(ctx) =>
|
||||||
members.FirstOrDefault(m => m.Id == ctx.LastMessageMember.Value),
|
members.FirstOrDefault(m => m.Id == ctx.LastMessageMember.Value),
|
||||||
|
|
||||||
_ => null
|
_ => null
|
||||||
@ -75,11 +75,15 @@ namespace PluralKit.Bot
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsLatchExpired(ulong? messageId)
|
private bool IsLatchExpired(MessageContext ctx)
|
||||||
{
|
{
|
||||||
if (messageId == null) return true;
|
if (ctx.LastMessage == null) return true;
|
||||||
var timestamp = DiscordUtils.SnowflakeToInstant(messageId.Value);
|
if (ctx.LatchTimeout == 0) return false;
|
||||||
return _clock.GetCurrentInstant() - timestamp > LatchExpiryTime;
|
|
||||||
|
var timeout = Duration.FromHours(ctx.LatchTimeout == -1 ? DefaultLatchExpiryTime : ctx.LatchTimeout);
|
||||||
|
|
||||||
|
var timestamp = DiscordUtils.SnowflakeToInstant(ctx.LastMessage.Value);
|
||||||
|
return _clock.GetCurrentInstant() - timestamp > timeout;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -25,5 +25,6 @@ namespace PluralKit.Core
|
|||||||
public string? SystemTag { get; }
|
public string? SystemTag { get; }
|
||||||
public string? SystemAvatar { get; }
|
public string? SystemAvatar { get; }
|
||||||
public bool AllowAutoproxy { get; }
|
public bool AllowAutoproxy { get; }
|
||||||
|
public int LatchTimeout { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,7 +15,8 @@
|
|||||||
last_switch_timestamp timestamp,
|
last_switch_timestamp timestamp,
|
||||||
system_tag text,
|
system_tag text,
|
||||||
system_avatar text,
|
system_avatar text,
|
||||||
allow_autoproxy bool
|
allow_autoproxy bool,
|
||||||
|
latch_timeout integer
|
||||||
)
|
)
|
||||||
as $$
|
as $$
|
||||||
-- CTEs to query "static" (accessible only through args) data
|
-- CTEs to query "static" (accessible only through args) data
|
||||||
@ -39,7 +40,8 @@ as $$
|
|||||||
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
|
system.account_autoproxy as allow_autoproxy,
|
||||||
|
system.latch_timeout as latch_timeout
|
||||||
-- 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
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
-- 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 --
|
-- Add disabling autoproxy per-account --
|
||||||
|
-- Add configurable latch timeout --
|
||||||
|
|
||||||
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;
|
alter table accounts add column allow_autoproxy bool not null default true;
|
||||||
|
alter table systems add column latch_timeout int not null default -1;
|
||||||
update info set schema_version = 12;
|
update info set schema_version = 12;
|
@ -18,6 +18,7 @@ namespace PluralKit.Core {
|
|||||||
public Instant Created { get; }
|
public Instant Created { get; }
|
||||||
public string UiTz { get; set; }
|
public string UiTz { get; set; }
|
||||||
public bool PingsEnabled { get; }
|
public bool PingsEnabled { get; }
|
||||||
|
public int LatchTimeout { get; }
|
||||||
public PrivacyLevel DescriptionPrivacy { get; }
|
public PrivacyLevel DescriptionPrivacy { get; }
|
||||||
public PrivacyLevel MemberListPrivacy { get;}
|
public PrivacyLevel MemberListPrivacy { get;}
|
||||||
public PrivacyLevel FrontPrivacy { get; }
|
public PrivacyLevel FrontPrivacy { get; }
|
||||||
|
@ -15,6 +15,7 @@ namespace PluralKit.Core
|
|||||||
public Partial<PrivacyLevel> FrontPrivacy { get; set; }
|
public Partial<PrivacyLevel> FrontPrivacy { get; set; }
|
||||||
public Partial<PrivacyLevel> FrontHistoryPrivacy { get; set; }
|
public Partial<PrivacyLevel> FrontHistoryPrivacy { get; set; }
|
||||||
public Partial<bool> PingsEnabled { get; set; }
|
public Partial<bool> PingsEnabled { get; set; }
|
||||||
|
public Partial<int> LatchTimeout { get; set; }
|
||||||
|
|
||||||
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
|
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
|
||||||
.With("name", Name)
|
.With("name", Name)
|
||||||
@ -28,6 +29,7 @@ namespace PluralKit.Core
|
|||||||
.With("group_list_privacy", GroupListPrivacy)
|
.With("group_list_privacy", GroupListPrivacy)
|
||||||
.With("front_privacy", FrontPrivacy)
|
.With("front_privacy", FrontPrivacy)
|
||||||
.With("front_history_privacy", FrontHistoryPrivacy)
|
.With("front_history_privacy", FrontHistoryPrivacy)
|
||||||
.With("pings_enabled", PingsEnabled);
|
.With("pings_enabled", PingsEnabled)
|
||||||
|
.With("latch_timeout", LatchTimeout);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -340,6 +340,20 @@ To enable latch-mode autoproxying for a given server, use the following command:
|
|||||||
|
|
||||||
pk;autoproxy latch
|
pk;autoproxy latch
|
||||||
|
|
||||||
|
::: tip
|
||||||
|
By default, latch mode times out after 6 hours. It is possible to change this:
|
||||||
|
|
||||||
|
pk;autoproxy timeout <new duration>
|
||||||
|
|
||||||
|
To reset the duration, use the following command:
|
||||||
|
|
||||||
|
pk;autoproxy timeout reset
|
||||||
|
|
||||||
|
To disable timeout (never timeout), use the following command:
|
||||||
|
|
||||||
|
pk;autoproxy timeout disable
|
||||||
|
:::
|
||||||
|
|
||||||
#### Member mode
|
#### Member mode
|
||||||
This autoproxy mode will autoproxy for a specific selected member, irrelevant of past proxies or fronters.
|
This autoproxy mode will autoproxy for a specific selected member, irrelevant of past proxies or fronters.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user