Add configurable autoproxy latch timeout

This commit is contained in:
spiral
2020-11-20 19:44:15 -05:00
parent 37294b68da
commit 8e5fb6520b
8 changed files with 70 additions and 10 deletions

View File

@@ -25,12 +25,17 @@ namespace PluralKit.Bot
ctx.CheckSystem();
// 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"))
{
await AutoproxyAccount(ctx);
return;
}
else if (ctx.Match("timeout", "tm"))
{
await AutoproxyTimeout(ctx);
return;
}
ctx.CheckGuildContext();
@@ -138,6 +143,35 @@ namespace PluralKit.Bot
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)
{
if (ctx.Match("enable", "on"))

View File

@@ -10,7 +10,7 @@ namespace PluralKit.Bot
public class ProxyMatcher
{
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 ProxyTagParser _parser;
@@ -56,7 +56,7 @@ namespace PluralKit.Bot
AutoproxyMode.Front when ctx.LastSwitchMembers.Length > 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),
_ => null
@@ -75,11 +75,15 @@ namespace PluralKit.Bot
return true;
}
private bool IsLatchExpired(ulong? messageId)
private bool IsLatchExpired(MessageContext ctx)
{
if (messageId == null) return true;
var timestamp = DiscordUtils.SnowflakeToInstant(messageId.Value);
return _clock.GetCurrentInstant() - timestamp > LatchExpiryTime;
if (ctx.LastMessage == null) return true;
if (ctx.LatchTimeout == 0) return false;
var timeout = Duration.FromHours(ctx.LatchTimeout == -1 ? DefaultLatchExpiryTime : ctx.LatchTimeout);
var timestamp = DiscordUtils.SnowflakeToInstant(ctx.LastMessage.Value);
return _clock.GetCurrentInstant() - timestamp > timeout;
}
}
}