generated from nhcarrigan/template
### Explanation _No response_ ### Issue _No response_ ### Attestations - [ ] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [ ] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [ ] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [ ] I have pinned the dependencies to a specific patch version. ### Style - [ ] I have run the linter and resolved any errors. - [ ] My pull request uses an appropriate title, matching the conventional commit standards. - [ ] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [ ] All new and existing tests pass locally with my changes. - [ ] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning _No response_ Reviewed-on: #3 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit was merged in pull request #3.
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { MessageFlags } from "discord.js";
|
||||
import { errorHandler } from "../utils/errorHandler.js";
|
||||
import type { Command } from "../interfaces/command.js";
|
||||
|
||||
/**
|
||||
* Handles the `/leaderboard` command.
|
||||
* @param pavelle - Pavelle's instance.
|
||||
* @param interaction - The interaction payload from Discord.
|
||||
*/
|
||||
// eslint-disable-next-line max-lines-per-function -- Lazy.
|
||||
export const leaderboard: Command = async(pavelle, interaction) => {
|
||||
try {
|
||||
const members = await pavelle.db.users.findMany({
|
||||
where: {
|
||||
serverId: interaction.guild.id,
|
||||
},
|
||||
});
|
||||
|
||||
const sorted = members.sort((a, b) => {
|
||||
return b.points - a.points;
|
||||
});
|
||||
|
||||
const topTen = sorted.slice(0, 10).map((member, index) => {
|
||||
return `- **#${index.toString()}:** <@${member.userId}> - ${member.points.toString()} point(s).`;
|
||||
}).
|
||||
join("\n");
|
||||
const yourScore = sorted.find((member) => {
|
||||
return member.userId === interaction.member.id;
|
||||
});
|
||||
const yourRank = yourScore
|
||||
? `You are rank #${sorted.indexOf(yourScore).toString()} with ${yourScore.points.toString()} points.`
|
||||
: "You are not ranked. Try throwing some stuff!";
|
||||
await interaction.editReply({
|
||||
components: [
|
||||
{
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Discord API convention.
|
||||
accent_color: null,
|
||||
|
||||
components: [
|
||||
{
|
||||
content: "# Leaderboard",
|
||||
type: 10,
|
||||
},
|
||||
{
|
||||
divider: true,
|
||||
spacing: 1,
|
||||
type: 14,
|
||||
},
|
||||
{
|
||||
content: "## Top 10 Members",
|
||||
type: 10,
|
||||
},
|
||||
{
|
||||
content: topTen,
|
||||
type: 10,
|
||||
},
|
||||
{
|
||||
content: `-# ${yourRank}`,
|
||||
type: 10,
|
||||
},
|
||||
],
|
||||
spoiler: false,
|
||||
type: 17,
|
||||
},
|
||||
],
|
||||
flags: [ MessageFlags.IsComponentsV2 ],
|
||||
});
|
||||
} catch (error) {
|
||||
const id = await errorHandler(error, "leaderboard command");
|
||||
await interaction.editReply({
|
||||
content:
|
||||
`An error occurred while processing your request. Please try again later, or join our support server and provide this ID: ${id}`,
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user