PluralKit/PluralKit.Bot/Commands/Random.cs

98 lines
3.4 KiB
C#
Raw Normal View History

using PluralKit.Core;
namespace PluralKit.Bot;
public class Random
{
private readonly EmbedService _embeds;
private readonly global::System.Random randGen = new();
public Random(EmbedService embeds)
{
_embeds = embeds;
}
// todo: get postgresql to return one random member/group instead of querying all members/groups
2022-08-27 11:32:48 +00:00
public async Task Member(Context ctx, PKSystem target)
{
2022-08-27 11:32:48 +00:00
if (target == null)
throw Errors.NoSystemError;
2022-08-27 11:32:48 +00:00
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();
2022-08-27 11:32:48 +00:00
else
ctx.CheckOwnSystem(target);
2021-08-27 15:03:47 +00:00
if (members == null || !members.Any())
throw new PKError(
2022-08-27 13:15:14 +00:00
ctx.System?.Id == target.Id ?
"Your system has no members! Please create at least one member before using this command." :
"This system has no members!");
var randInt = randGen.Next(members.Count);
2022-08-27 11:32:48 +00:00
await ctx.Reply(embed: await _embeds.CreateMemberEmbed(target, members[randInt], ctx.Guild,
ctx.LookupContextFor(target.Id), ctx.Zone));
}
2022-08-27 11:32:48 +00:00
public async Task Group(Context ctx, PKSystem target)
{
2022-08-27 11:32:48 +00:00
if (target == null)
throw Errors.NoSystemError;
ctx.CheckSystemPrivacy(target.Id, target.GroupListPrivacy);
2022-08-27 11:32:48 +00:00
var groups = await ctx.Repository.GetSystemGroups(target.Id).ToListAsync();
if (!ctx.MatchFlag("all", "a"))
groups = groups.Where(g => g.Visibility == PrivacyLevel.Public).ToList();
2022-08-27 11:32:48 +00:00
else
ctx.CheckOwnSystem(target);
if (groups == null || !groups.Any())
throw new PKError(
2022-08-27 13:15:14 +00:00
ctx.System?.Id == target.Id ?
"Your system has no groups! Please create at least one group before using this command." :
$"This system has no groups!");
var randInt = randGen.Next(groups.Count());
2022-08-27 11:32:48 +00:00
await ctx.Reply(embed: await _embeds.CreateGroupEmbed(ctx, target, groups.ToArray()[randInt]));
}
public async Task GroupMember(Context ctx, PKGroup group)
{
2022-08-27 11:32:48 +00:00
ctx.CheckSystemPrivacy(group.System, group.ListPrivacy);
var opts = ctx.ParseListOptions(ctx.DirectLookupContextFor(group.System));
opts.GroupFilter = group.Id;
2022-08-27 11:32:48 +00:00
var members = await ctx.Database.Execute(conn => conn.QueryMemberList(group.System, opts.ToQueryOptions()));
if (members == null || !members.Any())
throw new PKError(
2022-08-27 13:15:14 +00:00
"This group has no members!"
+ ( ctx.System?.Id == group.System ? " Please add at least one member to this group before using this command." : ""));
if (!ctx.MatchFlag("all", "a"))
members = members.Where(g => g.MemberVisibility == PrivacyLevel.Public);
2022-08-27 11:32:48 +00:00
else
ctx.CheckOwnGroup(group);
var ms = members.ToList();
2022-08-27 11:32:48 +00:00
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);
2022-08-27 11:32:48 +00:00
await ctx.Reply(embed: await _embeds.CreateMemberEmbed(system, ms[randInt], ctx.Guild,
ctx.LookupContextFor(group.System), ctx.Zone));
}
}