Compare commits
No commits in common. "ae3a1be106c6aa824eb0a82263515f960b9fc947" and "d48d4e67bd8c827ddb40b335667a631b90ef99ce" have entirely different histories.
ae3a1be106
...
d48d4e67bd
@ -1,20 +0,0 @@
|
||||
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
156
index.js
@ -1,6 +1,6 @@
|
||||
import fs from "fs";
|
||||
import path, { parse } from "path";
|
||||
import { Client, Collection, GatewayIntentBits, Partials } from "discord.js";
|
||||
import { Client, GatewayIntentBits, Partials } from "discord.js";
|
||||
const {
|
||||
TOKEN,
|
||||
GUILD_ID,
|
||||
@ -27,20 +27,6 @@ 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");
|
||||
|
||||
@ -204,40 +190,6 @@ 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(
|
||||
@ -360,6 +312,10 @@ 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
|
||||
);
|
||||
@ -367,14 +323,20 @@ 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 blu or red");
|
||||
return await interaction.followUp(
|
||||
`Found no members in ${
|
||||
command === "resetteams" ? "blu" : "addup, blu,"
|
||||
} or red`
|
||||
);
|
||||
}
|
||||
|
||||
let eCount = 0;
|
||||
// move members to addup
|
||||
let idx = 0,
|
||||
eCount = 0;
|
||||
|
||||
// move members to picking
|
||||
const moveToPicking = async (member) => {
|
||||
const moveToAddup = async (member) => {
|
||||
try {
|
||||
await member.voice.setChannel(picking);
|
||||
} catch (error) {
|
||||
@ -383,19 +345,19 @@ client.on("interactionCreate", async (interaction) => {
|
||||
}
|
||||
};
|
||||
|
||||
const moveAllToPicking = async () => {
|
||||
const moveAllToAddup = async () => {
|
||||
return Promise.all(
|
||||
Array.from(members, async ([memberId, member]) => {
|
||||
await moveToPicking(member);
|
||||
await moveToAddup(member);
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
moveAllToPicking().then(() =>
|
||||
moveAllToAddup().then(() =>
|
||||
interaction.followUp(
|
||||
`Moved members in blu and red${
|
||||
eCount > 0 ? ` (error moving ${eCount} members)` : ""
|
||||
}`
|
||||
`Moved members in ${
|
||||
command === "resetteams" ? "blu" : "addup, blu,"
|
||||
} and red${eCount > 0 ? ` (error moving ${eCount} members)` : ""}`
|
||||
)
|
||||
);
|
||||
}
|
||||
@ -417,7 +379,6 @@ 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) => {
|
||||
@ -427,58 +388,20 @@ client.on("interactionCreate", async (interaction) => {
|
||||
"Found too few players in picking to fatkid, moved all addup players to picking instead"
|
||||
);
|
||||
}
|
||||
|
||||
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"
|
||||
);
|
||||
}
|
||||
let fatkids = [];
|
||||
|
||||
// select excess players to be sat out
|
||||
while (pickingPlayers.length + addupPlayers.length > targetPlayerCount) {
|
||||
while (pickingPlayers.length + addupPlayers.length > 18) {
|
||||
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];
|
||||
if (protectedFk.includes(fk.id)) {
|
||||
protectedStr += `${fk.id}, `;
|
||||
pickingPlayers.push(fk);
|
||||
continue;
|
||||
}
|
||||
console.log(fk);
|
||||
fatkids.push(fk);
|
||||
}
|
||||
if (protectedStr.length > 0) {
|
||||
console.log(`Protected fks: ${protectedStr}`);
|
||||
}
|
||||
|
||||
let errCount = 0;
|
||||
|
||||
@ -492,12 +415,9 @@ 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);
|
||||
@ -505,25 +425,14 @@ 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...");
|
||||
@ -595,7 +504,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
|
||||
@ -805,19 +714,6 @@ 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,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
|
||||
20
register.js
20
register.js
@ -1,9 +1,6 @@
|
||||
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",
|
||||
@ -29,10 +26,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",
|
||||
@ -103,19 +100,6 @@ 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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user