From 4f99552725981b44257d9738e3ade9bbf226a868 Mon Sep 17 00:00:00 2001 From: ethanf Date: Tue, 23 Jan 2024 04:22:29 -0600 Subject: [PATCH] feat: accept dms to get and set rank in a json --- config.json | 3 +- index.js | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 2 deletions(-) diff --git a/config.json b/config.json index 6ebe2b7..793acf8 100644 --- a/config.json +++ b/config.json @@ -8,5 +8,6 @@ "BLU_ID": "1175649793943552010", "RED_ID": "1175649777648680971", "FK_ID": "1176396207183106078", - "CAPTAIN_ID": "1178475124563919018" + "CAPTAIN_ID": "1178475124563919018", + "RANKING_WHITELIST": "233036215610245120," } diff --git a/index.js b/index.js index 3201edf..d98a217 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,9 @@ -import { Client, GatewayIntentBits } from "discord.js"; +import fs from "fs"; +import path from "path"; +import { Client, GatewayIntentBits, Partials } from "discord.js"; const { TOKEN, + GUILD_ID, COMMAND_CHANNEL_ID, ADDUP_ID, PICKING_ID, @@ -8,6 +11,7 @@ const { RED_ID, FK_ID, CAPTAIN_ID, + RANKING_WHITELIST, } = require("./config.json"); const client = new Client({ @@ -15,9 +19,33 @@ const client = new Client({ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildVoiceStates, + GatewayIntentBits.DirectMessages, + GatewayIntentBits.MessageContent, ], + partials: [Partials.Channel, Partials.Message], }); +const backticks = "```"; +const rankingsPath = path.join(__dirname, "rankings.json"); + +const findPlayer = (guild, name) => { + // fuzzy search globalname + let player = guild.members.cache.find((member) => + member.user.globalName.toLowerCase().includes(name.toLowerCase()) + ); + if (!player) { + // fuzzy search username + player = guild.members.cache.find((member) => + member.user.username.toLowerCase().includes(name.toLowerCase()) + ); + } + if (!player) { + // match id + player = guild.members.cache.find((member) => member.id === name); + } + return player; +}; + client.on("ready", () => { console.log(`Logged in as ${client.user.tag}!`); console.log( @@ -239,6 +267,7 @@ client.on("interactionCreate", async (interaction) => { ) ); } + if (command === "clear" || command === "bclear") { await interaction.reply("clearing messages..."); let channel = client.channels.cache.get(COMMAND_CHANNEL_ID); @@ -256,4 +285,91 @@ client.on("interactionCreate", async (interaction) => { } }); +client.on("messageCreate", async (message) => { + if (message.author.bot || message.guild) return; + + // check if user is whitelisted + if (!RANKING_WHITELIST.includes(message.author.id)) { + return; + } + + if (message.content.toLowerCase().includes("hello penguin")) { + await message.reply(`hello ${message.author.username}`); + return; + } + + const args = message.content.toLowerCase.split(" "); + + if (args[0] === "setrank") { + const pickupGuild = client.guilds.cache.get(GUILD_ID); + const player = findPlayer(pickupGuild, args[1]); + if (!player) { + await interaction.reply( + `could not find player ${args[1]}. if this issue persists, try using the player's id` + ); + return; + } + + const playerId = player.id; + const rank = parseInt(args[2]); + + // create rankings.json if it doesn't exist + const rankingsPath = path.join(__dirname, "rankings.json"); + if (!fs.existsSync(rankingsPath)) { + fs.writeFileSync(rankingsPath, "{}"); + } + + // rankings.json is a map of player's id to rank + const rankings = JSON.parse(fs.readFileSync(rankingsPath)); + rankings[playerId] = rank; + + console.log( + `setting rank of ${player.globalName}, ${playerId} to ${rank} at ${rankingsPath}...` + ); + new Promise((resolve, reject) => { + fs.writeFile(rankingsPath, JSON.stringify(rankings), (err) => { + if (err) reject(err); + else resolve(); + }); + }).then((res, err) => { + if (err) { + console.error(err); + message.reply(`error setting rank: ${err.message}`); + } else { + message.reply(`set rank of ${player.globalName} to ${rank}`); + } + }); + } + + if (args[0] === "getrank") { + const pickupGuild = client.guilds.cache.get(GUILD_ID); + const args = message.content.split(" "); + + const player = findPlayer(pickupGuild, args[1]); + if (!player) { + await interaction.reply( + `could not find player ${args[1]}. if this issue persists, try using the player's id` + ); + return; + } + + const playerId = player.id; + + try { + console.log( + `getting rank of ${player.globalName}, ${playerId} at ${rankingsPath}...` + ); + const rankings = JSON.parse(fs.readFileSync(rankingsPath)); + await message.reply( + `${backticks}${player.globalName} - ${"*".repeat( + rankings[playerId] + )}${backticks}` + ); + } catch (error) { + console.error(error); + await message.reply(`error getting rank: ${error.message}`); + } + } +}); + client.login(TOKEN);