feat: add pk;random for other systems
This commit is contained in:
@@ -15,48 +15,58 @@ public class Random
|
||||
|
||||
// todo: get postgresql to return one random member/group instead of querying all members/groups
|
||||
|
||||
public async Task Member(Context ctx)
|
||||
public async Task Member(Context ctx, PKSystem target)
|
||||
{
|
||||
ctx.CheckSystem();
|
||||
if (target == null)
|
||||
throw Errors.NoSystemError;
|
||||
|
||||
var members = await ctx.Repository.GetSystemMembers(ctx.System.Id).ToListAsync();
|
||||
ctx.CheckSystemPrivacy(target.Id, target.MemberListPrivacy);
|
||||
|
||||
var members = await ctx.Repository.GetSystemMembers(target.Id).ToListAsync();
|
||||
|
||||
if (!ctx.MatchFlag("all", "a"))
|
||||
members = members.Where(m => m.MemberVisibility == PrivacyLevel.Public).ToList();
|
||||
else
|
||||
ctx.CheckOwnSystem(target);
|
||||
|
||||
if (members == null || !members.Any())
|
||||
throw new PKError(
|
||||
"Your system has no members! Please create at least one member before using this command.");
|
||||
|
||||
var randInt = randGen.Next(members.Count);
|
||||
await ctx.Reply(embed: await _embeds.CreateMemberEmbed(ctx.System, members[randInt], ctx.Guild,
|
||||
ctx.LookupContextFor(ctx.System.Id), ctx.Zone));
|
||||
await ctx.Reply(embed: await _embeds.CreateMemberEmbed(target, members[randInt], ctx.Guild,
|
||||
ctx.LookupContextFor(target.Id), ctx.Zone));
|
||||
}
|
||||
|
||||
public async Task Group(Context ctx)
|
||||
public async Task Group(Context ctx, PKSystem target)
|
||||
{
|
||||
ctx.CheckSystem();
|
||||
if (target == null)
|
||||
throw Errors.NoSystemError;
|
||||
|
||||
ctx.CheckSystemPrivacy(target.Id, target.GroupListPrivacy);
|
||||
|
||||
var groups = await ctx.Repository.GetSystemGroups(ctx.System.Id).ToListAsync();
|
||||
var groups = await ctx.Repository.GetSystemGroups(target.Id).ToListAsync();
|
||||
if (!ctx.MatchFlag("all", "a"))
|
||||
groups = groups.Where(g => g.Visibility == PrivacyLevel.Public).ToList();
|
||||
else
|
||||
ctx.CheckOwnSystem(target);
|
||||
|
||||
if (groups == null || !groups.Any())
|
||||
throw new PKError(
|
||||
"Your system has no groups! Please create at least one group before using this command.");
|
||||
|
||||
var randInt = randGen.Next(groups.Count());
|
||||
await ctx.Reply(embed: await _embeds.CreateGroupEmbed(ctx, ctx.System, groups.ToArray()[randInt]));
|
||||
await ctx.Reply(embed: await _embeds.CreateGroupEmbed(ctx, target, groups.ToArray()[randInt]));
|
||||
}
|
||||
|
||||
public async Task GroupMember(Context ctx, PKGroup group)
|
||||
{
|
||||
ctx.CheckOwnGroup(group);
|
||||
ctx.CheckSystemPrivacy(group.System, group.ListPrivacy);
|
||||
|
||||
var opts = ctx.ParseListOptions(ctx.DirectLookupContextFor(group.System));
|
||||
opts.GroupFilter = group.Id;
|
||||
|
||||
var members = await ctx.Database.Execute(conn => conn.QueryMemberList(ctx.System.Id, opts.ToQueryOptions()));
|
||||
var members = await ctx.Database.Execute(conn => conn.QueryMemberList(group.System, opts.ToQueryOptions()));
|
||||
|
||||
if (members == null || !members.Any())
|
||||
throw new PKError(
|
||||
@@ -64,11 +74,20 @@ public class Random
|
||||
|
||||
if (!ctx.MatchFlag("all", "a"))
|
||||
members = members.Where(g => g.MemberVisibility == PrivacyLevel.Public);
|
||||
else
|
||||
ctx.CheckOwnGroup(group);
|
||||
|
||||
|
||||
var ms = members.ToList();
|
||||
|
||||
PKSystem system;
|
||||
if (ctx.System?.Id == group.System)
|
||||
system = ctx.System;
|
||||
else
|
||||
system = await ctx.Repository.GetSystem(group.System);
|
||||
|
||||
var randInt = randGen.Next(ms.Count);
|
||||
await ctx.Reply(embed: await _embeds.CreateMemberEmbed(ctx.System, ms[randInt], ctx.Guild,
|
||||
ctx.LookupContextFor(ctx.System.Id), ctx.Zone));
|
||||
await ctx.Reply(embed: await _embeds.CreateMemberEmbed(system, ms[randInt], ctx.Guild,
|
||||
ctx.LookupContextFor(group.System), ctx.Zone));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user