penguin-band/commands/end.js

72 lines
1.7 KiB
JavaScript

import { SlashCommandBuilder } from "discord.js";
import { findChannelfromCache } from "../utils.js";
import { RUNNER_ROLE_ID, PICKING_ID, BLU_ID, RED_ID } from "../config.json";
const data = new SlashCommandBuilder()
.setName("end")
.setDescription("Moves the team channel members back to the picking channel");
const permissions = [RUNNER_ROLE_ID];
const execute = async (interaction) => {
await interaction.reply("Moving members...");
// get voice channels
const picking = findChannelfromCache(
interaction.guild.channels.cache,
"picking",
PICKING_ID
);
const blu = findChannelfromCache(
interaction.guild.channels.cache,
"blu",
BLU_ID
);
const red = findChannelfromCache(
interaction.guild.channels.cache,
"red",
RED_ID
);
if (!picking || !blu || !red) {
return console.error("Could not find all channels for /end");
}
// get members in voice channel
let members = blu.members.concat(red.members);
if (members.size === 0) {
return await interaction.followUp("Found no members in blu or red");
}
let eCount = 0;
// move members to picking
const moveToPicking = async (member) => {
try {
await member.voice.setChannel(picking);
} catch (error) {
console.error(error);
eCount++;
}
};
const moveAllToPicking = async () => {
return Promise.all(
Array.from(members, async ([memberId, member]) => {
await moveToPicking(member);
})
);
};
moveAllToPicking().then(() =>
interaction.followUp(
`Moved members in blu and red${
eCount > 0 ? ` (error moving ${eCount} members)` : ""
}`
)
);
};
const synonyms = ["resetteams"];
export { data, permissions, execute, synonyms };