Compare commits
2 Commits
d48d4e67bd
...
ae3a1be106
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae3a1be106 | ||
|
|
8fd097ba67 |
20
commands/roll.js
Normal file
20
commands/roll.js
Normal 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
156
index.js
@ -1,6 +1,6 @@
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path, { parse } from "path";
|
import path, { parse } from "path";
|
||||||
import { Client, GatewayIntentBits, Partials } from "discord.js";
|
import { Client, Collection, GatewayIntentBits, Partials } from "discord.js";
|
||||||
const {
|
const {
|
||||||
TOKEN,
|
TOKEN,
|
||||||
GUILD_ID,
|
GUILD_ID,
|
||||||
@ -27,6 +27,20 @@ const client = new Client({
|
|||||||
partials: [Partials.Channel, Partials.Message],
|
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 backticks = "```";
|
||||||
const rankingsPath = path.join(__dirname, "rankings.json");
|
const rankingsPath = path.join(__dirname, "rankings.json");
|
||||||
|
|
||||||
@ -190,6 +204,40 @@ const balanceArrays = (a1, a2) => {
|
|||||||
return [modArr1, modArr2];
|
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", () => {
|
client.on("ready", () => {
|
||||||
console.log(`Logged in as ${client.user.tag}!`);
|
console.log(`Logged in as ${client.user.tag}!`);
|
||||||
console.log(
|
console.log(
|
||||||
@ -312,10 +360,6 @@ client.on("interactionCreate", async (interaction) => {
|
|||||||
(channel) => channel.name === "red" || channel.id === RED_ID
|
(channel) => channel.name === "red" || channel.id === RED_ID
|
||||||
);
|
);
|
||||||
if (!red) return console.error("Can't find channel 'red'!");
|
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(
|
const picking = interaction.guild.channels.cache.find(
|
||||||
(channel) => channel.name === "picking" || channel.id === PICKING_ID
|
(channel) => channel.name === "picking" || channel.id === PICKING_ID
|
||||||
);
|
);
|
||||||
@ -323,20 +367,14 @@ client.on("interactionCreate", async (interaction) => {
|
|||||||
|
|
||||||
// get members in voice channel
|
// get members in voice channel
|
||||||
let members = blu.members.concat(red.members);
|
let members = blu.members.concat(red.members);
|
||||||
//if (command !== "resetteams") members = members.concat(addup.members);
|
|
||||||
if (members.size === 0) {
|
if (members.size === 0) {
|
||||||
return await interaction.followUp(
|
return await interaction.followUp("Found no members in blu or red");
|
||||||
`Found no members in ${
|
|
||||||
command === "resetteams" ? "blu" : "addup, blu,"
|
|
||||||
} or red`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// move members to addup
|
let eCount = 0;
|
||||||
let idx = 0,
|
|
||||||
eCount = 0;
|
|
||||||
|
|
||||||
const moveToAddup = async (member) => {
|
// move members to picking
|
||||||
|
const moveToPicking = async (member) => {
|
||||||
try {
|
try {
|
||||||
await member.voice.setChannel(picking);
|
await member.voice.setChannel(picking);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -345,19 +383,19 @@ client.on("interactionCreate", async (interaction) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const moveAllToAddup = async () => {
|
const moveAllToPicking = async () => {
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
Array.from(members, async ([memberId, member]) => {
|
Array.from(members, async ([memberId, member]) => {
|
||||||
await moveToAddup(member);
|
await moveToPicking(member);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
moveAllToAddup().then(() =>
|
moveAllToPicking().then(() =>
|
||||||
interaction.followUp(
|
interaction.followUp(
|
||||||
`Moved members in ${
|
`Moved members in blu and red${
|
||||||
command === "resetteams" ? "blu" : "addup, blu,"
|
eCount > 0 ? ` (error moving ${eCount} members)` : ""
|
||||||
} and red${eCount > 0 ? ` (error moving ${eCount} members)` : ""}`
|
}`
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -379,6 +417,7 @@ client.on("interactionCreate", async (interaction) => {
|
|||||||
const pickingPlayers = [];
|
const pickingPlayers = [];
|
||||||
addup.members.forEach((member) => addupPlayers.push(member));
|
addup.members.forEach((member) => addupPlayers.push(member));
|
||||||
picking.members.forEach((member) => pickingPlayers.push(member));
|
picking.members.forEach((member) => pickingPlayers.push(member));
|
||||||
|
|
||||||
if (pickingPlayers.length < addupPlayers.length) {
|
if (pickingPlayers.length < addupPlayers.length) {
|
||||||
// move all addup into empty picking
|
// move all addup into empty picking
|
||||||
addup.members.forEach((member) => {
|
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"
|
"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
|
// select excess players to be sat out
|
||||||
while (pickingPlayers.length + addupPlayers.length > 18) {
|
while (pickingPlayers.length + addupPlayers.length > targetPlayerCount) {
|
||||||
if (pickingPlayers.length === 0) {
|
if (pickingPlayers.length === 0) {
|
||||||
return await interaction.followUp(
|
return await interaction.followUp(
|
||||||
"Error: ran out of players in picking to fatkid"
|
"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);
|
const idx = Math.floor(Math.random() * pickingPlayers.length);
|
||||||
let fk = await pickingPlayers.splice(idx, 1)[0];
|
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);
|
fatkids.push(fk);
|
||||||
}
|
}
|
||||||
|
if (protectedStr.length > 0) {
|
||||||
|
console.log(`Protected fks: ${protectedStr}`);
|
||||||
|
}
|
||||||
|
|
||||||
let errCount = 0;
|
let errCount = 0;
|
||||||
|
|
||||||
@ -415,9 +492,12 @@ client.on("interactionCreate", async (interaction) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let fkIds = [];
|
||||||
|
|
||||||
// move players from picking to fatkid
|
// move players from picking to fatkid
|
||||||
for (const fk of fatkids) {
|
for (const fk of fatkids) {
|
||||||
try {
|
try {
|
||||||
|
fkIds.push(fk.id);
|
||||||
await fk.voice.setChannel(addup);
|
await fk.voice.setChannel(addup);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(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(
|
interaction.followUp(
|
||||||
`Sat out ${fatkids.length} players${
|
`Sat out ${fatkids.length} players${
|
||||||
errCount > 0 ? ` (error moving ${errCount} members)` : ""
|
errCount > 0 ? ` (error moving ${errCount} members)` : ""
|
||||||
|
}${
|
||||||
|
fkAttempts > 100
|
||||||
|
? ` (fk protection sets ignored: ${3 - recentFkSets.length}/3)`
|
||||||
|
: ""
|
||||||
}`
|
}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (command === "testfk") {
|
/*if (command === "testfk") {
|
||||||
// debug fk
|
// 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
|
// 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...");
|
await interaction.reply("Randomly choosing fatkids from picking...");
|
||||||
@ -504,7 +595,7 @@ client.on("interactionCreate", async (interaction) => {
|
|||||||
errCount > 0 ? ` (error moving ${errCount} members)` : ""
|
errCount > 0 ? ` (error moving ${errCount} members)` : ""
|
||||||
}`
|
}`
|
||||||
);
|
);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
if (command === "fklist" || command === "listfk") {
|
if (command === "fklist" || command === "listfk") {
|
||||||
// moves players in picking to fatkid channel, for use in captain pugs
|
// moves players in picking to fatkid channel, for use in captain pugs
|
||||||
@ -714,6 +805,19 @@ client.on("interactionCreate", async (interaction) => {
|
|||||||
})
|
})
|
||||||
.catch(console.error);
|
.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,6 +1,9 @@
|
|||||||
import { REST, Routes } from "discord.js";
|
import { REST, Routes } from "discord.js";
|
||||||
const { TOKEN, CLIENT_ID, GUILD_ID } = require("./config.json");
|
const { TOKEN, CLIENT_ID, GUILD_ID } = require("./config.json");
|
||||||
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
// set static commands
|
||||||
const commands = [
|
const commands = [
|
||||||
{
|
{
|
||||||
name: "hello",
|
name: "hello",
|
||||||
@ -26,10 +29,10 @@ const commands = [
|
|||||||
name: "fk",
|
name: "fk",
|
||||||
description: "Moves added players to picking and randomly selects fatkids",
|
description: "Moves added players to picking and randomly selects fatkids",
|
||||||
},
|
},
|
||||||
{
|
/*{
|
||||||
name: "testfk",
|
name: "testfk",
|
||||||
description: "debug fk",
|
description: "debug fk",
|
||||||
},
|
},*/
|
||||||
{
|
{
|
||||||
name: "fklist",
|
name: "fklist",
|
||||||
description: "Pulls addup channel members into fk channel and lists them",
|
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);
|
const rest = new REST({ version: "10" }).setToken(TOKEN);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user