PluralKit/PluralKit.Bot/Commands/Member.cs

78 lines
3.9 KiB
C#
Raw Normal View History

2020-02-01 12:03:02 +00:00
using System.Linq;
using System.Threading.Tasks;
using PluralKit.Core;
namespace PluralKit.Bot
2020-02-01 12:03:02 +00:00
{
public class Member
{
private IDataStore _data;
2020-06-13 17:42:04 +00:00
private IDatabase _db;
2020-02-01 12:03:02 +00:00
private EmbedService _embeds;
2020-06-13 17:42:04 +00:00
public Member(IDataStore data, EmbedService embeds, IDatabase db)
2020-02-01 12:03:02 +00:00
{
_data = data;
_embeds = embeds;
2020-06-13 17:42:04 +00:00
_db = db;
2020-02-01 12:03:02 +00:00
}
public async Task NewMember(Context ctx) {
if (ctx.System == null) throw Errors.NoSystemError;
var memberName = ctx.RemainderOrNull() ?? throw new PKSyntaxError("You must pass a member name.");
// Hard name length cap
if (memberName.Length > Limits.MaxMemberNameLength) throw Errors.MemberNameTooLongError(memberName.Length);
// Warn if there's already a member by this name
var existingMember = await _data.GetMemberByName(ctx.System, memberName);
if (existingMember != null) {
2020-07-21 00:10:26 +00:00
var msg = $"{Emojis.Warn} You already have a member in your system with the name \"{existingMember.NameFor(ctx)}\" (with ID `{existingMember.Hid}`). Do you want to create another member with the same name?";
2020-02-01 12:03:02 +00:00
if (!await ctx.PromptYesNo(msg)) throw new PKError("Member creation cancelled.");
}
2020-06-29 12:54:11 +00:00
await using var conn = await _db.Obtain();
2020-02-01 12:03:02 +00:00
// Enforce per-system member limit
2020-06-29 12:54:11 +00:00
var memberCount = await conn.GetSystemMemberCount(ctx.System.Id);
2020-02-01 12:03:02 +00:00
if (memberCount >= Limits.MaxMemberCount)
throw Errors.MemberLimitReachedError;
// Create the member
2020-06-29 12:54:11 +00:00
var member = await conn.CreateMember(ctx.System.Id, memberName);
2020-02-01 12:03:02 +00:00
memberCount++;
// Send confirmation and space hint
await ctx.Reply($"{Emojis.Success} Member \"{memberName}\" (`{member.Hid}`) registered! Check out the getting started page for how to get a member up and running: https://pluralkit.me/start#members");
2020-02-01 12:03:02 +00:00
if (memberName.Contains(" "))
await ctx.Reply($"{Emojis.Note} Note that this member's name contains spaces. You will need to surround it with \"double quotes\" when using commands referring to it, or just use the member's 5-character ID (which is `{member.Hid}`).");
if (memberCount >= Limits.MaxMemberCount)
await ctx.Reply($"{Emojis.Warn} You have reached the per-system member limit ({Limits.MaxMemberCount}). You will be unable to create additional members until existing members are deleted.");
else if (memberCount >= Limits.MaxMembersWarnThreshold)
await ctx.Reply($"{Emojis.Warn} You are approaching the per-system member limit ({memberCount} / {Limits.MaxMemberCount} members). Please review your member list for unused or duplicate members.");
}
public async Task MemberRandom(Context ctx)
{
ctx.CheckSystem();
var randGen = new global::System.Random();
//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
Feature/granular member privacy (#174) * Some reasons this needs to exist for it to run on my machine? I don't think it would hurt to have it in other machines so * Add options to member model * Add Privacy to member embed * Added member privacy display list * Update database settings * apparetnly this is nolonger needed? * Fix sql call * Fix more sql errors * Added in settings control * Add all subject to system privacy * Basic API Privacy * Name privacy in logs * update todo * remove CheckReadMemberPermission * Added name privacy to log embed * update todo * Update todo * Update api to handle privacy * update todo * Update systemlist full to respect privacy (as well as system list) * include colour as option for member privacy subject * move todo file (why was it there?) * Update TODO.md * Update TODO.md * Update TODO.md * Deleted to create pr * Update command usage and add to the command tree * Make api respect created privacy * Add editing privacy through the api * Fix pronoun privacy field in api * Fix info leak of display name in api * deprecate privacy field in api * Deprecate privacy diffrently * Update API * Update documentation * Update documentation * Remove comment in yml * Update userguide * Update migration (fix typo in 5.sql too) * Sanatize names * some full stops * Fix after merge * update migration * update schema version * update edit command * update privacy filter * fix a dumb mistake * clarify on what name privacy does * make it easier on someone else * Update docs * Comment out unused code * Add aliases for `member privacy all public` and `member privacy all private`
2020-06-17 19:31:39 +00:00
var members = await _data.GetSystemMembers(ctx.System).Where(m => m.MemberVisibility == PrivacyLevel.Public).ToListAsync();
2020-02-01 12:03:02 +00:00
if (members == null || !members.Any())
throw Errors.NoMembersError;
var randInt = randGen.Next(members.Count);
await ctx.Reply(embed: await _embeds.CreateMemberEmbed(ctx.System, members[randInt], ctx.Guild, ctx.LookupContextFor(ctx.System)));
}
public async Task ViewMember(Context ctx, PKMember target)
{
2020-06-13 17:42:04 +00:00
var system = await _db.Execute(c => c.QuerySystem(target.System));
2020-02-01 12:03:02 +00:00
await ctx.Reply(embed: await _embeds.CreateMemberEmbed(system, target, ctx.Guild, ctx.LookupContextFor(system)));
}
}
}