From 1c4aff27dec0c6c9ef48db8d0b4aa49fd5d34206 Mon Sep 17 00:00:00 2001 From: ethanf Date: Tue, 16 Jan 2024 14:48:57 -0600 Subject: [PATCH] fix: replace defer/edit with reply/followup --- index.js | 85 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/index.js b/index.js index 91c695f..6eea524 100644 --- a/index.js +++ b/index.js @@ -20,16 +20,21 @@ const client = new Client({ client.on("ready", () => { console.log(`Logged in as ${client.user.tag}!`); - console.log(`Deleting old messages from this bot in command channel: ${COMMAND_CHANNEL_ID}...`); + console.log( + `Deleting old messages from this bot in command channel: ${COMMAND_CHANNEL_ID}...` + ); let channel = client.channels.cache.get(COMMAND_CHANNEL_ID); if (!channel) return console.error("(ready) Can't find command channel"); - channel.messages.fetch({ limit: 100 }).then(messages => { - messages.forEach(message => { - if (message.author.id === client.user.id) { - message.delete(); - } - }); - }).catch(console.error); + channel.messages + .fetch({ limit: 100 }) + .then((messages) => { + messages.forEach((message) => { + if (message.author.id === client.user.id) { + message.delete(); + } + }); + }) + .catch(console.error); }); // send message on error @@ -81,7 +86,10 @@ client.on("interactionCreate", async (interaction) => { }); return; } else { - await interaction.deferReply(); + await interaction.reply({ + content: "checking picking channel...", + ephemeral: true, + }); // set role name let roleName = command; @@ -89,19 +97,18 @@ client.on("interactionCreate", async (interaction) => { if (command === "engi") roleName = "engineer"; // check each member in picking channel for role - let str = "In picking:"; + let str = `In picking (${roleName}):`; for (const member of picking.members.values()) { if (member.roles.cache.find((role) => role.name === roleName)) { - if (str !== "In picking:") str += ","; + if (str !== `In picking (${roleName}):`) str += ","; str += " " + member.user.globalName; } } - if (str === "In picking:") str = "None found ¯\\_(ツ)_/¯"; + if (str === `In picking (${roleName}):`) + str = `None found ¯\\_(ツ)_/¯ (${roleName})`; // respond - return await interaction.editReply({ - content: str, - }); + return await interaction.followUp(str); } } @@ -122,7 +129,7 @@ client.on("interactionCreate", async (interaction) => { command === "end" || command === "resetteams" ) { - await interaction.deferReply(); + await interaction.reply("moving members..."); // get voice channels const blu = interaction.guild.channels.cache.find( @@ -146,7 +153,7 @@ client.on("interactionCreate", async (interaction) => { let members = blu.members.concat(red.members); if (command !== "resetteams") members = members.concat(addup.members); if (members.size === 0) { - return await interaction.editReply( + return await interaction.followUp( `found no members in ${ command === "resetteams" ? "blu" : "addup, blu," } or red` @@ -158,26 +165,26 @@ client.on("interactionCreate", async (interaction) => { eCount = 0; members.forEach(async (member) => { idx++; + console.log(idx, members.size); try { await member.voice.setChannel(picking); } catch (error) { console.error(error); eCount++; - } finally { - // respond when done - if (idx === members.size) { - return await interaction.editReply({ - content: `moved members in ${ - command === "resetteams" ? "blu" : "addup, blu," - } and red${eCount > 0 ? ` (error moving ${eCount} members)` : ""}`, - }); - } + } + // respond when done + if (idx === members.size) { + return await interaction.followUp( + `moved members in ${ + command === "resetteams" ? "blu" : "addup, blu," + } and red${eCount > 0 ? ` (error moving ${eCount} members)` : ""}` + ); } }); } if (command === "fk" || command === "fatkid") { - await interaction.deferReply(); + await interaction.reply("moving fatkids..."); // get voice channels const picking = interaction.guild.channels.cache.find( @@ -191,15 +198,15 @@ client.on("interactionCreate", async (interaction) => { const members = picking.members; if (members.size === 0) { - return await interaction.editReply("found no members in picking"); + return await interaction.followUp("found no members in picking"); } - // get members in voice channel let idx = 0, eCount = 0, str = ""; members.forEach(async (member) => { idx++; + console.log(idx, members.size); try { await member.voice.setChannel(fk); if (str.length > 0) str += ", "; @@ -207,20 +214,14 @@ client.on("interactionCreate", async (interaction) => { } catch (error) { console.error(error); eCount++; - } finally { + } + if (idx === members.size) { // list members when done - if (idx === members.size) { - if (eCount > 0) { - return await interaction.editReply({ - content: `fatkids: ${str}`, - }); - } - return await interaction.editReply({ - content: `fatkids: ${str}${ - eCount > 0 ? ` (error moving ${eCount} members)` : "" - }`, - }); - } + return await interaction.followUp( + `fatkids: ${str}${ + eCount > 0 ? ` (error moving ${eCount} members)` : "" + }` + ); } }); }