import { Client, GatewayIntentBits } from "discord.js"; const { TOKEN, COMMAND_CHANNEL_ID, ADDUP_ID, PICKING_ID, BLU_ID, RED_ID, FK_ID, CAPTAIN_ID, } = require("./config.json"); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildVoiceStates, ], }); 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}...` ); 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); }); // send message on error client.on("error", (error) => { console.error(error); let channel = client.channels.cache.get(COMMAND_CHANNEL_ID); if (!channel) return console.error("Can't find command channel"); client.channels.cache .get(COMMAND_CHANNEL_ID) .send(`error: ${error.message}`) .catch(console.error); }); client.on("interactionCreate", async (interaction) => { const command = interaction.commandName; if (!interaction.isChatInputCommand()) return; if ( command === "scout" || command === "soldier" || command === "pyro" || command === "demoman" || command === "demo" || command === "heavy" || command === "engineer" || command === "engi" || command === "medic" || command === "sniper" || command === "spy" ) { // get voice channels const picking = interaction.guild.channels.cache.find( (channel) => channel.name === "picking" || channel.id === PICKING_ID ); if (!picking) return console.error("Can't find channel 'picking'!"); const captain = interaction.guild.channels.cache.find( (channel) => channel.name === "captains" || channel.id === CAPTAIN_ID ); if (!captain) return console.error("Can't find channel 'captains'!"); // make sure user is in picking or captain channel if ( !picking.members.has(interaction.user.id) && !captain.members.has(interaction.user.id) ) { await interaction.reply({ content: "must be in picking or captains channel to use this command", ephemeral: true, }); return; } else { await interaction.reply({ content: "checking picking channel...", ephemeral: true, }); // set role name let roleName = command; if (command === "demoman") roleName = "demo"; if (command === "engi") roleName = "engineer"; // check each member in picking channel for role 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 (${roleName}):`) str += ","; str += " " + member.user.globalName; } } if (str === `In picking (${roleName}):`) str = `None found ¯\\_(ツ)_/¯ (${roleName})`; // respond return await interaction.followUp(str); } } if (interaction.channelId !== COMMAND_CHANNEL_ID) { await interaction.reply({ content: "wrong channel, or you lack the required permissions", ephemeral: true, }); return; } if (command === "hello") { await interaction.reply("world"); } if ( command === "topicking" || command === "end" || command === "resetteams" ) { await interaction.reply("moving members..."); // get voice channels const blu = interaction.guild.channels.cache.find( (channel) => channel.name === "blu" || channel.id === BLU_ID ); if (!blu) return console.error("Can't find channel 'blu'!"); const red = interaction.guild.channels.cache.find( (channel) => channel.name === "red" || channel.id === RED_ID ); if (!red) return console.error("Can't find channel 'red'!"); const addup = interaction.guild.channels.cache.find( (channel) => channel.name === "add-up" || channel.id === ADDUP_ID ); if (!addup) return console.error("Can't find channel 'add-up'!"); const picking = interaction.guild.channels.cache.find( (channel) => channel.name === "picking" || channel.id === PICKING_ID ); if (!picking) return console.error("Can't find channel 'picking'!"); // get members in voice channel let members = blu.members.concat(red.members); if (command !== "resetteams") members = members.concat(addup.members); if (members.size === 0) { return await interaction.followUp( `found no members in ${ command === "resetteams" ? "blu" : "addup, blu," } or red` ); } // move members to addup let idx = 0, eCount = 0; members.forEach(async (member) => { idx++; console.log(idx, members.size); try { await member.voice.setChannel(picking); } catch (error) { console.error(error); eCount++; } // 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.reply("moving fatkids..."); // get voice channels const picking = interaction.guild.channels.cache.find( (channel) => channel.name === "picking" || channel.id === PICKING_ID ); if (!picking) return console.error("Can't find channel 'add-up'!"); const fk = interaction.guild.channels.cache.find( (channel) => channel.name === "fatkid" || channel.id === FK_ID ); if (!fk) return console.error("Can't find channel 'fatkid'!"); const members = picking.members; if (members.size === 0) { return await interaction.followUp("found no members in picking"); } // get members in voice channel let str = "", eCount = 0; const logFk = async (key, member) => { try { await member.voice.setChannel(fk); if (str.length > 0) str += ", "; str += member.user.globalName; } catch (error) { console.error(error); eCount++; } }; const processAllEntries = async () => { return Promise.all( Array.from(members, async ([key, value]) => { console.log(key, value); // what are these? await logFk(key, value); }) ); }; processAllEntries().then(() => interaction.followUp( `fatkids: ${str}${ eCount > 0 ? ` (error moving ${eCount} members)` : "" }` ) ); } }); client.login(TOKEN);