aboutsummaryrefslogtreecommitdiff
path: root/src/commands/leveling
diff options
context:
space:
mode:
authorIRONM00N <64110067+IRONM00N@users.noreply.github.com>2021-08-30 11:09:36 -0400
committerIRONM00N <64110067+IRONM00N@users.noreply.github.com>2021-08-30 11:09:36 -0400
commitfb73d1b421638ea695eeafeee2215a1e923c4d4a (patch)
tree57c631f590478014f9ba1739a0d0c19ec05df09d /src/commands/leveling
parent48661511dd485957506e7900c8e86916aa3ea3af (diff)
downloadtanzanite-fb73d1b421638ea695eeafeee2215a1e923c4d4a.tar.gz
tanzanite-fb73d1b421638ea695eeafeee2215a1e923c4d4a.tar.bz2
tanzanite-fb73d1b421638ea695eeafeee2215a1e923c4d4a.zip
leaderboard command and some fixes
Diffstat (limited to 'src/commands/leveling')
-rw-r--r--src/commands/leveling/leaderboard.ts52
-rw-r--r--src/commands/leveling/level.ts42
2 files changed, 79 insertions, 15 deletions
diff --git a/src/commands/leveling/leaderboard.ts b/src/commands/leveling/leaderboard.ts
index e69de29..b8838b7 100644
--- a/src/commands/leveling/leaderboard.ts
+++ b/src/commands/leveling/leaderboard.ts
@@ -0,0 +1,52 @@
+import { BushCommand, BushMessage, BushSlashMessage, Level } from '@lib';
+import { MessageEmbed } from 'discord.js';
+
+export default class LeaderboardCommand extends BushCommand {
+ public constructor() {
+ super('leaderboard', {
+ aliases: ['leaderboard', 'lb'],
+ category: 'leveling',
+ description: {
+ content: 'Allows you to see the users with the highest levels in the server.',
+ usage: 'leaderboard [page]',
+ examples: ['leaderboard 5']
+ },
+ args: [
+ {
+ id: 'page',
+ type: 'integer',
+ prompt: {
+ start: 'What would you like to set your first argument to be?',
+ retry: '{error} Pick a valid argument.',
+ optional: true
+ }
+ }
+ ],
+ slash: true,
+ slashOptions: [
+ {
+ name: 'page',
+ description: 'What would you like to set your first argument to be?',
+ type: 'INTEGER',
+ required: false
+ }
+ ],
+ channel: 'guild',
+ clientPermissions: ['SEND_MESSAGES'],
+ userPermissions: ['SEND_MESSAGES']
+ });
+ }
+
+ public override async exec(message: BushMessage | BushSlashMessage, args: { page: number }): Promise<unknown> {
+ if (!message.guild) return await message.util.reply(`${util.emojis.error} This command can only be run in a server.`);
+ const ranks = (await Level.findAll({ where: { guild: message.guild.id } })).sort((a, b) => b.xp - a.xp);
+ const mapedRanks = ranks.map(
+ (val, index) => `\`${index + 1}\` <@${val.user}> - Level ${val.level} (${val.xp.toLocaleString()} xp)`
+ );
+ const chunked = util.chunk(mapedRanks, 25);
+ const embeds = chunked.map((c) =>
+ new MessageEmbed().setTitle(`${message.guild!.name}'s Leaderboard`).setDescription(c.join('\n'))
+ );
+ return await util.buttonPaginate(message, embeds, null, true, args?.page ?? undefined);
+ }
+}
diff --git a/src/commands/leveling/level.ts b/src/commands/leveling/level.ts
index 35a0a3e..6640744 100644
--- a/src/commands/leveling/level.ts
+++ b/src/commands/leveling/level.ts
@@ -1,4 +1,13 @@
-import { BushCommand, BushGuild, BushMessage, BushSlashMessage, BushUser, CanvasProgressBar, Level } from '@lib';
+import {
+ AllowedMentions,
+ BushCommand,
+ BushGuild,
+ BushMessage,
+ BushSlashMessage,
+ BushUser,
+ CanvasProgressBar,
+ Level
+} from '@lib';
import canvas from 'canvas';
import { MessageAttachment } from 'discord.js';
import got from 'got/dist/source';
@@ -41,17 +50,10 @@ export default class LevelCommand extends BushCommand {
private async getImage(user: BushUser, guild: BushGuild): Promise<Buffer> {
// I added comments because this code is impossible to read
- const [userLevelRow] = await Level.findOrBuild({
- where: {
- user: user.id,
- guild: guild.id
- },
- defaults: {
- user: user.id,
- guild: guild.id
- }
- });
- const rank = (await Level.findAll({ where: { guild: guild.id } })).sort((a, b) => b.xp - a.xp);
+ const guildRows = await Level.findAll({ where: { guild: guild.id } });
+ const rank = guildRows.sort((a, b) => b.xp - a.xp);
+ const userLevelRow = guildRows.find((a) => a.user === user.id);
+ if (!userLevelRow) throw new Error('User does not have a level');
const userLevel = userLevelRow.level;
const currentLevelXP = Level.convertLevelToXp(userLevel);
const currentLevelXPProgress = userLevelRow.xp - currentLevelXP;
@@ -124,8 +126,18 @@ export default class LevelCommand extends BushCommand {
}
public override async exec(message: BushMessage | BushSlashMessage, args: { user?: BushUser }): Promise<unknown> {
- return await message.reply({
- files: [new MessageAttachment(await this.getImage(args.user ?? message.author, message.guild!), 'level.png')]
- });
+ const user = args.user ?? message.author;
+ try {
+ return await message.util.reply({
+ files: [new MessageAttachment(await this.getImage(user, message.guild!), 'level.png')]
+ });
+ } catch (e) {
+ if (e instanceof Error && e.message === 'User does not have a level') {
+ return await message.util.reply({
+ content: `${util.emojis.error} ${user} does not have a level.`,
+ allowedMentions: AllowedMentions.none()
+ });
+ } else throw e;
+ }
}
}