From 3ffd0d2b808e583a06e4ecb7e0868f694f1699e9 Mon Sep 17 00:00:00 2001 From: ahmadk953 <103906421+ahmadk953@users.noreply.github.com> Date: Sun, 15 Jun 2025 21:24:51 -0400 Subject: [PATCH 1/4] fix(bot): default values for when values are undefined --- src/util/levelingSystem.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util/levelingSystem.ts b/src/util/levelingSystem.ts index e6bf4b9..ca57b5d 100644 --- a/src/util/levelingSystem.ts +++ b/src/util/levelingSystem.ts @@ -16,9 +16,9 @@ import { processMessageAchievements } from './achievementManager.js'; const config = loadConfig(); -const XP_COOLDOWN = config.leveling.xpCooldown * 1000; -const MIN_XP = config.leveling.minXpAwarded; -const MAX_XP = config.leveling.maxXpAwarded; +const XP_COOLDOWN = config.leveling.xpCooldown || 60 * 1000; +const MIN_XP = config.leveling.minXpAwarded || 5; +const MAX_XP = config.leveling.maxXpAwarded || 15; const __dirname = path.resolve(); From 01c1bbc21d0ae6182c485109e878691e3bbbbb9a Mon Sep 17 00:00:00 2001 From: ahmadk953 <103906421+ahmadk953@users.noreply.github.com> Date: Sun, 15 Jun 2025 22:02:57 -0400 Subject: [PATCH 2/4] fix(bot): added better type checking --- src/db/functions/levelFunctions.ts | 4 ++-- src/util/levelingSystem.ts | 29 ++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/db/functions/levelFunctions.ts b/src/db/functions/levelFunctions.ts index 3bfd42a..36f831e 100644 --- a/src/db/functions/levelFunctions.ts +++ b/src/db/functions/levelFunctions.ts @@ -86,8 +86,8 @@ export async function addXpToUser( const cacheKey = `level-${discordId}`; const userData = await getUserLevel(discordId); const currentLevel = userData.level; - const currentXp = Number(userData.xp); - const xpToAdd = Number(amount); + const currentXp = userData.xp; + const xpToAdd = amount; userData.xp = currentXp + xpToAdd; diff --git a/src/util/levelingSystem.ts b/src/util/levelingSystem.ts index ca57b5d..80f4996 100644 --- a/src/util/levelingSystem.ts +++ b/src/util/levelingSystem.ts @@ -16,9 +16,32 @@ import { processMessageAchievements } from './achievementManager.js'; const config = loadConfig(); -const XP_COOLDOWN = config.leveling.xpCooldown || 60 * 1000; -const MIN_XP = config.leveling.minXpAwarded || 5; -const MAX_XP = config.leveling.maxXpAwarded || 15; +let minXpOffered = config.leveling.minXpAwarded ?? 5; +let maxXpOffered = config.leveling.maxXpAwarded ?? 15; + +if (typeof minXpOffered === 'string') { + minXpOffered = parseInt(minXpOffered, 10); +} +if (typeof maxXpOffered === 'string') { + maxXpOffered = parseInt(maxXpOffered, 10); +} +if (minXpOffered > maxXpOffered) { + throw new Error( + 'Minimum XP awarded must be less than or equal to maximum XP awarded.', + ); +} + +const MIN_XP = minXpOffered; +const MAX_XP = maxXpOffered; + +let xpCooldownValue = config.leveling.xpCooldown ?? 60; +if (typeof xpCooldownValue === 'string') { + xpCooldownValue = parseInt(xpCooldownValue, 10); +} +if (isNaN(xpCooldownValue)) { + throw new Error('XP cooldown must be a number.'); +} +const XP_COOLDOWN = xpCooldownValue * 1000; const __dirname = path.resolve(); From ea7b70b3f0b60eb201c911230436b7bbcd02687b Mon Sep 17 00:00:00 2001 From: ahmadk953 <103906421+ahmadk953@users.noreply.github.com> Date: Sun, 15 Jun 2025 22:15:44 -0400 Subject: [PATCH 3/4] fix(bot): imporved xp type checking --- src/db/functions/levelFunctions.ts | 4 +--- src/util/levelingSystem.ts | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) 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(); From 6028396e74d39a86518476c9f9ed22229f52be5b Mon Sep 17 00:00:00 2001 From: Ahmad <103906421+ahmadk953@users.noreply.github.com> Date: Sun, 15 Jun 2025 22:20:01 -0400 Subject: [PATCH 4/4] chore(bot): update src/util/levelingSystem.ts Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> Signed-off-by: Ahmad <103906421+ahmadk953@users.noreply.github.com> --- src/util/levelingSystem.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/levelingSystem.ts b/src/util/levelingSystem.ts index b311fe6..97744dd 100644 --- a/src/util/levelingSystem.ts +++ b/src/util/levelingSystem.ts @@ -46,7 +46,7 @@ let xpCooldownValue = config.leveling.xpCooldown ?? 60; if (typeof xpCooldownValue === 'string') { xpCooldownValue = Number(xpCooldownValue); } -if (isNaN(xpCooldownValue) || xpCooldownValue < 0) { +if (!Number.isFinite(xpCooldownValue) || xpCooldownValue < 0) { throw new Error('XP cooldown must be a non-negative number.'); }