From 612ed820a0600ec11ed642005377cd7f5a8a8b77 Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Mon, 3 Oct 2022 22:57:40 -0400 Subject: wip --- src/commands/leveling/level.ts | 8 ++++---- src/commands/leveling/setLevel.ts | 28 ++++++++++++++++++---------- src/commands/leveling/setXp.ts | 34 +++++++++++++++++++++++++--------- 3 files changed, 47 insertions(+), 23 deletions(-) (limited to 'src/commands/leveling') diff --git a/src/commands/leveling/level.ts b/src/commands/leveling/level.ts index 869140d..bf4ca9b 100644 --- a/src/commands/leveling/level.ts +++ b/src/commands/leveling/level.ts @@ -9,11 +9,11 @@ import { type SlashMessage } from '#lib'; import canvas from '@napi-rs/canvas'; -import { SimplifyNumber } from '@notenoughupdates/simplify-number'; +import { simplifyNumber } from '@tanzanite/simplify-number'; import assert from 'assert/strict'; import { ApplicationCommandOptionType, AttachmentBuilder, Guild, PermissionFlagsBits, User } from 'discord.js'; + assert(canvas); -assert(SimplifyNumber); export default class LevelCommand extends BotCommand { public constructor() { @@ -119,9 +119,9 @@ export default class LevelCommand extends BotCommand { // Draw level data text ctx.fillStyle = white; - const xpTxt = `${SimplifyNumber(currentLevelXpProgress)}/${SimplifyNumber(xpForNextLevel)}`; + const xpTxt = `${simplifyNumber(currentLevelXpProgress)}/${simplifyNumber(xpForNextLevel)}`; - const rankTxt = SimplifyNumber(rank.indexOf(rank.find((x) => x.user === user.id)!) + 1); + const rankTxt = simplifyNumber(rank.indexOf(rank.find((x) => x.user === user.id)!) + 1); ctx.fillText(`Level: ${userLevel} XP: ${xpTxt} Rank: ${rankTxt}`, AVATAR_SIZE + 70, AVATAR_SIZE - 20); // Return image in buffer form diff --git a/src/commands/leveling/setLevel.ts b/src/commands/leveling/setLevel.ts index 6f6f69e..3a995a2 100644 --- a/src/commands/leveling/setLevel.ts +++ b/src/commands/leveling/setLevel.ts @@ -1,4 +1,5 @@ -import { AllowedMentions, BotCommand, emojis, format, Level, type ArgType, type CommandMessage, type SlashMessage } from '#lib'; +import { AllowedMentions, BotCommand, emojis, Level, type ArgType, type CommandMessage, type SlashMessage } from '#lib'; +import { commas } from '#lib/common/tags.js'; import assert from 'assert/strict'; import { ApplicationCommandOptionType } from 'discord.js'; @@ -42,20 +43,27 @@ export default class SetLevelCommand extends BotCommand { assert(message.inGuild()); assert(user.id); - if (isNaN(level) || !Number.isInteger(level)) + if (isNaN(level) || !Number.isInteger(level)) { return await message.util.reply(`${emojis.error} Provide a valid number to set the user's level to.`); - if (level > 6553 || level < 0) - return await message.util.reply(`${emojis.error} You cannot set a level higher than **6,553**.`); + } + + if (level > Level.MAX_LEVEL || level < 0) { + return await message.util.reply(commas`${emojis.error} You cannot set a level higher than **${Level.MAX_LEVEL}**.`); + } const [levelEntry] = await Level.findOrBuild({ - where: { user: user.id, guild: message.guild.id }, - defaults: { user: user.id, guild: message.guild.id, xp: 0 } + where: { + user: user.id, + guild: message.guild.id + } }); - await levelEntry.update({ xp: Level.convertLevelToXp(level), user: user.id, guild: message.guild.id }); + + const xp = Level.convertLevelToXp(level); + + await levelEntry.update({ xp, user: user.id, guild: message.guild.id }); + return await message.util.send({ - content: `Successfully set level of <@${user.id}> to ${format.input(level.toLocaleString())} (${format.input( - levelEntry.xp.toLocaleString() - )} XP)`, + content: commas`Successfully set level of <@${user.id}> to **${level}** (**${xp}** xp)`, allowedMentions: AllowedMentions.none() }); } diff --git a/src/commands/leveling/setXp.ts b/src/commands/leveling/setXp.ts index 8c3b86f..270ad68 100644 --- a/src/commands/leveling/setXp.ts +++ b/src/commands/leveling/setXp.ts @@ -1,4 +1,6 @@ -import { AllowedMentions, BotCommand, emojis, format, Level, type ArgType, type CommandMessage, type SlashMessage } from '#lib'; +import { AllowedMentions, BotCommand, emojis, Level, type ArgType, type CommandMessage, type SlashMessage } from '#lib'; +import { commas } from '#lib/common/tags.js'; +import { input } from '#lib/utils/Format.js'; import assert from 'assert/strict'; import { ApplicationCommandOptionType } from 'discord.js'; @@ -44,22 +46,36 @@ export default class SetXpCommand extends BotCommand { assert(user.id); if (isNaN(xp)) return await message.util.reply(`${emojis.error} Provide a valid number.`); - if (xp > 2147483647 || xp < 0) + + if (xp > Level.MAX_XP || xp < 0) { return await message.util.reply( - `${emojis.error} Provide an positive integer under **2,147,483,647** to set the user's xp to.` + commas`${emojis.error} Provide an positive integer under **${Level.MAX_XP}** to set the user's xp to.` ); + } const [levelEntry] = await Level.findOrBuild({ - where: { user: user.id, guild: message.guild.id }, - defaults: { user: user.id, guild: message.guild.id } + where: { + user: user.id, + guild: message.guild.id + } }); - await levelEntry.update({ xp: xp, user: user.id, guild: message.guild.id }); + const res = await levelEntry + .update({ xp: xp, user: user.id, guild: message.guild.id }) + .catch((e) => (e instanceof Error ? e : null)); + + xp = levelEntry.xp; + const level = Level.convertXpToLevel(xp); + + if (res instanceof Error || res == null) { + return await message.util.reply({ + content: commas`Unable to set <@${user.id}>'s xp to **${xp}** with error ${input(res?.message ?? '¯\\_(ツ)_/¯')}.`, + allowedMentions: AllowedMentions.none() + }); + } return await message.util.send({ - content: `Successfully set <@${user.id}>'s xp to ${format.input(levelEntry.xp.toLocaleString())} (level ${format.input( - Level.convertXpToLevel(levelEntry.xp).toLocaleString() - )}).`, + content: commas`${emojis.success} Successfully set <@${user.id}>'s xp to **${xp}** (level **${level}**).`, allowedMentions: AllowedMentions.none() }); } -- cgit