fix: get correct guild, rebuild findPlayer()

This commit is contained in:
ethanf 2024-01-23 16:10:09 -06:00
parent 74e482ed90
commit 7c0bee42db
3 changed files with 62 additions and 16 deletions

View File

@ -1,7 +1,7 @@
{ {
"TOKEN": "redacted", "TOKEN": "redacted",
"CLIENT_ID": "1177748390092742767", "CLIENT_ID": "1177748390092742767",
"GUILD_ID": "658490352189046784", "GUILD_ID": "1175646363707523132",
"COMMAND_CHANNEL_ID": "1177469184192565288", "COMMAND_CHANNEL_ID": "1177469184192565288",
"ADDUP_ID": "1175649994674544721", "ADDUP_ID": "1175649994674544721",
"PICKING_ID": "1175649967935856680", "PICKING_ID": "1175649967935856680",

View File

@ -18,6 +18,7 @@ const client = new Client({
intents: [ intents: [
GatewayIntentBits.Guilds, GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.DirectMessages, GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent, GatewayIntentBits.MessageContent,
@ -28,20 +29,31 @@ const client = new Client({
const backticks = "```"; const backticks = "```";
const rankingsPath = path.join(__dirname, "rankings.json"); const rankingsPath = path.join(__dirname, "rankings.json");
const findPlayer = (guild, name) => { const matchString = (str, search) => {
// fuzzy search globalname 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) => let player = guild.members.cache.find((member) =>
member.user.globalName.toLowerCase().includes(name.toLowerCase()) matchString(member.displayName, searchName)
); );
if (!player) { if (!player) {
// fuzzy search username // search global name
player = guild.members.cache.find((member) => 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) { if (!player) {
// match id // match id
player = guild.members.cache.find((member) => member.id === name); player = guild.members.cache.find((member) => member.id === searchName);
} }
return player; return player;
}; };
@ -301,19 +313,37 @@ client.on("messageCreate", async (message) => {
const args = message.content.toLowerCase().split(" "); const args = message.content.toLowerCase().split(" ");
if (args[0] === "setrank") { if (args[0] === "setrank") {
if (args.length < 3) {
await message.reply(
"invalid number of arguments. usage: `setrank <player name or id> <rank (0-5)>`"
);
return;
}
const pickupGuild = client.guilds.cache.get(GUILD_ID); 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 interaction.reply( await message.reply(
`could not find player ${args[1]}. if this issue persists, try using the player's id` `could not find player ${args[1]}. if this issue persists, try copy/pasting the player's discord handle or user id`
); );
return; return;
} }
const playerId = player.id; const playerId = player.id;
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) {
await interaction.reply( await message.reply(
`invalid rank ${args[2]}. must be an integer between 0 and 5` `invalid rank ${args[2]}. must be an integer between 0 and 5`
); );
return; return;
@ -330,7 +360,7 @@ client.on("messageCreate", async (message) => {
rankings[playerId] = rank; rankings[playerId] = rank;
console.log( 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) => { new Promise((resolve, reject) => {
fs.writeFile(rankingsPath, JSON.stringify(rankings), (err) => { fs.writeFile(rankingsPath, JSON.stringify(rankings), (err) => {
@ -342,32 +372,47 @@ client.on("messageCreate", async (message) => {
console.error(err); console.error(err);
message.reply(`error setting rank: ${err.message}`); message.reply(`error setting rank: ${err.message}`);
} else { } else {
message.reply(`set rank of ${player.globalName} to ${rank}`); message.reply(`set rank of ${applicableName} to ${rank}`);
} }
}); });
} }
if (args[0] === "getrank") { if (args[0] === "getrank") {
if (args.length < 2) {
await message.reply(
"invalid number of arguments. usage: `getrank <player name or id>`"
);
return;
}
const pickupGuild = client.guilds.cache.get(GUILD_ID); 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]); const player = findPlayer(pickupGuild, args[1]);
if (!player) { if (!player) {
await interaction.reply( await message.reply(
`could not find player ${args[1]}. if this issue persists, try using the player's id` `could not find player ${args[1]}. if this issue persists, try using the player's id`
); );
return; return;
} }
const playerId = player.id; const playerId = player.id;
const applicableName =
player.displayName ||
player.user.globalName ||
player.user.username ||
playerId;
try { try {
console.log( console.log(
`getting rank of ${player.globalName}, ${playerId} at ${rankingsPath}...` `getting rank of ${applicableName}, ${playerId} at ${rankingsPath}...`
); );
const rankings = JSON.parse(fs.readFileSync(rankingsPath)); const rankings = JSON.parse(fs.readFileSync(rankingsPath));
await message.reply( await message.reply(
`${backticks}${player.globalName} - ${"*".repeat( `${backticks}${applicableName} - ${"*".repeat(
rankings[playerId] rankings[playerId]
)}${backticks}` )}${backticks}`
); );

1
rankings.json Normal file
View File

@ -0,0 +1 @@
{"233036215610245120":0}