fix: get correct guild, rebuild findPlayer()
This commit is contained in:
parent
74e482ed90
commit
7c0bee42db
@ -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",
|
||||
|
||||
75
index.js
75
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 <player name or id> <rank (0-5)>`"
|
||||
);
|
||||
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 <player name or id>`"
|
||||
);
|
||||
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}`
|
||||
);
|
||||
|
||||
1
rankings.json
Normal file
1
rankings.json
Normal file
@ -0,0 +1 @@
|
||||
{"233036215610245120":0}
|
||||
Loading…
Reference in New Issue
Block a user