feat: added variable role permissions
This commit is contained in:
parent
7a65e95fe0
commit
df85f29f73
@ -1,5 +1,5 @@
|
||||
import { SlashCommandBuilder } from "discord.js";
|
||||
import { ADDUP_ID, PICKING_ID } from "../config.json";
|
||||
import { RUNNER_ROLE_ID, ADDUP_ID, PICKING_ID } from "../config.json";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
@ -56,6 +56,8 @@ const data = new SlashCommandBuilder()
|
||||
.setDescription("Player to protect from sitting out, intended for medic")
|
||||
);
|
||||
|
||||
const permissions = [RUNNER_ROLE_ID];
|
||||
|
||||
const execute = async (interaction) => {
|
||||
// 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...");
|
||||
@ -214,4 +216,4 @@ const execute = async (interaction) => {
|
||||
interaction.followUp(fkStr);
|
||||
};
|
||||
|
||||
export { data, execute };
|
||||
export { data, permissions, execute };
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { SlashCommandBuilder } from "discord.js";
|
||||
import { PLAYER_ROLE_ID } from "../config.json";
|
||||
|
||||
const data = new SlashCommandBuilder()
|
||||
.setName("roll")
|
||||
@ -11,10 +12,12 @@ const data = new SlashCommandBuilder()
|
||||
.setMinValue(1)
|
||||
);
|
||||
|
||||
const permissions = [PLAYER_ROLE_ID];
|
||||
|
||||
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 };
|
||||
export { data, permissions, execute };
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
"TOKEN": "redacted",
|
||||
"CLIENT_ID": "1177748390092742767",
|
||||
"GUILD_ID": "1175646363707523132",
|
||||
"PLAYER_ROLE_ID": "1175646999140384778",
|
||||
"RUNNER_ROLE_ID": "1175646976235282452",
|
||||
"COMMAND_CHANNEL_ID": "1177469184192565288",
|
||||
"ADDUP_ID": "1175649994674544721",
|
||||
"PICKING_ID": "1175649967935856680",
|
||||
|
||||
287
index.js
287
index.js
@ -4,8 +4,8 @@ import { Client, Collection, GatewayIntentBits, Partials } from "discord.js";
|
||||
const {
|
||||
TOKEN,
|
||||
GUILD_ID,
|
||||
RUNNER_ROLE_ID,
|
||||
COMMAND_CHANNEL_ID,
|
||||
ADDUP_ID,
|
||||
PICKING_ID,
|
||||
BLU_ID,
|
||||
RED_ID,
|
||||
@ -204,40 +204,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(
|
||||
@ -272,6 +238,39 @@ client.on("interactionCreate", async (interaction) => {
|
||||
const command = interaction.commandName;
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
|
||||
const fileCommand = client.fileCommands.get(command);
|
||||
if (fileCommand) {
|
||||
try {
|
||||
if (fileCommand.permissions) {
|
||||
const member = interaction.member;
|
||||
for (const permission of fileCommand.permissions) {
|
||||
if (!member.roles.cache.has(permission)) {
|
||||
await interaction.reply({
|
||||
content: "You lack the required permissions",
|
||||
ephemeral: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
await fileCommand.execute(interaction);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
await interaction.reply({
|
||||
content: "Internal error executing command from file",
|
||||
ephemeral: true,
|
||||
});
|
||||
}
|
||||
} else if (interaction.channelId !== COMMAND_CHANNEL_ID) {
|
||||
//let isRunner = await interaction.member.roles.cache.has(RUNNER_ROLE_ID);
|
||||
//if (!isRunner) {
|
||||
await interaction.reply({
|
||||
content: "Wrong channel, or you lack the required permissions",
|
||||
ephemeral: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
command === "scout" ||
|
||||
command === "soldier" ||
|
||||
@ -332,14 +331,6 @@ client.on("interactionCreate", async (interaction) => {
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
@ -400,203 +391,6 @@ client.on("interactionCreate", async (interaction) => {
|
||||
);
|
||||
}
|
||||
|
||||
/*if (command === "fk" || command === "fatkid") {
|
||||
// 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...");
|
||||
|
||||
// get voice channels
|
||||
const addup = interaction.guild.channels.cache.find(
|
||||
(channel) => channel.name === "add-up" || channel.id === ADDUP_ID
|
||||
);
|
||||
const picking = interaction.guild.channels.cache.find(
|
||||
(channel) => channel.name === "picking" || channel.id === PICKING_ID
|
||||
);
|
||||
|
||||
// get members in voice channel
|
||||
const addupPlayers = [];
|
||||
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) => {
|
||||
member.voice.setChannel(picking);
|
||||
});
|
||||
return await interaction.followUp(
|
||||
"Found too few players in picking to fatkid, moved all addup players to picking instead"
|
||||
);
|
||||
}
|
||||
|
||||
const targetPlayerCount = 18;
|
||||
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 > 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 = pickingPlayers[idx];
|
||||
if (protectedFk.includes(fk.id)) {
|
||||
protectedStr += `${fk.id}, `;
|
||||
} else {
|
||||
pickingPlayers.splice(idx, 1);
|
||||
fatkids.push(fk);
|
||||
}
|
||||
}
|
||||
if (protectedStr.length > 0) {
|
||||
console.log(`Protected fks: ${protectedStr}`);
|
||||
}
|
||||
|
||||
let errCount = 0;
|
||||
|
||||
// move players from addup to picking
|
||||
for (const newPlayer of addupPlayers) {
|
||||
try {
|
||||
await newPlayer.voice.setChannel(picking);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
errCount++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
errCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 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") {
|
||||
// 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...");
|
||||
|
||||
// get voice channels
|
||||
const addup = interaction.guild.channels.cache.find(
|
||||
(channel) => channel.name === "add-up" || channel.id === ADDUP_ID
|
||||
);
|
||||
const picking = interaction.guild.channels.cache.find(
|
||||
(channel) => channel.name === "picking" || channel.id === PICKING_ID
|
||||
);
|
||||
|
||||
// get members in voice channel
|
||||
//const addupPlayers = Array.from(addup.members.values());
|
||||
//const pickingPlayers = Array.from(picking.members.values());
|
||||
const addupPlayers = [];
|
||||
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) => {
|
||||
member.voice.setChannel(picking);
|
||||
});
|
||||
return await interaction.followUp(
|
||||
"Found too few players in picking to fatkid, moved all addup players to picking instead"
|
||||
);
|
||||
}
|
||||
let fatkids = [];
|
||||
|
||||
// select excess players to be sat out
|
||||
while (pickingPlayers.length + addupPlayers.length > 3) {
|
||||
if (pickingPlayers.length === 0) {
|
||||
return await interaction.followUp(
|
||||
"Error: ran out of players in picking to fatkid"
|
||||
);
|
||||
}
|
||||
const idx = Math.floor(Math.random() * pickingPlayers.length);
|
||||
let fk = await pickingPlayers.splice(idx, 1)[0];
|
||||
console.log(fk);
|
||||
fatkids.push(fk);
|
||||
}
|
||||
|
||||
let errCount = 0;
|
||||
|
||||
// move players from addup to picking
|
||||
for (const newPlayer of addupPlayers) {
|
||||
try {
|
||||
await newPlayer.voice.setChannel(picking);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
errCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// move players from picking to fatkid
|
||||
for (const fk of fatkids) {
|
||||
try {
|
||||
// if (picking.members.size <= 18) break;
|
||||
await fk.voice.setChannel(addup);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
errCount++;
|
||||
}
|
||||
}
|
||||
|
||||
interaction.followUp(
|
||||
`Sat out ${fatkids.length} players${
|
||||
errCount > 0 ? ` (error moving ${errCount} members)` : ""
|
||||
}`
|
||||
);
|
||||
}*/
|
||||
|
||||
if (command === "fklist" || command === "listfk") {
|
||||
// moves players in picking to fatkid channel, for use in captain pugs
|
||||
await interaction.reply("Moving fatkids...");
|
||||
@ -830,19 +624,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,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
|
||||
Loading…
Reference in New Issue
Block a user