penguin-band/index.js

131 lines
3.7 KiB
JavaScript

import { Client, GatewayIntentBits } from "discord.js";
const { TOKEN, COMMAND_CHANNEL_ID, ADDUP_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) => {
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 (interaction.commandName === "hello") {
await interaction.reply("world");
}
if (
interaction.commandName === "pullTeams" ||
interaction.commandName === "end"
) {
await interaction.deferReply({ ephemeral: true });
// 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'!");
// get members in voice channel
const members = blu.members.concat(red.members);
if (members.size === 0) {
return await interaction.editReply("found no members in teams");
}
// move members to addup
let idx = 0,
eCount = 0;
members.forEach(async (member) => {
idx++;
try {
await member.voice.setChannel(addup);
} catch (error) {
console.error(error);
eCount++;
} finally {
// respond when done
if (idx === members.size) {
if (eCount > 0) {
return await interaction.editReply({
content: `moved teams, error moving ${eCount} members`,
});
}
return await interaction.editReply({
content: `moved teams`,
});
}
}
});
}
if (
interaction.commandName === "fk" ||
interaction.commandName === "fatkid"
) {
await interaction.deferReply();
// get voice channels
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 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 = addup.members;
if (members.size === 0) {
return await interaction.editReply("found no members in addup");
}
// 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}, error moving ${eCount} members`,
});
}
return await interaction.editReply({
content: `fatkids: ${str}`,
});
}
}
});
}
});
client.login(TOKEN);