2020-11-22 11:58:23 -05:00
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
|
|
public class Random
|
2020-11-22 11:58:23 -05:00
|
|
|
{
|
2021-11-26 21:10:56 -05:00
|
|
|
private readonly EmbedService _embeds;
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
private readonly global::System.Random randGen = new();
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2022-01-22 03:05:01 -05:00
|
|
|
public Random(EmbedService embeds)
|
2021-11-26 21:10:56 -05:00
|
|
|
{
|
|
|
|
|
_embeds = embeds;
|
|
|
|
|
}
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
// todo: get postgresql to return one random member/group instead of querying all members/groups
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2025-09-26 23:56:49 +00:00
|
|
|
public async Task Member(Context ctx, PKSystem target, bool all, bool showEmbed = false)
|
2021-11-26 21:10:56 -05:00
|
|
|
{
|
2022-08-27 13:32:48 +02:00
|
|
|
if (target == null)
|
2024-12-31 08:09:18 -07:00
|
|
|
throw Errors.NoSystemError(ctx.DefaultPrefix);
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2022-08-27 13:32:48 +02:00
|
|
|
ctx.CheckSystemPrivacy(target.Id, target.MemberListPrivacy);
|
|
|
|
|
|
|
|
|
|
var members = await ctx.Repository.GetSystemMembers(target.Id).ToListAsync();
|
2021-09-29 21:51:38 -04:00
|
|
|
|
2025-09-26 23:56:49 +00:00
|
|
|
if (!all)
|
2021-11-26 21:10:56 -05:00
|
|
|
members = members.Where(m => m.MemberVisibility == PrivacyLevel.Public).ToList();
|
2022-08-27 13:32:48 +02:00
|
|
|
else
|
|
|
|
|
ctx.CheckOwnSystem(target);
|
2021-08-27 11:03:47 -04:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
if (members == null || !members.Any())
|
|
|
|
|
throw new PKError(
|
2022-08-27 15:15:14 +02: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!");
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
var randInt = randGen.Next(members.Count);
|
2025-09-07 10:16:50 +12:00
|
|
|
|
2025-09-26 18:47:54 +00:00
|
|
|
if (showEmbed)
|
2025-09-07 10:16:50 +12:00
|
|
|
{
|
|
|
|
|
await ctx.Reply(
|
|
|
|
|
text: EmbedService.LEGACY_EMBED_WARNING,
|
|
|
|
|
embed: await _embeds.CreateMemberEmbed(target, members[randInt], ctx.Guild, ctx.Config, ctx.LookupContextFor(target.Id), ctx.Zone));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await ctx.Reply(
|
|
|
|
|
components: await _embeds.CreateMemberMessageComponents(target, members[randInt], ctx.Guild, ctx.Config, ctx.LookupContextFor(target.Id), ctx.Zone));
|
2021-11-26 21:10:56 -05:00
|
|
|
}
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2025-09-26 23:56:49 +00:00
|
|
|
public async Task Group(Context ctx, PKSystem target, bool all, bool showEmbed = false)
|
2021-11-26 21:10:56 -05:00
|
|
|
{
|
2022-08-27 13:32:48 +02:00
|
|
|
if (target == null)
|
2024-12-31 08:09:18 -07:00
|
|
|
throw Errors.NoSystemError(ctx.DefaultPrefix);
|
2022-11-03 22:24:17 +00:00
|
|
|
|
2022-08-27 13:32:48 +02:00
|
|
|
ctx.CheckSystemPrivacy(target.Id, target.GroupListPrivacy);
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2022-08-27 13:32:48 +02:00
|
|
|
var groups = await ctx.Repository.GetSystemGroups(target.Id).ToListAsync();
|
2025-09-26 23:56:49 +00:00
|
|
|
if (!all)
|
2022-01-14 22:30:02 -05:00
|
|
|
groups = groups.Where(g => g.Visibility == PrivacyLevel.Public).ToList();
|
2022-08-27 13:32:48 +02:00
|
|
|
else
|
|
|
|
|
ctx.CheckOwnSystem(target);
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
if (groups == null || !groups.Any())
|
|
|
|
|
throw new PKError(
|
2022-11-03 22:24:17 +00:00
|
|
|
ctx.System?.Id == target.Id ?
|
|
|
|
|
"Your system has no groups! Please create at least one group before using this command." :
|
2022-08-27 15:15:14 +02:00
|
|
|
$"This system has no groups!");
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
var randInt = randGen.Next(groups.Count());
|
2025-09-07 10:16:50 +12:00
|
|
|
|
2025-09-26 18:47:54 +00:00
|
|
|
if (showEmbed)
|
2025-09-07 10:16:50 +12:00
|
|
|
{
|
|
|
|
|
await ctx.Reply(
|
|
|
|
|
text: EmbedService.LEGACY_EMBED_WARNING,
|
2025-10-08 03:26:40 +00:00
|
|
|
embed: await _embeds.CreateGroupEmbed(ctx, target, groups.ToArray()[randInt], all));
|
2025-09-07 10:16:50 +12:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await ctx.Reply(
|
2025-10-08 03:26:40 +00:00
|
|
|
components: await _embeds.CreateGroupMessageComponents(ctx, target, groups.ToArray()[randInt], all));
|
2021-11-26 21:10:56 -05:00
|
|
|
}
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2025-11-23 11:56:14 +00:00
|
|
|
public async Task GroupMember(Context ctx, PKGroup group, bool all, bool show_embed, IHasListOptions flags)
|
2021-11-26 21:10:56 -05:00
|
|
|
{
|
2022-08-27 13:32:48 +02:00
|
|
|
ctx.CheckSystemPrivacy(group.System, group.ListPrivacy);
|
2022-01-13 12:27:00 -05:00
|
|
|
|
2025-09-30 18:45:35 +00:00
|
|
|
var opts = flags.GetListOptions(ctx, group.System);
|
2021-11-26 21:10:56 -05:00
|
|
|
opts.GroupFilter = group.Id;
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2022-08-27 13:32:48 +02:00
|
|
|
var members = await ctx.Database.Execute(conn => conn.QueryMemberList(group.System, opts.ToQueryOptions()));
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
if (members == null || !members.Any())
|
|
|
|
|
throw new PKError(
|
2022-08-27 15:15:14 +02:00
|
|
|
"This group has no members!"
|
2022-11-03 22:24:17 +00:00
|
|
|
+ (ctx.System?.Id == group.System ? " Please add at least one member to this group before using this command." : ""));
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2025-11-23 11:56:14 +00:00
|
|
|
if (!all)
|
2021-11-26 21:10:56 -05:00
|
|
|
members = members.Where(g => g.MemberVisibility == PrivacyLevel.Public);
|
2022-08-27 13:32:48 +02:00
|
|
|
else
|
|
|
|
|
ctx.CheckOwnGroup(group);
|
|
|
|
|
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
var ms = members.ToList();
|
2020-11-22 11:58:23 -05:00
|
|
|
|
2022-08-27 13:32:48 +02:00
|
|
|
PKSystem system;
|
|
|
|
|
if (ctx.System?.Id == group.System)
|
|
|
|
|
system = ctx.System;
|
|
|
|
|
else
|
|
|
|
|
system = await ctx.Repository.GetSystem(group.System);
|
|
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
var randInt = randGen.Next(ms.Count);
|
2025-09-07 10:16:50 +12:00
|
|
|
|
2025-11-23 11:56:14 +00:00
|
|
|
if (show_embed)
|
2025-09-07 10:16:50 +12:00
|
|
|
{
|
|
|
|
|
await ctx.Reply(
|
|
|
|
|
text: EmbedService.LEGACY_EMBED_WARNING,
|
|
|
|
|
embed: await _embeds.CreateMemberEmbed(system, ms[randInt], ctx.Guild, ctx.Config, ctx.LookupContextFor(system.Id), ctx.Zone));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await ctx.Reply(
|
|
|
|
|
components: await _embeds.CreateMemberMessageComponents(system, ms[randInt], ctx.Guild, ctx.Config, ctx.LookupContextFor(system.Id), ctx.Zone));
|
2020-11-22 11:58:23 -05:00
|
|
|
}
|
|
|
|
|
}
|