diff --git a/src/db/functions/levelFunctions.ts b/src/db/functions/levelFunctions.ts index 36f831e..1858d1d 100644 --- a/src/db/functions/levelFunctions.ts +++ b/src/db/functions/levelFunctions.ts @@ -86,10 +86,8 @@ export async function addXpToUser( const cacheKey = `level-${discordId}`; const userData = await getUserLevel(discordId); const currentLevel = userData.level; - const currentXp = userData.xp; - const xpToAdd = amount; - userData.xp = currentXp + xpToAdd; + userData.xp = Number(userData.xp ?? 0) + Number(amount); userData.lastMessageTimestamp = new Date(); userData.level = calculateLevelFromXp(userData.xp); diff --git a/src/util/levelingSystem.ts b/src/util/levelingSystem.ts index 80f4996..b311fe6 100644 --- a/src/util/levelingSystem.ts +++ b/src/util/levelingSystem.ts @@ -20,11 +20,19 @@ let minXpOffered = config.leveling.minXpAwarded ?? 5; let maxXpOffered = config.leveling.maxXpAwarded ?? 15; if (typeof minXpOffered === 'string') { - minXpOffered = parseInt(minXpOffered, 10); + minXpOffered = Number(minXpOffered); } +if (isNaN(minXpOffered) || minXpOffered < 0) { + throw new Error('Minimum XP awarded must be a non-negative number.'); +} + if (typeof maxXpOffered === 'string') { - maxXpOffered = parseInt(maxXpOffered, 10); + maxXpOffered = Number(maxXpOffered); } +if (isNaN(maxXpOffered) || maxXpOffered < 0) { + throw new Error('Maximum XP awarded must be a non-negative number.'); +} + if (minXpOffered > maxXpOffered) { throw new Error( 'Minimum XP awarded must be less than or equal to maximum XP awarded.', @@ -36,11 +44,12 @@ const MAX_XP = maxXpOffered; let xpCooldownValue = config.leveling.xpCooldown ?? 60; if (typeof xpCooldownValue === 'string') { - xpCooldownValue = parseInt(xpCooldownValue, 10); + xpCooldownValue = Number(xpCooldownValue); } -if (isNaN(xpCooldownValue)) { - throw new Error('XP cooldown must be a number.'); +if (isNaN(xpCooldownValue) || xpCooldownValue < 0) { + throw new Error('XP cooldown must be a non-negative number.'); } + const XP_COOLDOWN = xpCooldownValue * 1000; const __dirname = path.resolve();