Compare commits

...

2 Commits

Author SHA1 Message Date
ethanf
ae3a1be106 feat: roll command with file command handlers 2024-02-01 16:08:24 -06:00
ethanf
8fd097ba67 feat: fk protection 2024-02-01 14:55:11 -06:00
3 changed files with 168 additions and 28 deletions

20
commands/roll.js Normal file
View File

@ -0,0 +1,20 @@
import { SlashCommandBuilder } from "discord.js";
const data = new SlashCommandBuilder()
.setName("roll")
.setDescription("Prints a random number between 1 and input")
.addIntegerOption((option) =>
option
.setName("max")
.setDescription("Maximum output number")
.setRequired(true)
.setMinValue(1)
);
const execute = async (interaction) => {
const max = interaction.options.getInteger("max");
const roll = Math.floor(Math.random() * max) + 1;
await interaction.reply(`🎲 Rolled a ${roll} (max: ${max})`);
};
export { data, execute };

156
index.js
View File

@ -1,6 +1,6 @@
import fs from "fs";
import path, { parse } from "path";
import { Client, GatewayIntentBits, Partials } from "discord.js";
import { Client, Collection, GatewayIntentBits, Partials } from "discord.js";
const {
TOKEN,
GUILD_ID,
@ -27,6 +27,20 @@ const client = new Client({
partials: [Partials.Channel, Partials.Message],
});
client.fileCommands = new Collection();
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs.readdirSync(commandsPath);
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
if ("data" in command && "execute" in command) {
client.fileCommands.set(command.data.name, command);
} else {
console.error(`${__filename}: Invalid command file: ${file}`);
}
}
const backticks = "```";
const rankingsPath = path.join(__dirname, "rankings.json");
@ -190,6 +204,40 @@ const balanceArrays = (a1, a2) => {
return [modArr1, modArr2];
};
const storeFk = async (idArr) => {
// store fk history to avoid repeats
if (!idArr || typeof idArr !== "object")
return console.error("Cannot store fk history");
if (!fs.existsSync("fk.json")) {
fs.writeFileSync("fk.json", "[]");
}
let fkHistory = JSON.parse(fs.readFileSync("fk.json"));
// add to history
fkHistory.push(idArr);
if (fkHistory.length > 10) fkHistory.shift();
try {
fs.writeFileSync("fk.json", JSON.stringify(fkHistory));
} catch (error) {
console.error(error);
}
return;
};
const getPastFk = async () => {
// get 3 most recent sets of fks to avoid repeats
if (!fs.existsSync("fk.json")) {
fs.writeFileSync("fk.json", "[]");
}
let fkHistory = JSON.parse(fs.readFileSync("fk.json"));
let recentFk = fkHistory.slice(-3);
if (!recentFk || typeof recentFk !== "object")
console.error("Invalid fk history");
if (recentFk.length === 0) return [[], [], []];
return recentFk;
};
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
console.log(
@ -312,10 +360,6 @@ client.on("interactionCreate", async (interaction) => {
(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
);
@ -323,20 +367,14 @@ client.on("interactionCreate", async (interaction) => {
// 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`
);
return await interaction.followUp("Found no members in blu or red");
}
// move members to addup
let idx = 0,
eCount = 0;
let eCount = 0;
const moveToAddup = async (member) => {
// move members to picking
const moveToPicking = async (member) => {
try {
await member.voice.setChannel(picking);
} catch (error) {
@ -345,19 +383,19 @@ client.on("interactionCreate", async (interaction) => {
}
};
const moveAllToAddup = async () => {
const moveAllToPicking = async () => {
return Promise.all(
Array.from(members, async ([memberId, member]) => {
await moveToAddup(member);
await moveToPicking(member);
})
);
};
moveAllToAddup().then(() =>
moveAllToPicking().then(() =>
interaction.followUp(
`Moved members in ${
command === "resetteams" ? "blu" : "addup, blu,"
} and red${eCount > 0 ? ` (error moving ${eCount} members)` : ""}`
`Moved members in blu and red${
eCount > 0 ? ` (error moving ${eCount} members)` : ""
}`
)
);
}
@ -379,6 +417,7 @@ client.on("interactionCreate", async (interaction) => {
const pickingPlayers = [];
addup.members.forEach((member) => addupPlayers.push(member));
picking.members.forEach((member) => pickingPlayers.push(member));
if (pickingPlayers.length < addupPlayers.length) {
// move all addup into empty picking
addup.members.forEach((member) => {
@ -388,20 +427,58 @@ client.on("interactionCreate", async (interaction) => {
"Found too few players in picking to fatkid, moved all addup players to picking instead"
);
}
let fatkids = [];
const targetPlayerCount = 3;
if (pickingPlayers.length + addupPlayers.length < targetPlayerCount) {
return await interaction.followUp(
`Can't find enough total players, expected ${targetPlayerCount} (found ${
pickingPlayers.length + addupPlayers.length
})`
);
}
// set up past fk protection
let fatkids = [],
recentFkSets = [],
protectedFk = [],
protectedStr = "",
fkAttempts = 0;
try {
recentFkSets = await getPastFk();
protectedFk = recentFkSets.flat();
} catch (error) {
console.error(error);
await interaction.followUp(
"Failed to get history, fk protection may not be applied"
);
}
// select excess players to be sat out
while (pickingPlayers.length + addupPlayers.length > 18) {
while (pickingPlayers.length + addupPlayers.length > targetPlayerCount) {
if (pickingPlayers.length === 0) {
return await interaction.followUp(
"Error: ran out of players in picking to fatkid"
);
}
// remove earliest fk sets as attempts become unreasonably high
if (fkAttempts === 100 || fkAttempts === 250 || fkAttempts === 500) {
console.log(`Shifting fk history ${fkAttempts}`);
recentFkSets.shift();
protectedFk = recentFkSets.flat();
}
fkAttempts++;
const idx = Math.floor(Math.random() * pickingPlayers.length);
let fk = await pickingPlayers.splice(idx, 1)[0];
console.log(fk);
if (protectedFk.includes(fk.id)) {
protectedStr += `${fk.id}, `;
pickingPlayers.push(fk);
continue;
}
fatkids.push(fk);
}
if (protectedStr.length > 0) {
console.log(`Protected fks: ${protectedStr}`);
}
let errCount = 0;
@ -415,9 +492,12 @@ client.on("interactionCreate", async (interaction) => {
}
}
let fkIds = [];
// move players from picking to fatkid
for (const fk of fatkids) {
try {
fkIds.push(fk.id);
await fk.voice.setChannel(addup);
} catch (error) {
console.error(error);
@ -425,14 +505,25 @@ client.on("interactionCreate", async (interaction) => {
}
}
// store fatkid history
try {
storeFk(fkIds);
} catch (error) {
console.error(error);
}
interaction.followUp(
`Sat out ${fatkids.length} players${
errCount > 0 ? ` (error moving ${errCount} members)` : ""
}${
fkAttempts > 100
? ` (fk protection sets ignored: ${3 - recentFkSets.length}/3)`
: ""
}`
);
}
if (command === "testfk") {
/*if (command === "testfk") {
// debug fk
// pull users in addup into picking, then randomly select other users in picking to be sat out until there are 18 in picking
await interaction.reply("Randomly choosing fatkids from picking...");
@ -504,7 +595,7 @@ client.on("interactionCreate", async (interaction) => {
errCount > 0 ? ` (error moving ${errCount} members)` : ""
}`
);
}
}*/
if (command === "fklist" || command === "listfk") {
// moves players in picking to fatkid channel, for use in captain pugs
@ -714,6 +805,19 @@ client.on("interactionCreate", async (interaction) => {
})
.catch(console.error);
}
const fileCommand = client.fileCommands.get(command);
if (!fileCommand) return;
try {
await fileCommand.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "Internal error executing command from file",
ephemeral: true,
});
}
});
/*

View File

@ -1,6 +1,9 @@
import { REST, Routes } from "discord.js";
const { TOKEN, CLIENT_ID, GUILD_ID } = require("./config.json");
const fs = require("fs");
const path = require("path");
// set static commands
const commands = [
{
name: "hello",
@ -26,10 +29,10 @@ const commands = [
name: "fk",
description: "Moves added players to picking and randomly selects fatkids",
},
{
/*{
name: "testfk",
description: "debug fk",
},
},*/
{
name: "fklist",
description: "Pulls addup channel members into fk channel and lists them",
@ -100,6 +103,19 @@ const commands = [
},
];
// get commands from files
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs.readdirSync(commandsPath);
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
if ("data" in command && "execute" in command) {
commands.push(command.data.toJSON());
} else {
console.error(`${__filename}: Invalid command file: ${file}`);
}
}
const rest = new REST({ version: "10" }).setToken(TOKEN);
try {