updated profile and centralized functions
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
const {
|
||||
EmbedBuilder
|
||||
} = require("discord.js");
|
||||
|
||||
const {
|
||||
getProfileCollections
|
||||
} = require("./profileSystem");
|
||||
|
||||
const {
|
||||
getCollections
|
||||
} = require("./cookieEconomy");
|
||||
|
||||
const RECORDS = {
|
||||
balance: {
|
||||
label:
|
||||
"🍪 Current Balance",
|
||||
source:
|
||||
"cookies",
|
||||
field:
|
||||
"cookies",
|
||||
format:
|
||||
value =>
|
||||
`${Number(value ?? 0).toLocaleString()} 🍪`
|
||||
},
|
||||
obtained: {
|
||||
label:
|
||||
"🍪 Lifetime Cookies Obtained",
|
||||
field:
|
||||
"economy.cookiesObtainedTotal",
|
||||
format:
|
||||
value =>
|
||||
`${Number(value ?? 0).toLocaleString()} 🍪`
|
||||
},
|
||||
given: {
|
||||
label:
|
||||
"🎁 Cookies Given",
|
||||
field:
|
||||
"economy.cookiesGivenTotal",
|
||||
format:
|
||||
value =>
|
||||
`${Number(value ?? 0).toLocaleString()} 🍪`
|
||||
},
|
||||
stolen: {
|
||||
label:
|
||||
"🥷 Cookies Stolen",
|
||||
field:
|
||||
"economy.stolenTotal",
|
||||
format:
|
||||
value =>
|
||||
`${Number(value ?? 0).toLocaleString()} 🍪`
|
||||
},
|
||||
vault: {
|
||||
label:
|
||||
"🏦 Bot Vault Stolen",
|
||||
field:
|
||||
"economy.vaultStolenTotal",
|
||||
format:
|
||||
value =>
|
||||
`${Number(value ?? 0).toLocaleString()} 🍪`
|
||||
},
|
||||
wins: {
|
||||
label:
|
||||
"🏆 Games Won",
|
||||
field:
|
||||
"games.total.won",
|
||||
format:
|
||||
value =>
|
||||
Number(value ?? 0).toLocaleString()
|
||||
},
|
||||
played: {
|
||||
label:
|
||||
"🎮 Games Played",
|
||||
field:
|
||||
"games.total.played",
|
||||
format:
|
||||
value =>
|
||||
Number(value ?? 0).toLocaleString()
|
||||
},
|
||||
streak: {
|
||||
label:
|
||||
"🔥 Best Win Streak",
|
||||
field:
|
||||
"streaks.bestWin",
|
||||
format:
|
||||
value =>
|
||||
Number(value ?? 0).toLocaleString()
|
||||
},
|
||||
lost: {
|
||||
label:
|
||||
"💸 Lifetime Cookies Lost",
|
||||
field:
|
||||
"economy.cookiesLostTotal",
|
||||
format:
|
||||
value =>
|
||||
`${Number(value ?? 0).toLocaleString()} 🍪`
|
||||
}
|
||||
};
|
||||
|
||||
function getNestedValue(
|
||||
document,
|
||||
path
|
||||
) {
|
||||
return path
|
||||
.split(".")
|
||||
.reduce(
|
||||
(value, key) =>
|
||||
value?.[key],
|
||||
document
|
||||
);
|
||||
}
|
||||
|
||||
async function buildRecordsEmbed({
|
||||
guildId,
|
||||
category,
|
||||
botUserId,
|
||||
limit = 10
|
||||
}) {
|
||||
const definition =
|
||||
RECORDS[category] ??
|
||||
RECORDS.balance;
|
||||
|
||||
let rows;
|
||||
|
||||
if (
|
||||
definition.source ===
|
||||
"cookies"
|
||||
) {
|
||||
const {
|
||||
cookies
|
||||
} = getCollections();
|
||||
|
||||
rows =
|
||||
await cookies
|
||||
.find({
|
||||
userId: {
|
||||
$ne: botUserId
|
||||
},
|
||||
cookies: {
|
||||
$gt: 0
|
||||
}
|
||||
})
|
||||
.sort({
|
||||
cookies: -1
|
||||
})
|
||||
.limit(limit)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
else {
|
||||
const {
|
||||
stats
|
||||
} = getProfileCollections();
|
||||
|
||||
rows =
|
||||
await stats
|
||||
.find({
|
||||
guildId,
|
||||
userId: {
|
||||
$ne: botUserId
|
||||
},
|
||||
[definition.field]: {
|
||||
$gt: 0
|
||||
}
|
||||
})
|
||||
.sort({
|
||||
[definition.field]: -1
|
||||
})
|
||||
.limit(limit)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
const medals = [
|
||||
"🥇",
|
||||
"🥈",
|
||||
"🥉"
|
||||
];
|
||||
|
||||
const description =
|
||||
rows.length > 0
|
||||
? rows.map(
|
||||
(row, index) => {
|
||||
const value =
|
||||
getNestedValue(
|
||||
row,
|
||||
definition.field
|
||||
);
|
||||
|
||||
const rank =
|
||||
medals[index] ??
|
||||
`**${index + 1}.**`;
|
||||
|
||||
return `${rank} <@${row.userId}> — **${definition.format(value)}**`;
|
||||
}
|
||||
).join("\n")
|
||||
: "No qualifying records yet.";
|
||||
|
||||
return new EmbedBuilder()
|
||||
.setTitle(
|
||||
`📚 Records — ${definition.label}`
|
||||
)
|
||||
.setDescription(
|
||||
description
|
||||
)
|
||||
.setFooter({
|
||||
text:
|
||||
"Records are derived from persisted profile statistics."
|
||||
})
|
||||
.setTimestamp();
|
||||
}
|
||||
|
||||
function getRecordChoices() {
|
||||
return Object.entries(RECORDS)
|
||||
.map(
|
||||
([value, definition]) => ({
|
||||
name:
|
||||
definition.label,
|
||||
value
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
buildRecordsEmbed,
|
||||
getRecordChoices
|
||||
};
|
||||
Reference in New Issue
Block a user