From fb73d1b421638ea695eeafeee2215a1e923c4d4a Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Mon, 30 Aug 2021 11:09:36 -0400 Subject: leaderboard command and some fixes --- src/commands/leveling/level.ts | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'src/commands/leveling/level.ts') 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 { // 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 { - 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; + } } } -- cgit