diff --git a/src/commands/Matcha.ts b/src/commands/Matcha.ts index 2d8bf16..91def2d 100644 --- a/src/commands/Matcha.ts +++ b/src/commands/Matcha.ts @@ -24,6 +24,9 @@ export default class Matcha extends Command { constructor(client: BotClient) { const definition = new SlashCommandBuilder() .setName('matcha') + .addIntegerOption(option => + option.setName('groupsize').setDescription('Set the minimum group size').setRequired(true) + ) .setDescription( 'Triggers matching for Matcha! Threads will be created in the channel where this command is called.' ); @@ -68,11 +71,27 @@ export default class Matcha extends Command { return shuffledArray; } + private static splitIntoGroups(shuffledMembersList: GuildMember[], groupsize: number): GuildMember[] { + const extraMembers = shuffledMembersList.length % groupsize; + const numGroups = Math.floor(shuffledMembersList.length / groupsize); + if (extraMembers !== 0) { + // If length % size is off add people to the first group or first n groups + // for one group remaining add all extras + if (numGroups === 1) { + return shuffledMembersList.splice(0, shuffledMembersList.length); + } + // otherwise, disperse extra people across groupss + const addedToGroup = Math.floor(extraMembers / numGroups); + return shuffledMembersList.splice(0, groupsize + addedToGroup); + } + return shuffledMembersList.splice(0, groupsize); + } /** * Gets all users with the given role in the current discord server where this command was called. * @param interaction The original CommandInteraction from calling the /matcha command. * @returns A list of server users who have the specified 'match' role. */ + private async getRoleUsers(interaction: CommandInteraction): Promise { // Not all members will be stored in the role cache initially, so fetching // all guild members in the server will populate them in the role cache for our use. @@ -100,21 +119,15 @@ export default class Matcha extends Command { */ private static async createMatches(interaction: CommandInteraction, users: GuildMember[]): Promise { const numMembersMatched = users.length; - // Now, we make the pairings for Donuts members. + // Now, we make the pairings for Matcha members. const shuffledMembersList = Matcha.shuffle(users); const memberPairings = []; + const groupsize = interaction.options.getInteger('groupsize', true); while (shuffledMembersList.length > 0) { - let pairedMembers: GuildMember[]; - if (shuffledMembersList.length % 2 === 1) { - // If there's an odd number of people, we start with a group of 3 to correct it. - pairedMembers = shuffledMembersList.splice(0, 3); - } else { - pairedMembers = shuffledMembersList.splice(0, 2); - } + const pairedMembers: GuildMember[] = Matcha.splitIntoGroups(shuffledMembersList, groupsize); memberPairings.push(pairedMembers); } - /** * To prevent ourselves from hitting Discord's API rate limit (50 requests/second), * we add a small delay between each creation of a group thread and execute them @@ -181,6 +194,18 @@ export default class Matcha extends Command { await super.edit(interaction, { content: '/matcha was canceled!', components: [] }); return; } + const groupsize = interaction.options.getInteger('groupsize', true); + + if (groupsize <= 1) { + await super.edit(interaction, { + content: + '**/matcha** needs a group size greater than 1! Otherwise, your only friend will be yourself! :rage:', + components: [], + ephemeral: true, + }); + return; + } + // Otherwise, the 'Confirm' button was called. this.lastRun = DateTime.now(); // Remove the button so they can't press it again. diff --git a/yarn.lock b/yarn.lock index c67dfcc..5816188 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1505,6 +1505,11 @@ has-flag@^3.0.0: resolved "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz" integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0= +has-flag@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b" + integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ== + has-symbols@^1.0.1, has-symbols@^1.0.2: version "1.0.2" resolved "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.2.tgz" @@ -2142,7 +2147,7 @@ ms@^2.1.1: resolved "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz" integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA== -nan@^2.17.0: +nan@^2.14.0, nan@^2.17.0: version "2.17.0" resolved "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz" integrity sha512-2ZTgtl0nJsO0KQCjEpxcIr5D+Yv90plTitZt9JBfQvVJDS5seMl3FOvsh3+9CoYWXf/1l5OaZzzF6nDm4cagaQ== @@ -2234,9 +2239,9 @@ object-inspect@^1.11.0, object-inspect@^1.9.0: resolved "https://registry.npmjs.org/object-inspect/-/object-inspect-1.11.0.tgz" integrity sha512-jp7ikS6Sd3GxQfZJPyH3cjcbJF6GZPClgdV+EFygjFLQ5FmW/dRUnTd9PQ9k0JhoNDabWFbpF1yCdSWCC6gexg== -object-keys@^1.1.1: +object-keys@^1.0.12, object-keys@^1.1.1: version "1.1.1" - resolved "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz" + resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.1.1.tgz#1c47f272df277f3b1daf061677d9c82e2322c60e" integrity sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA== object.assign@^4.1.2: @@ -2388,6 +2393,11 @@ path-type@^3.0.0: dependencies: pify "^3.0.0" +path-type@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b" + integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw== + picocolors@^1.0.0: version "1.0.0" resolved "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz"