Merge pull request #211 from dev-kittens/random-stuff

Add -all flag to pk;random
This commit is contained in:
Astrid 2020-08-12 17:15:55 +02:00 committed by GitHub
commit 801db3c274
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View File

@ -40,7 +40,8 @@ namespace PluralKit.Bot
if (ctx.MatchFlag("by-last-fronted", "by-last-front", "by-last-switch", "blf", "bls")) p.SortProperty = SortProperty.LastSwitch; if (ctx.MatchFlag("by-last-fronted", "by-last-front", "by-last-switch", "blf", "bls")) p.SortProperty = SortProperty.LastSwitch;
if (ctx.MatchFlag("by-last-message", "blm", "blp")) p.SortProperty = SortProperty.LastMessage; if (ctx.MatchFlag("by-last-message", "blm", "blp")) p.SortProperty = SortProperty.LastMessage;
if (ctx.MatchFlag("by-birthday", "by-birthdate", "bbd")) p.SortProperty = SortProperty.Birthdate; if (ctx.MatchFlag("by-birthday", "by-birthdate", "bbd")) p.SortProperty = SortProperty.Birthdate;
if (ctx.MatchFlag("random")) p.SortProperty = SortProperty.Random;
// Sort reverse? // Sort reverse?
if (ctx.MatchFlag("r", "rev", "reverse")) if (ctx.MatchFlag("r", "rev", "reverse"))
p.Reverse = true; p.Reverse = true;

View File

@ -28,7 +28,8 @@ namespace PluralKit.Bot
public string CreateFilterString() public string CreateFilterString()
{ {
var str = new StringBuilder(); var str = new StringBuilder();
str.Append("Sorting by "); str.Append("Sorting ");
if (SortProperty != SortProperty.Random) str.Append("by ");
str.Append(SortProperty switch str.Append(SortProperty switch
{ {
SortProperty.Name => "member name", SortProperty.Name => "member name",
@ -39,6 +40,7 @@ namespace PluralKit.Bot
SortProperty.LastSwitch => "last switch", SortProperty.LastSwitch => "last switch",
SortProperty.MessageCount => "message count", SortProperty.MessageCount => "message count",
SortProperty.Birthdate => "birthday", SortProperty.Birthdate => "birthday",
SortProperty.Random => "randomly",
_ => new ArgumentOutOfRangeException($"Couldn't find readable string for sort property {SortProperty}") _ => new ArgumentOutOfRangeException($"Couldn't find readable string for sort property {SortProperty}")
}); });
@ -75,6 +77,8 @@ namespace PluralKit.Bot
IComparer<T> ReverseMaybe<T>(IComparer<T> c) => IComparer<T> ReverseMaybe<T>(IComparer<T> c) =>
opts.Reverse ? Comparer<T>.Create((a, b) => c.Compare(b, a)) : c; opts.Reverse ? Comparer<T>.Create((a, b) => c.Compare(b, a)) : c;
var randGen = new global::System.Random();
var culture = StringComparer.InvariantCultureIgnoreCase; var culture = StringComparer.InvariantCultureIgnoreCase;
return (opts.SortProperty switch return (opts.SortProperty switch
{ {
@ -96,6 +100,8 @@ namespace PluralKit.Bot
SortProperty.LastSwitch => input SortProperty.LastSwitch => input
.OrderByDescending(m => m.LastSwitchTime.HasValue) .OrderByDescending(m => m.LastSwitchTime.HasValue)
.ThenByDescending(m => m.LastSwitchTime, ReverseMaybe(Comparer<Instant?>.Default)), .ThenByDescending(m => m.LastSwitchTime, ReverseMaybe(Comparer<Instant?>.Default)),
SortProperty.Random => input
.OrderBy(m => randGen.Next()),
_ => throw new ArgumentOutOfRangeException($"Unknown sort property {opts.SortProperty}") _ => throw new ArgumentOutOfRangeException($"Unknown sort property {opts.SortProperty}")
}) })
// Lastly, add a by-name fallback order for collisions (generally hits w/ lots of null values) // Lastly, add a by-name fallback order for collisions (generally hits w/ lots of null values)
@ -112,7 +118,8 @@ namespace PluralKit.Bot
CreationDate, CreationDate,
LastSwitch, LastSwitch,
LastMessage, LastMessage,
Birthdate Birthdate,
Random
} }
public enum ListType public enum ListType

View File

@ -1,5 +1,6 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic;
using PluralKit.Core; using PluralKit.Core;
@ -61,7 +62,11 @@ namespace PluralKit.Bot
//Maybe move this somewhere else in the file structure since it doesn't need to get created at every command //Maybe move this somewhere else in the file structure since it doesn't need to get created at every command
// TODO: don't buffer these, find something else to do ig // TODO: don't buffer these, find something else to do ig
var members = await _data.GetSystemMembers(ctx.System).Where(m => m.MemberVisibility == PrivacyLevel.Public).ToListAsync();
List<PKMember> members;
if (ctx.MatchFlag("all", "a")) members = await _data.GetSystemMembers(ctx.System).ToListAsync();
else members = await _data.GetSystemMembers(ctx.System).Where(m => m.MemberVisibility == PrivacyLevel.Public).ToListAsync();
if (members == null || !members.Any()) if (members == null || !members.Any())
throw Errors.NoMembersError; throw Errors.NoMembersError;
var randInt = randGen.Next(members.Count); var randInt = randGen.Next(members.Count);