fix(bot): throw error if user requests incompatible list options

current behaviour is to silently ignore the incompatible ones, but this will make it more obvious as to why what they're trying to do is not working
This commit is contained in:
spiral 2022-09-22 17:17:53 +00:00
parent ba32038c28
commit 5aa8d45139
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
2 changed files with 47 additions and 6 deletions

View File

@ -76,11 +76,18 @@ public static class ContextListExt
if (ctx.MatchFlag("with-displayname", "wdn"))
p.IncludeDisplayName = true;
// Always show the sort property, too
if (p.SortProperty == SortProperty.LastSwitch) p.IncludeLastSwitch = true;
if (p.SortProperty == SortProperty.LastMessage) p.IncludeLastMessage = true;
// Always show the sort property, too (unless this is the short list)
if (p.Type != ListType.Short)
{
if (p.SortProperty == SortProperty.DisplayName) p.IncludeDisplayName = true;
if (p.SortProperty == SortProperty.MessageCount) p.IncludeMessageCount = true;
if (p.SortProperty == SortProperty.CreationDate) p.IncludeCreated = true;
if (p.SortProperty == SortProperty.LastSwitch) p.IncludeLastSwitch = true;
if (p.SortProperty == SortProperty.LastMessage) p.IncludeLastMessage = true;
}
// Make sure the options are valid
p.AssertIsValid();
// Done!
return p;

View File

@ -11,7 +11,20 @@ namespace PluralKit.Bot;
public class ListOptions
{
public SortProperty SortProperty { get; set; } = SortProperty.Name;
private SortProperty? _sortProperty { get; set; }
public SortProperty SortProperty
{
get => _sortProperty ?? SortProperty.Name;
set
{
if (_sortProperty != null)
throw new PKError("Cannot sort in multiple ways at the same time. Please choose only one sorting method.");
_sortProperty = value;
}
}
public bool Reverse { get; set; }
public PrivacyLevel? PrivacyFilter { get; set; } = PrivacyLevel.Public;
@ -148,6 +161,27 @@ public static class ListOptionsExt
// Lastly, add a by-name fallback order for collisions (generally hits w/ lots of null values)
.ThenBy(g => g.NameFor(ctx), culture);
}
public static void AssertIsValid(this ListOptions opts)
{
if (opts.Type == ListType.Short)
{
var hasMultipleIncluded = new[] {
opts.IncludeMessageCount,
opts.IncludeLastSwitch,
opts.IncludeLastMessage,
opts.IncludeCreated,
opts.IncludeAvatar,
opts.IncludePronouns,
opts.IncludeDisplayName,
}.Sum(x => Convert.ToInt32(x)) > 0;
if (hasMultipleIncluded)
throw new PKError("The short list does not support showing items from multiple flags. Try using the full list instead.");
}
// the check for multiple *sorting* property flags is done in SortProperty setter
}
}
public enum SortProperty