penguin-band/index.js

146 lines
4.1 KiB
JavaScript

import { Client, GatewayIntentBits } from "discord.js";
const {
TOKEN,
COMMAND_CHANNEL_ID,
ADDUP_ID,
PICKING_ID,
BLU_ID,
RED_ID,
FK_ID,
} = require("./config.json");
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates],
});
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("interactionCreate", async (interaction) => {
const command = interaction.commandName;
if (!interaction.isChatInputCommand()) return;
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.deferReply();
// 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 (commandName !== "resetteams") members = members.concat(addup.members);
if (members.size === 0) {
return await interaction.editReply(
`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++;
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)` : ""}`,
});
}
}
});
}
if (command === "fk" || command === "fatkid") {
await interaction.deferReply();
// 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.editReply("found no members in picking");
}
// get members in voice channel
let idx = 0,
eCount = 0,
str = "";
members.forEach(async (member) => {
idx++;
try {
await member.voice.setChannel(fk);
if (str.length > 0) str += ", ";
str += member.user.globalName;
} catch (error) {
console.error(error);
eCount++;
} finally {
// 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)` : ""
}`,
});
}
}
});
}
});
client.login(TOKEN);