From 7c0bee42dbcafa5c183e4b42f7b851859beba8f3 Mon Sep 17 00:00:00 2001 From: ethanf Date: Tue, 23 Jan 2024 16:10:09 -0600 Subject: [PATCH] fix: get correct guild, rebuild findPlayer() --- config.json | 2 +- index.js | 75 ++++++++++++++++++++++++++++++++++++++++----------- rankings.json | 1 + 3 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 rankings.json diff --git a/config.json b/config.json index 793acf8..cbcaa81 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,7 @@ { "TOKEN": "redacted", "CLIENT_ID": "1177748390092742767", - "GUILD_ID": "658490352189046784", + "GUILD_ID": "1175646363707523132", "COMMAND_CHANNEL_ID": "1177469184192565288", "ADDUP_ID": "1175649994674544721", "PICKING_ID": "1175649967935856680", diff --git a/index.js b/index.js index c71f128..d102eac 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,7 @@ const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, + GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.DirectMessages, GatewayIntentBits.MessageContent, @@ -28,20 +29,31 @@ const client = new Client({ const backticks = "```"; const rankingsPath = path.join(__dirname, "rankings.json"); -const findPlayer = (guild, name) => { - // fuzzy search globalname +const matchString = (str, search) => { + if (!str || !search) return false; + return str.toLowerCase().includes(search.toLowerCase()); +}; + +const findPlayer = (guild, searchName) => { + // search display name let player = guild.members.cache.find((member) => - member.user.globalName.toLowerCase().includes(name.toLowerCase()) + matchString(member.displayName, searchName) ); if (!player) { - // fuzzy search username + // search global name player = guild.members.cache.find((member) => - member.user.username.toLowerCase().includes(name.toLowerCase()) + matchString(member.user.globalName, searchName) + ); + } + if (!player) { + // search username + player = guild.members.cache.find((member) => + matchString(member.user.username, searchName) ); } if (!player) { // match id - player = guild.members.cache.find((member) => member.id === name); + player = guild.members.cache.find((member) => member.id === searchName); } return player; }; @@ -301,19 +313,37 @@ client.on("messageCreate", async (message) => { const args = message.content.toLowerCase().split(" "); if (args[0] === "setrank") { + if (args.length < 3) { + await message.reply( + "invalid number of arguments. usage: `setrank `" + ); + 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 interaction.reply( - `could not find player ${args[1]}. if this issue persists, try using the player's id` + await message.reply( + `could not find player ${args[1]}. if this issue persists, try copy/pasting the player's discord handle or user id` ); return; } const playerId = player.id; + const applicableName = + player.displayName || + player.user.globalName || + player.user.username || + playerId; + const rank = parseInt(args[2]); if (isNaN(rank) || rank < 0 || rank > 5) { - await interaction.reply( + await message.reply( `invalid rank ${args[2]}. must be an integer between 0 and 5` ); return; @@ -330,7 +360,7 @@ client.on("messageCreate", async (message) => { rankings[playerId] = rank; console.log( - `setting rank of ${player.globalName}, ${playerId} to ${rank} at ${rankingsPath}...` + `setting rank of ${applicableName}, ${playerId} to ${rank} at ${rankingsPath}...` ); new Promise((resolve, reject) => { fs.writeFile(rankingsPath, JSON.stringify(rankings), (err) => { @@ -342,32 +372,47 @@ client.on("messageCreate", async (message) => { console.error(err); message.reply(`error setting rank: ${err.message}`); } else { - message.reply(`set rank of ${player.globalName} to ${rank}`); + message.reply(`set rank of ${applicableName} to ${rank}`); } }); } if (args[0] === "getrank") { + if (args.length < 2) { + await message.reply( + "invalid number of arguments. usage: `getrank `" + ); + return; + } + const pickupGuild = client.guilds.cache.get(GUILD_ID); - const args = message.content.split(" "); + if (!pickupGuild) { + await message.reply("could not find guild"); + return; + } const player = findPlayer(pickupGuild, args[1]); if (!player) { - await interaction.reply( + await message.reply( `could not find player ${args[1]}. if this issue persists, try using the player's id` ); return; } const playerId = player.id; + const applicableName = + player.displayName || + player.user.globalName || + player.user.username || + playerId; try { console.log( - `getting rank of ${player.globalName}, ${playerId} at ${rankingsPath}...` + `getting rank of ${applicableName}, ${playerId} at ${rankingsPath}...` ); const rankings = JSON.parse(fs.readFileSync(rankingsPath)); await message.reply( - `${backticks}${player.globalName} - ${"*".repeat( + `${backticks}${applicableName} - ${"*".repeat( rankings[playerId] )}${backticks}` ); diff --git a/rankings.json b/rankings.json new file mode 100644 index 0000000..24c11e6 --- /dev/null +++ b/rankings.json @@ -0,0 +1 @@ +{"233036215610245120":0} \ No newline at end of file