Add rank to /rank, i'm dumb

This commit is contained in:
Christopher Cookman 2023-04-20 15:03:17 -06:00
parent 6390f718fa
commit d785cf72ad
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42

174
index.js
View file

@ -140,32 +140,53 @@ client.on("interactionCreate", async interaction => {
if (row) { if (row) {
var data = row; var data = row;
let lvl = data.lvl; let lvl = data.lvl;
interaction.reply({ let rank;
embeds: [{ // Calculate rank
title: `${user.tag}'s Rank`, await db.all(`SELECT * FROM levels ORDER BY totalXp DESC`, async (err, rows) => {
fields: [{ if (err) {
name: "Level", console.error(err);
value: data.lvl, }
inline: true if (rows) {
}, let rank = 0;
{ for (let i = 0; i < rows.length; i++) {
name: "XP", if (rows[i].id === user.id) {
value: `${data.xp}/${eval(config.discord.levels.lvlUpEquation)}`, rank = i + 1;
}, break;
{
name: "Total XP",
value: data.totalXp,
inline: true
},
{
name: "Messages Sent",
value: data.msgCount,
inline: true
} }
], }
color: 0x00ff00 interaction.reply({
}] embeds: [{
}) title: `${user.tag}'s Rank`,
fields: [{
name: "Rank",
value: `#${rank}`,
inline: true
},
{
name: "Level",
value: data.lvl,
inline: true
},
{
name: "XP",
value: `${data.xp}/${eval(config.discord.levels.lvlUpEquation)}`,
},
{
name: "Total XP",
value: data.totalXp,
inline: true
},
{
name: "Messages Sent",
value: data.msgCount,
inline: true
}
],
color: 0x00ff00
}]
})
}
});
} }
}); });
break; break;
@ -199,63 +220,62 @@ client.on("interactionCreate", async interaction => {
}); });
break; break;
case "givexp": case "givexp":
// Dont gotta check perms, done on discord // Dont gotta check perms, done on discord
// Dont gotta check arguments, done on discord // Dont gotta check arguments, done on discord
// Get user data // Get user data
await db.get(`SELECT * FROM levels WHERE id = '${interaction.options.getUser("user").id}'`, async (err, row) => { await db.get(`SELECT * FROM levels WHERE id = '${interaction.options.getUser("user").id}'`, async (err, row) => {
if (err) { if (err) {
console.error(err); console.error(err);
} }
if (!row) { if (!row) {
await db.run(`INSERT INTO levels (id, xp, lvl, totalXp, msgCount) VALUES ('${interaction.options.getUser("user").id}', ${interaction.options.getInteger("amount")}, 1, ${interaction.options.getInteger("amount")}, 0)`); await db.run(`INSERT INTO levels (id, xp, lvl, totalXp, msgCount) VALUES ('${interaction.options.getUser("user").id}', ${interaction.options.getInteger("amount")}, 1, ${interaction.options.getInteger("amount")}, 0)`);
// Run level up equation // Run level up equation
var lvl = 1; var lvl = 1;
let lvlUpXp = eval(config.discord.levels.lvlUpEquation); let lvlUpXp = eval(config.discord.levels.lvlUpEquation);
// Keep running level up equation until xp is less than the calculated level up xp // Keep running level up equation until xp is less than the calculated level up xp
while (interaction.options.getInteger("amount") >= lvlUpXp) { while (interaction.options.getInteger("amount") >= lvlUpXp) {
lvl++; lvl++;
interaction.options.getInteger("amount") -= lvlUpXp; interaction.options.getInteger("amount") -= lvlUpXp;
lvlUpXp = eval(config.discord.levels.lvlUpEquation); lvlUpXp = eval(config.discord.levels.lvlUpEquation);
interaction.channel.send(`${interaction.options.getUser("user")}, you have leveled up to level ${lvl}!`).then(msg => { interaction.channel.send(`${interaction.options.getUser("user")}, you have leveled up to level ${lvl}!`).then(msg => {
setTimeout(() => { setTimeout(() => {
msg.delete(); msg.delete();
}, 10000); }, 10000);
});
}
return;
}
if (row) {
var data = row;
let lvl = data.lvl;
data.xp += interaction.options.getInteger("amount");
data.totalXp += interaction.options.getInteger("amount");
// If user is in database, and xp is greater than or equal to the calculated level up XP, add 1 to lvl and add the remainder to xp
let lvlUpXp = eval(config.discord.levels.lvlUpEquation);
// Keep running level up equation until xp is less than the calculated level up xp
while (data.xp >= lvlUpXp) {
data.lvl++;
data.xp -= lvlUpXp;
lvlUpXp = eval(config.discord.levels.lvlUpEquation);
interaction.channel.send(`${interaction.options.getUser("user")}, you have leveled up to level ${data.lvl}!`).then(msg => {
setTimeout(() => {
msg.delete();
}, 10000);
});
}
// Update database
await db.run(`UPDATE levels SET xp = ${data.xp}, lvl = ${data.lvl}, totalXp = ${data.totalXp}, msgCount = ${data.msgCount} WHERE id = '${interaction.options.getUser("user").id}'`);
interaction.reply({
content: `Gave ${interaction.options.getInteger("amount")} XP to ${interaction.options.getUser("user").tag}!`,
ephemeral: true
}); });
} }
return;
} }
); if (row) {
var data = row;
let lvl = data.lvl;
data.xp += interaction.options.getInteger("amount");
data.totalXp += interaction.options.getInteger("amount");
// If user is in database, and xp is greater than or equal to the calculated level up XP, add 1 to lvl and add the remainder to xp
let lvlUpXp = eval(config.discord.levels.lvlUpEquation);
// Keep running level up equation until xp is less than the calculated level up xp
while (data.xp >= lvlUpXp) {
data.lvl++;
data.xp -= lvlUpXp;
lvlUpXp = eval(config.discord.levels.lvlUpEquation);
interaction.channel.send(`${interaction.options.getUser("user")}, you have leveled up to level ${data.lvl}!`).then(msg => {
setTimeout(() => {
msg.delete();
}, 10000);
});
}
// Update database
await db.run(`UPDATE levels SET xp = ${data.xp}, lvl = ${data.lvl}, totalXp = ${data.totalXp}, msgCount = ${data.msgCount} WHERE id = '${interaction.options.getUser("user").id}'`);
interaction.reply({
content: `Gave ${interaction.options.getInteger("amount")} XP to ${interaction.options.getUser("user").tag}!`,
ephemeral: true
});
}
});
break; break;
}; };
}); });