2020-11-22 16:58:23 +00:00
|
|
|
using PluralKit.Core;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public class Random
|
2020-11-22 16:58:23 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly IDatabase _db;
|
|
|
|
private readonly EmbedService _embeds;
|
|
|
|
private readonly ModelRepository _repo;
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly global::System.Random randGen = new();
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Random(EmbedService embeds, IDatabase db, ModelRepository repo)
|
|
|
|
{
|
|
|
|
_embeds = embeds;
|
|
|
|
_db = db;
|
|
|
|
_repo = repo;
|
|
|
|
}
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// todo: get postgresql to return one random member/group instead of querying all members/groups
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task Member(Context ctx)
|
|
|
|
{
|
|
|
|
ctx.CheckSystem();
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var members = await _repo.GetSystemMembers(ctx.System.Id).ToListAsync();
|
2021-09-30 01:51:38 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!ctx.MatchFlag("all", "a"))
|
|
|
|
members = members.Where(m => m.MemberVisibility == PrivacyLevel.Public).ToList();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (members == null || !members.Any())
|
|
|
|
throw new PKError(
|
|
|
|
"Your system has no members! Please create at least one member before using this command.");
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var randInt = randGen.Next(members.Count);
|
|
|
|
await ctx.Reply(embed: await _embeds.CreateMemberEmbed(ctx.System, members[randInt], ctx.Guild,
|
2021-12-06 05:32:54 +00:00
|
|
|
ctx.LookupContextFor(ctx.System.Id), ctx.Zone));
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task Group(Context ctx)
|
|
|
|
{
|
|
|
|
ctx.CheckSystem();
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var groups = await _db.Execute(c => c.QueryGroupList(ctx.System.Id));
|
|
|
|
if (!ctx.MatchFlag("all", "a"))
|
|
|
|
groups = groups.Where(g => g.Visibility == PrivacyLevel.Public);
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (groups == null || !groups.Any())
|
|
|
|
throw new PKError(
|
|
|
|
"Your system has no groups! Please create at least one group before using this command.");
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var randInt = randGen.Next(groups.Count());
|
|
|
|
await ctx.Reply(embed: await _embeds.CreateGroupEmbed(ctx, ctx.System, groups.ToArray()[randInt]));
|
|
|
|
}
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task GroupMember(Context ctx, PKGroup group)
|
|
|
|
{
|
2022-01-13 17:27:00 +00:00
|
|
|
ctx.CheckOwnGroup(group);
|
|
|
|
|
2021-12-07 06:32:29 +00:00
|
|
|
var opts = ctx.ParseMemberListOptions(ctx.DirectLookupContextFor(group.System));
|
2021-11-27 02:10:56 +00:00
|
|
|
opts.GroupFilter = group.Id;
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
await using var conn = await _db.Obtain();
|
|
|
|
var members = await conn.QueryMemberList(ctx.System.Id, opts.ToQueryOptions());
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (members == null || !members.Any())
|
|
|
|
throw new PKError(
|
|
|
|
"This group has no members! Please add at least one member to this group before using this command.");
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!ctx.MatchFlag("all", "a"))
|
|
|
|
members = members.Where(g => g.MemberVisibility == PrivacyLevel.Public);
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var ms = members.ToList();
|
2020-11-22 16:58:23 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var randInt = randGen.Next(ms.Count);
|
|
|
|
await ctx.Reply(embed: await _embeds.CreateMemberEmbed(ctx.System, ms[randInt], ctx.Guild,
|
2021-12-06 05:32:54 +00:00
|
|
|
ctx.LookupContextFor(ctx.System.Id), ctx.Zone));
|
2020-11-22 16:58:23 +00:00
|
|
|
}
|
|
|
|
}
|