aboutsummaryrefslogtreecommitdiff
path: root/src/commands/moulberry-bush/level.ts
blob: ab08361de0f19bd543dd75f84060cf4153cf8395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { ApplicationCommandOptionType } from 'discord-api-types';
import { Message } from 'discord.js';
import { CommandInteractionOption } from 'discord.js';
import { CommandInteraction } from 'discord.js';
import { User } from 'discord.js';
import { BotCommand } from '../../lib/extensions/BotCommand';
import { Level } from '../../lib/models';

export default class LevelCommand extends BotCommand {
	constructor() {
		super('level', {
			aliases: ['level', 'rank'],
			description: {
				content: 'Shows the level of a user',
				usage: 'level [user]',
				examples: ['level', 'level @Tyman']
			},
			args: [
				{
					id: 'user',
					type: 'user',
					prompt: {
						start: 'What user would you like to see the level of?',
						retry:
							'Invalid user. What user would you like to see the level of?',
						optional: true
					}
				}
			],
			slashCommandOptions: [
				{
					type: ApplicationCommandOptionType.USER,
					name: 'user',
					description: 'The user to get the level of',
					required: false
				}
			]
		});
	}

	private async getResponse(user: User): Promise<string> {
		const userLevelRow = await Level.findByPk(user.id);
		if (userLevelRow) {
			return `${user ? `${user.tag}'s` : 'Your'} level is ${
				userLevelRow.level
			} (${userLevelRow.xp} XP)`;
		} else {
			return `${user ? `${user.tag} does` : 'You do'} not have a level yet!`;
		}
	}

	async exec(message: Message, { user }: { user?: User }): Promise<void> {
		await message.reply(await this.getResponse(user || message.author));
	}
	async execSlash(
		message: CommandInteraction,
		{ user }: { user?: CommandInteractionOption }
	): Promise<void> {
		await message.reply(await this.getResponse(user?.user || message.user));
	}
}