feat: edit whitelist with bot

This commit is contained in:
ethanf 2024-01-23 17:11:22 -06:00
parent 7c0bee42db
commit 163f3fe881
2 changed files with 110 additions and 25 deletions

View File

@ -9,5 +9,5 @@
"RED_ID": "1175649777648680971",
"FK_ID": "1176396207183106078",
"CAPTAIN_ID": "1178475124563919018",
"RANKING_WHITELIST": "233036215610245120,"
"RANKING_WHITELIST": "233036215610245120"
}

133
index.js
View File

@ -11,9 +11,10 @@ const {
RED_ID,
FK_ID,
CAPTAIN_ID,
RANKING_WHITELIST,
} = require("./config.json");
let whitelistStr = require("./config.json").RANKING_WHITELIST;
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
@ -58,6 +59,15 @@ const findPlayer = (guild, searchName) => {
return player;
};
const getApplicableName = (player) => {
return (
player.displayName ||
player.user.globalName ||
player.user.username ||
player.id
);
};
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
console.log(
@ -301,7 +311,7 @@ client.on("messageCreate", async (message) => {
if (message.author.bot || message.guild) return;
// check if user is whitelisted
if (!RANKING_WHITELIST.includes(message.author.id)) {
if (!whitelistStr.includes(message.author.id)) {
return;
}
@ -310,6 +320,12 @@ client.on("messageCreate", async (message) => {
return;
}
const pickupGuild = client.guilds.cache.get(GUILD_ID);
if (!pickupGuild) {
await message.reply("could not find guild");
return;
}
const args = message.content.toLowerCase().split(" ");
if (args[0] === "setrank") {
@ -320,12 +336,6 @@ client.on("messageCreate", async (message) => {
return;
}
const pickupGuild = client.guilds.cache.get(GUILD_ID);
if (!pickupGuild) {
await message.reply("could not find guild");
return;
}
const player = findPlayer(pickupGuild, args[1]);
if (!player) {
await message.reply(
@ -335,11 +345,7 @@ client.on("messageCreate", async (message) => {
}
const playerId = player.id;
const applicableName =
player.displayName ||
player.user.globalName ||
player.user.username ||
playerId;
const applicableName = getApplicableName(player);
const rank = parseInt(args[2]);
if (isNaN(rank) || rank < 0 || rank > 5) {
@ -385,12 +391,6 @@ client.on("messageCreate", async (message) => {
return;
}
const pickupGuild = client.guilds.cache.get(GUILD_ID);
if (!pickupGuild) {
await message.reply("could not find guild");
return;
}
const player = findPlayer(pickupGuild, args[1]);
if (!player) {
await message.reply(
@ -400,11 +400,7 @@ client.on("messageCreate", async (message) => {
}
const playerId = player.id;
const applicableName =
player.displayName ||
player.user.globalName ||
player.user.username ||
playerId;
const applicableName = getApplicableName(player);
try {
console.log(
@ -421,6 +417,95 @@ client.on("messageCreate", async (message) => {
await message.reply(`error getting rank: ${error.message}`);
}
}
if (args[0] === "whitelist") {
// add user to config.json whitelist
if (args.length < 2) {
await message.reply(
"invalid number of arguments. usage: `whitelist <name>`"
);
return;
}
const name = args[1];
const player = findPlayer(pickupGuild, name);
if (!player) {
await message.reply(
`could not find player ${name}. if this issue persists, try copy/pasting the player's discord handle or user id`
);
return;
}
const playerId = player.id;
const applicableName = getApplicableName(player);
if (whitelistStr.includes(playerId)) {
await message.reply(
`user ${applicableName} (${playerId}) is already whitelisted`
);
return;
}
const configPath = path.join(__dirname, "config.json");
const config = JSON.parse(fs.readFileSync(configPath));
whitelistStr += `,${playerId}`;
config.RANKING_WHITELIST = whitelistStr;
new Promise((resolve, reject) => {
fs.writeFile(configPath, JSON.stringify(config), (err) => {
if (err) reject(err);
else resolve();
});
}).then((res, err) => {
if (err) {
console.error(err);
message.reply(`error whitelisting user: ${err.message}`);
} else {
message.reply(
`whitelisted user ${applicableName} (${playerId}). if this was done in error, please contact an admin`
);
}
});
}
if (args[0] === "getwhitelist") {
let str = `${backticks}${whitelistStr}${backticks}${backticks}`
const whitelistIds = whitelistStr.split(",");
for (const id of whitelistIds) {
const player = findPlayer(pickupGuild, id);
if (player) {
str += `\n${getApplicableName(player)}`;
}
}
str += `${backticks}`;
await message.reply(str);
}
if (args[0] === "clearwhitelist") {
if (args[1] !== "confirm") {
await message.reply(
"this command will clear the whitelist and prevent further dm commands. this will require manual intervention to undo. to confirm, use `clearwhitelist confirm`"
);
return;
} else {
const configPath = path.join(__dirname, "config.json");
const config = JSON.parse(fs.readFileSync(configPath));
whitelistStr = "";
config.RANKING_WHITELIST = whitelistStr;
new Promise((resolve, reject) => {
fs.writeFile(configPath, JSON.stringify(config), (err) => {
if (err) reject(err);
else resolve();
});
}).then((res, err) => {
if (err) {
console.error(err);
message.reply(`error clearing whitelist: ${err.message}`);
} else {
message.reply("whitelist cleared, please alert an admin");
}
});
}
}
});
client.login(TOKEN);