From 3e845f120fa1bb0f38e54969238a4d27abad7098 Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Fri, 3 Sep 2021 11:11:00 -0400 Subject: add setxp command --- src/arguments/abbreviatedNumber.ts | 6 ++ src/commands/leveling/setLevel.ts | 6 +- src/commands/leveling/setXp.ts | 78 ++++++++++++++++++++++++ src/lib/extensions/discord-akairo/BushClient.ts | 4 +- src/lib/extensions/discord-akairo/BushCommand.ts | 3 +- src/lib/models/Guild.ts | 2 +- 6 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 src/arguments/abbreviatedNumber.ts create mode 100644 src/commands/leveling/setXp.ts (limited to 'src') diff --git a/src/arguments/abbreviatedNumber.ts b/src/arguments/abbreviatedNumber.ts new file mode 100644 index 0000000..9da7063 --- /dev/null +++ b/src/arguments/abbreviatedNumber.ts @@ -0,0 +1,6 @@ +import { BushArgumentTypeCaster } from '@lib'; +import numeral = require('numeral'); + +export const abbreviatedNumberTypeCaster: BushArgumentTypeCaster = (_, phrase): number | null => { + return numeral(phrase?.toLowerCase()).value(); +}; diff --git a/src/commands/leveling/setLevel.ts b/src/commands/leveling/setLevel.ts index be3700a..5eb97b7 100644 --- a/src/commands/leveling/setLevel.ts +++ b/src/commands/leveling/setLevel.ts @@ -54,8 +54,6 @@ export default class SetLevelCommand extends BushCommand { message: BushMessage | BushSlashMessage, { user, level }: { user: User; level: number } ): Promise { - if (!message.author.isOwner()) - return await message.util.reply(`${util.emojis.error} Only my developers can run this command.`); if (!message.guild) return await message.util.reply(`${util.emojis.error} This command can only be run in a guild.`); if (!user.id) throw new Error('user.id is null'); @@ -71,7 +69,9 @@ export default class SetLevelCommand extends BushCommand { }); await levelEntry.update({ xp: Level.convertLevelToXp(level) }); return await message.util.send({ - content: `Successfully set level of <@${user.id}> to \`${level}\` (\`${levelEntry.xp}\` XP)`, + content: `Successfully set level of <@${ + user.id + }> to \`${level.toLocaleString()}\` (\`${levelEntry.xp.toLocaleString()}\` XP)`, allowedMentions: AllowedMentions.none() }); } diff --git a/src/commands/leveling/setXp.ts b/src/commands/leveling/setXp.ts new file mode 100644 index 0000000..4a07519 --- /dev/null +++ b/src/commands/leveling/setXp.ts @@ -0,0 +1,78 @@ +import { AllowedMentions, BushCommand, BushMessage, BushSlashMessage, Level } from '@lib'; +import { User } from 'discord.js'; + +export default class SetXpCommand extends BushCommand { + public constructor() { + super('setXp', { + aliases: ['setxp'], + category: 'leveling', + description: { + content: 'Sets the xp of a user', + usage: 'setlevel ', + examples: ['setlevel @Moulberry 69k'] //nice + }, + args: [ + { + id: 'user', + type: 'user', + prompt: { + start: 'What user would you like to change the xp of?', + retry: '{error} Choose a valid user to change the xp of.' + } + }, + { + id: 'xp', + type: 'abbreviatedNumber', + prompt: { + start: 'How much xp should the user have?', + retry: "{error} Choose a valid number to set the user's xp to." + } + } + ], + slashOptions: [ + { + name: 'user', + description: 'What user would you like to change the xp of?', + type: 'USER', + required: true + }, + { + name: 'xp', + description: 'How much xp should the user have?', + type: 'INTEGER', + required: true + } + ], + slash: true, + channel: 'guild', + clientPermissions: ['SEND_MESSAGES'], + userPermissions: ['SEND_MESSAGES', 'ADMINISTRATOR'] + }); + } + + public override async exec( + message: BushMessage | BushSlashMessage, + { user, xp }: { user: User; xp: number } + ): Promise { + if (!message.guild) return await message.util.reply(`${util.emojis.error} This command can only be run in a guild.`); + if (!user.id) throw new Error('user.id is null'); + + const [levelEntry] = await Level.findOrBuild({ + where: { + user: user.id, + guild: message.guild.id + }, + defaults: { + user: user.id, + guild: message.guild.id + } + }); + await levelEntry.update({ xp: xp }); + return await message.util.send({ + content: `Successfully set <@${user.id}>'s xp to \`${levelEntry.xp.toLocaleString()}\` (level \`${Level.convertXpToLevel( + levelEntry.xp + ).toLocaleString()}\`).`, + allowedMentions: AllowedMentions.none() + }); + } +} diff --git a/src/lib/extensions/discord-akairo/BushClient.ts b/src/lib/extensions/discord-akairo/BushClient.ts index 8a21d32..ab66f60 100644 --- a/src/lib/extensions/discord-akairo/BushClient.ts +++ b/src/lib/extensions/discord-akairo/BushClient.ts @@ -22,6 +22,7 @@ import path from 'path'; import { exit } from 'process'; import readline from 'readline'; import { Sequelize } from 'sequelize'; +import { abbreviatedNumberTypeCaster } from '../../../arguments/abbreviatedNumber'; import { contentWithDurationTypeCaster } from '../../../arguments/contentWithDuration'; import { discordEmojiTypeCaster } from '../../../arguments/discordEmoji'; import { durationTypeCaster } from '../../../arguments/duration'; @@ -266,7 +267,8 @@ export class BushClient extends AkairoClient