aboutsummaryrefslogtreecommitdiff
path: root/src/commands/leveling/level.ts
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/level.ts
parent48661511dd485957506e7900c8e86916aa3ea3af (diff)
downloadtanzanite-fb73d1b421638ea695eeafeee2215a1e923c4d4a.tar.gz
tanzanite-fb73d1b421638ea695eeafeee2215a1e923c4d4a.tar.bz2
tanzanite-fb73d1b421638ea695eeafeee2215a1e923c4d4a.zip
leaderboard command and some fixes
Diffstat (limited to 'src/commands/leveling/level.ts')
-rw-r--r--src/commands/leveling/level.ts42
1 files changed, 27 insertions, 15 deletions
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;
+ }
}
}