Compare commits
No commits in common. "7217e968ddd88e694dbb3401ade00b70339aae59" and "7c0bee42dbcafa5c183e4b42f7b851859beba8f3" have entirely different histories.
7217e968dd
...
7c0bee42db
@ -9,5 +9,5 @@
|
|||||||
"RED_ID": "1175649777648680971",
|
"RED_ID": "1175649777648680971",
|
||||||
"FK_ID": "1176396207183106078",
|
"FK_ID": "1176396207183106078",
|
||||||
"CAPTAIN_ID": "1178475124563919018",
|
"CAPTAIN_ID": "1178475124563919018",
|
||||||
"RANKING_WHITELIST": "233036215610245120"
|
"RANKING_WHITELIST": "233036215610245120,"
|
||||||
}
|
}
|
||||||
|
|||||||
176
index.js
176
index.js
@ -11,10 +11,9 @@ const {
|
|||||||
RED_ID,
|
RED_ID,
|
||||||
FK_ID,
|
FK_ID,
|
||||||
CAPTAIN_ID,
|
CAPTAIN_ID,
|
||||||
|
RANKING_WHITELIST,
|
||||||
} = require("./config.json");
|
} = require("./config.json");
|
||||||
|
|
||||||
let whitelistStr = require("./config.json").RANKING_WHITELIST;
|
|
||||||
|
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: [
|
intents: [
|
||||||
GatewayIntentBits.Guilds,
|
GatewayIntentBits.Guilds,
|
||||||
@ -59,15 +58,6 @@ const findPlayer = (guild, searchName) => {
|
|||||||
return player;
|
return player;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getApplicableName = (player) => {
|
|
||||||
return (
|
|
||||||
player.displayName ||
|
|
||||||
player.user.globalName ||
|
|
||||||
player.user.username ||
|
|
||||||
player.id
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
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(
|
||||||
@ -311,7 +301,7 @@ client.on("messageCreate", async (message) => {
|
|||||||
if (message.author.bot || message.guild) return;
|
if (message.author.bot || message.guild) return;
|
||||||
|
|
||||||
// check if user is whitelisted
|
// check if user is whitelisted
|
||||||
if (!whitelistStr.includes(message.author.id)) {
|
if (!RANKING_WHITELIST.includes(message.author.id)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,12 +310,6 @@ client.on("messageCreate", async (message) => {
|
|||||||
return;
|
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(" ");
|
const args = message.content.toLowerCase().split(" ");
|
||||||
|
|
||||||
if (args[0] === "setrank") {
|
if (args[0] === "setrank") {
|
||||||
@ -336,6 +320,12 @@ client.on("messageCreate", async (message) => {
|
|||||||
return;
|
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]);
|
const player = findPlayer(pickupGuild, args[1]);
|
||||||
if (!player) {
|
if (!player) {
|
||||||
await message.reply(
|
await message.reply(
|
||||||
@ -345,7 +335,11 @@ client.on("messageCreate", async (message) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const playerId = player.id;
|
const playerId = player.id;
|
||||||
const applicableName = getApplicableName(player);
|
const applicableName =
|
||||||
|
player.displayName ||
|
||||||
|
player.user.globalName ||
|
||||||
|
player.user.username ||
|
||||||
|
playerId;
|
||||||
|
|
||||||
const rank = parseInt(args[2]);
|
const rank = parseInt(args[2]);
|
||||||
if (isNaN(rank) || rank < 0 || rank > 5) {
|
if (isNaN(rank) || rank < 0 || rank > 5) {
|
||||||
@ -391,6 +385,12 @@ client.on("messageCreate", async (message) => {
|
|||||||
return;
|
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]);
|
const player = findPlayer(pickupGuild, args[1]);
|
||||||
if (!player) {
|
if (!player) {
|
||||||
await message.reply(
|
await message.reply(
|
||||||
@ -400,15 +400,16 @@ client.on("messageCreate", async (message) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const playerId = player.id;
|
const playerId = player.id;
|
||||||
const applicableName = getApplicableName(player);
|
const applicableName =
|
||||||
|
player.displayName ||
|
||||||
|
player.user.globalName ||
|
||||||
|
player.user.username ||
|
||||||
|
playerId;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(
|
console.log(
|
||||||
`getting rank of ${applicableName}, ${playerId} at ${rankingsPath}...`
|
`getting rank of ${applicableName}, ${playerId} at ${rankingsPath}...`
|
||||||
);
|
);
|
||||||
if (!fs.existsSync(rankingsPath)) {
|
|
||||||
fs.writeFileSync(rankingsPath, "{}");
|
|
||||||
}
|
|
||||||
const rankings = JSON.parse(fs.readFileSync(rankingsPath));
|
const rankings = JSON.parse(fs.readFileSync(rankingsPath));
|
||||||
await message.reply(
|
await message.reply(
|
||||||
`${backticks}${applicableName} - ${"*".repeat(
|
`${backticks}${applicableName} - ${"*".repeat(
|
||||||
@ -420,135 +421,6 @@ client.on("messageCreate", async (message) => {
|
|||||||
await message.reply(`error getting rank: ${error.message}`);
|
await message.reply(`error getting rank: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args[0] === "rankings") {
|
|
||||||
await message.reply("getting rankings...");
|
|
||||||
try {
|
|
||||||
console.log(`getting rankings at ${rankingsPath}...`);
|
|
||||||
if (!fs.existsSync(rankingsPath)) {
|
|
||||||
fs.writeFileSync(rankingsPath, "{}");
|
|
||||||
}
|
|
||||||
const rankings = JSON.parse(fs.readFileSync(rankingsPath));
|
|
||||||
let players = [];
|
|
||||||
for (const [playerId, rank] of Object.entries(rankings)) {
|
|
||||||
if (rank > 0) {
|
|
||||||
const player = await pickupGuild.members.fetch(playerId);
|
|
||||||
const applicableName = getApplicableName(player);
|
|
||||||
players.push({ name: applicableName, rank });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sort by rank, then name
|
|
||||||
players.sort((a, b) => {
|
|
||||||
if (a.rank === b.rank) {
|
|
||||||
return a.name.localeCompare(b.name);
|
|
||||||
}
|
|
||||||
return b.rank - a.rank;
|
|
||||||
});
|
|
||||||
|
|
||||||
// build string
|
|
||||||
let str = backticks;
|
|
||||||
const maxNameLength = Math.max(...players.map((p) => p.name.length));
|
|
||||||
for (const { name, rank } of players) {
|
|
||||||
str += `${name.padEnd(maxNameLength, " ")} - ${"*".repeat(rank)}\n`;
|
|
||||||
}
|
|
||||||
str += backticks;
|
|
||||||
if (str === backticks + backticks) str = "no rankings found";
|
|
||||||
await message.reply(str);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
await message.reply(`error getting rankings: ${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);
|
client.login(TOKEN);
|
||||||
|
|||||||
1
rankings.json
Normal file
1
rankings.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"233036215610245120":0}
|
||||||
Loading…
Reference in New Issue
Block a user