feat: better command parsing for pk;config

This commit is contained in:
spiral 2022-01-19 18:59:44 -05:00
parent 426d753827
commit 1626e0f548
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
2 changed files with 48 additions and 48 deletions

View File

@ -99,6 +99,16 @@ public static class ContextArgumentsExt
public static bool MatchRaw(this Context ctx) =>
ctx.Match("r", "raw") || ctx.MatchFlag("r", "raw");
public static bool MatchToggle(this Context ctx)
{
if (ctx.Match("yes", "on", "enable"))
return true;
else if (ctx.Match("no", "off", "disable"))
return false;
else
throw new PKError("You must pass either \"on\" or \"off\" to this command.");
}
public static (ulong? messageId, ulong? channelId) MatchMessage(this Context ctx, bool parseRawMessageId)
{
if (ctx.Message.Type == Message.MessageType.Reply && ctx.Message.MessageReference?.MessageId != null)

View File

@ -122,27 +122,18 @@ public class Config
}
);
}
private string EnabledDisabled(bool value) => value ? "enabled" : "disabled";
public async Task AutoproxyAccount(Context ctx)
{
// todo: this might be useful elsewhere, consider moving it to ctx.MatchToggle
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
if (!ctx.HasNext())
{
var statusString = ctx.MessageContext.AllowAutoproxy ? "enabled" : "disabled";
await ctx.Reply($"Autoproxy is currently **{statusString}** for account <@{ctx.Author.Id}>.");
}
await ctx.Reply($"Autoproxy is currently **{EnabledDisabled(ctx.MessageContext.AllowAutoproxy)}** for account <@{ctx.Author.Id}>.");
return;
}
private string EnabledDisabled(bool value) => value ? "enabled" : "disabled";
var allow = ctx.MatchToggle();
private async Task AutoproxyEnableDisable(Context ctx, bool allow)
{
var statusString = EnabledDisabled(allow);
if (ctx.MessageContext.AllowAutoproxy == allow)
{
@ -202,32 +193,6 @@ public class Config
await ctx.Reply($"{Emojis.Success} Latch timeout set to {newTimeout.Value!.ToTimeSpan().Humanize(4)}.");
}
public async Task SystemPing(Context ctx)
{
ctx.CheckSystem();
if (!ctx.HasNext())
{
if (ctx.Config.PingsEnabled) { await ctx.Reply("Reaction pings are currently **enabled** for your system. To disable reaction pings, type `pk;config ping disable`."); }
else { await ctx.Reply("Reaction pings are currently **disabled** for your system. To enable reaction pings, type `pk;config ping enable`."); }
}
else
{
if (ctx.Match("on", "enable"))
{
await _repo.UpdateSystemConfig(ctx.System.Id, new() { PingsEnabled = true });
await ctx.Reply("Reaction pings have now been enabled.");
}
if (ctx.Match("off", "disable"))
{
await _repo.UpdateSystemConfig(ctx.System.Id, new() { PingsEnabled = false });
await ctx.Reply("Reaction pings have now been disabled.");
}
}
}
public async Task SystemTimezone(Context ctx)
{
if (ctx.System == null) throw Errors.NoSystemError;
@ -320,7 +285,33 @@ public class Config
});
}
// todo: this command parsing is really messy
public async Task SystemPing(Context ctx)
{
// note: this is here because this is also used in `pk;system ping`, which does not CheckSystem
ctx.CheckSystem();
// todo: move all the other config settings to this format
String Response(bool isError, bool val)
=> $"Reaction pings are {(isError ? "already" : "currently")} **{EnabledDisabled(val)}** for your system. "
+ $"To {EnabledDisabled(!val)[..^1]} reaction pings, type `pk;config ping {EnabledDisabled(!val)[..^1]}`.";
if (!ctx.HasNext())
{
await ctx.Reply(Response(false, ctx.Config.PingsEnabled));
return;
}
var value = ctx.MatchToggle();
if (ctx.Config.PingsEnabled == value)
await ctx.Reply(Response(true, ctx.Config.PingsEnabled));
else
{
await _repo.UpdateSystemConfig(ctx.System.Id, new() { PingsEnabled = value });
await ctx.Reply($"Reaction pings have now been {EnabledDisabled(value)}.");
}
}
public async Task MemberDefaultPrivacy(Context ctx)
{
@ -331,13 +322,13 @@ public class Config
}
else
{
if (ctx.Match("on", "enable"))
if (ctx.MatchToggle())
{
await _repo.UpdateSystemConfig(ctx.System.Id, new() { MemberDefaultPrivate = true });
await ctx.Reply("Newly created members will now have their privacy settings set to private.");
}
if (ctx.Match("off", "disable"))
else
{
await _repo.UpdateSystemConfig(ctx.System.Id, new() { MemberDefaultPrivate = false });
@ -355,13 +346,13 @@ public class Config
}
else
{
if (ctx.Match("on", "enable"))
if (ctx.MatchToggle())
{
await _repo.UpdateSystemConfig(ctx.System.Id, new() { GroupDefaultPrivate = true });
await ctx.Reply("Newly created groups will now have their privacy settings set to private.");
}
if (ctx.Match("off", "disable"))
else
{
await _repo.UpdateSystemConfig(ctx.System.Id, new() { GroupDefaultPrivate = false });
@ -379,18 +370,17 @@ public class Config
return;
}
if (ctx.Match("true"))
if (ctx.MatchToggle())
{
await _repo.UpdateSystemConfig(ctx.System.Id, new() { ShowPrivateInfo = true });
await ctx.Reply("Private information will now be **shown** when looking up your own info. Use the `-public` flag to hide it.");
}
else if (ctx.Match("false"))
else
{
await _repo.UpdateSystemConfig(ctx.System.Id, new() { ShowPrivateInfo = false });
await ctx.Reply("Private information will now be **hidden** when looking up your own info. Use the `-private` flag to show it.");
}
else throw new PKError("You must pass 'true' or 'false' to this command.");
}
}