From f7549776f68498578c282548b1055c8ad57f52ad Mon Sep 17 00:00:00 2001 From: ethanf Date: Sun, 28 Jan 2024 18:30:17 -0600 Subject: [PATCH] feat: randomly select fatkids, rename old fk --- index.js | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++- register.js | 27 +++++++++++- 2 files changed, 149 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 2c71a6e..f916254 100644 --- a/index.js +++ b/index.js @@ -363,6 +363,129 @@ client.on("interactionCreate", async (interaction) => { } if (command === "fk" || command === "fatkid") { + // pull users in addup into picking, then randomly select other users in picking to be sat out until there are 18 in picking + await interaction.reply("Randomly choosing fatkids from picking..."); + + // get voice channels + const addup = interaction.guild.channels.cache.find( + (channel) => channel.name === "add-up" || channel.id === ADDUP_ID + ); + const picking = interaction.guild.channels.cache.find( + (channel) => channel.name === "picking" || channel.id === PICKING_ID + ); + + // get members in voice channel + const addupPlayers = Array.from(addup.members.values()); + if (addupPlayers.length === 0) { + return await interaction.followUp("Found no players in addup"); + } + const pickingPlayers = Array.from(picking.members.values()); + if (pickingPlayers.length === 0) { + return await interaction.followUp("Found no players in picking"); + } + + let fatkids = []; + + // select excess players to be sat out + while (pickingPlayers.length + addupPlayers.length > 18) { + const idx = Math.floor(Math.random() * pickingPlayers.length); + fatkids.push(pickingPlayers.splice(idx, 1)[0]); + } + + let errCount = 0; + + // move players from addup to picking + for (const newPlayer of addupPlayers) { + try { + await newPlayer.voice.setChannel(picking); + } catch (error) { + console.error(error); + errCount++; + } + } + + // move players from picking to fatkid + for (const fk of fatkids) { + try { + if (picking.members.size <= 18) break; + await fk.voice.setChannel(addup); + } catch (error) { + console.error(error); + errCount++; + } + } + + interaction.followUp( + `Sat out ${fatkids.length} players${ + errCount > 0 ? ` (error moving ${errCount} members)` : "" + }` + ); + } + + /*if (command === "testfk") { + // debug fk + // pull users in addup into picking, then randomly select other users in picking to be sat out until there are 18 in picking + await interaction.reply("Randomly choosing fatkids from picking..."); + + // get voice channels + const addup = interaction.guild.channels.cache.find( + (channel) => channel.name === "add-up" || channel.id === ADDUP_ID + ); + const picking = interaction.guild.channels.cache.find( + (channel) => channel.name === "picking" || channel.id === PICKING_ID + ); + + // get members in voice channel + const addupPlayers = Array.from(addup.members.values()); + if (addupPlayers.length === 0) { + return await interaction.followUp("Found no players in addup"); + } + const pickingPlayers = Array.from(picking.members.values()); + if (pickingPlayers.length === 0) { + return await interaction.followUp("Found no players in picking"); + } + + let fatkids = []; + + // select excess players to be sat out + while (pickingPlayers.length + addupPlayers.length > 2) { + const idx = Math.floor(Math.random() * pickingPlayers.length); + fatkids.push(pickingPlayers.splice(idx, 1)[0]); + } + + let errCount = 0; + + // move players from addup to picking + for (const newPlayer of addupPlayers) { + try { + await newPlayer.voice.setChannel(picking); + } catch (error) { + console.error(error); + errCount++; + } + } + + // move players from picking to fatkid + for (const fk of fatkids) { + try { + console.log(addup.members.size, picking.members.size) + if (picking.members.size <= 2) break; + await fk.voice.setChannel(addup); + } catch (error) { + console.error(error); + errCount++; + } + } + + interaction.followUp( + `Sat out ${fatkids.length} players${ + errCount > 0 ? ` (error moving ${errCount} members)` : "" + }` + ); + }*/ + + if (command === "fklist" || command === "listfk") { + // moves players in picking to fatkid channel, for use in captain pugs await interaction.reply("Moving fatkids..."); // get voice channels @@ -548,7 +671,7 @@ client.on("interactionCreate", async (interaction) => { } interaction.followUp( - `Teams selected and moved${ + `Players moved into teams${ moveErr > 0 ? ` (error moving ${moveErr} members)` : "" }` ); diff --git a/register.js b/register.js index 1267342..398a855 100644 --- a/register.js +++ b/register.js @@ -1,5 +1,5 @@ import { REST, Routes } from "discord.js"; -const { TOKEN, CLIENT_ID } = require("./config.json"); +const { TOKEN, CLIENT_ID, GUILD_ID } = require("./config.json"); const commands = [ { @@ -20,10 +20,22 @@ const commands = [ }, { name: "fatkid", - description: "Pulls addup channel members into fk channel and lists them", + description: "Moves added players to picking and randomly selects fatkids", }, { name: "fk", + description: "Moves added players to picking and randomly selects fatkids", + }, + /*{ + name: "testfk", + description: "debug fk", + },*/ + { + name: "fklist", + description: "Pulls addup channel members into fk channel and lists them", + }, + { + name: "listfk", description: "Pulls addup channel members into fk channel and lists them", }, { @@ -93,10 +105,21 @@ const rest = new REST({ version: "10" }).setToken(TOKEN); try { console.log("Started refreshing application (/) commands."); + // application reload await rest.put(Routes.applicationCommands(CLIENT_ID), { body: commands, }); + // guild reload, faster than application reload + //await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), { + // body: commands, + //}); + + // clear guild commands + //await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), { + // body: [], + //}); + console.log("Successfully reloaded application (/) commands."); } catch (error) { console.error(error);