예제 #1
0
        public async Task CreateGroup(Context ctx)
        {
            ctx.CheckSystem();

            // Check group name length
            var groupName = ctx.RemainderOrNull() ?? throw new PKSyntaxError("You must pass a group name.");

            if (groupName.Length > Limits.MaxGroupNameLength)
            {
                throw new PKError($"Group name too long ({groupName.Length}/{Limits.MaxGroupNameLength} characters).");
            }

            await using var conn = await _db.Obtain();

            // Check group cap
            var existingGroupCount = await conn.QuerySingleAsync <int>("select count(*) from groups where system = @System", new { System = ctx.System.Id });

            var groupLimit = ctx.System.GroupLimitOverride ?? Limits.MaxGroupCount;

            if (existingGroupCount >= groupLimit)
            {
                throw new PKError($"System has reached the maximum number of groups ({groupLimit}). Please delete unused groups first in order to create new ones.");
            }

            // Warn if there's already a group by this name
            var existingGroup = await _repo.GetGroupByName(conn, ctx.System.Id, groupName);

            if (existingGroup != null)
            {
                var msg = $"{Emojis.Warn} You already have a group in your system with the name \"{existingGroup.Name}\" (with ID `{existingGroup.Hid}`). Do you want to create another group with the same name?";
                if (!await ctx.PromptYesNo(msg))
                {
                    throw new PKError("Group creation cancelled.");
                }
            }

            var newGroup = await _repo.CreateGroup(conn, ctx.System.Id, groupName);

            var eb = new DiscordEmbedBuilder()
                     .WithDescription($"Your new group, **{groupName}**, has been created, with the group ID **`{newGroup.Hid}`**.\nBelow are a couple of useful commands:")
                     .AddField("View the group card", $"> pk;group **{newGroup.Reference()}**")
                     .AddField("Add members to the group", $"> pk;group **{newGroup.Reference()}** add **MemberName**\n> pk;group **{newGroup.Reference()}** add **Member1** **Member2** **Member3** (and so on...)")
                     .AddField("Set the description", $"> pk;group **{newGroup.Reference()}** description **This is my new group, and here is the description!**")
                     .AddField("Set the group icon", $"> pk;group **{newGroup.Reference()}** icon\n*(with an image attached)*");
            await ctx.Reply($"{Emojis.Success} Group created!", eb.Build());
        }