(client, guildId, userId, levels)
| 300 | } |
| 301 | |
| 302 | export async function addLevels(client, guildId, userId, levels) { |
| 303 | try { |
| 304 | const levelingConfig = await getLevelingConfig(client, guildId); |
| 305 | if (!levelingConfig?.enabled) { |
| 306 | throw new TitanBotError( |
| 307 | 'Leveling system is disabled on this server', |
| 308 | ErrorTypes.CONFIGURATION, |
| 309 | 'The leveling system is currently disabled on this server.' |
| 310 | ); |
| 311 | } |
| 312 | |
| 313 | if (!Number.isInteger(levels) || levels <= 0) { |
| 314 | throw new TitanBotError( |
| 315 | `Invalid level amount: ${levels}`, |
| 316 | ErrorTypes.VALIDATION, |
| 317 | 'You must add a positive number of levels.' |
| 318 | ); |
| 319 | } |
| 320 | |
| 321 | const userData = await getUserLevelData(client, guildId, userId); |
| 322 | const newLevel = userData.level + levels; |
| 323 | |
| 324 | if (newLevel > MAX_LEVEL) { |
| 325 | throw new TitanBotError( |
| 326 | `Level ${newLevel} exceeds maximum level ${MAX_LEVEL}`, |
| 327 | ErrorTypes.VALIDATION, |
| 328 | `Maximum level is ${MAX_LEVEL}.` |
| 329 | ); |
| 330 | } |
| 331 | |
| 332 | const newXp = 0; |
| 333 | const newTotalXp = calculateTotalXp(newLevel, newXp); |
| 334 | |
| 335 | userData.level = newLevel; |
| 336 | userData.xp = newXp; |
| 337 | userData.totalXp = newTotalXp; |
| 338 | |
| 339 | await saveUserLevelData(client, guildId, userId, userData); |
| 340 | |
| 341 | logger.info(`Added ${levels} levels to user ${userId} in guild ${guildId}`); |
| 342 | return userData; |
| 343 | } catch (error) { |
| 344 | logger.error(`Error adding levels for user ${userId}:`, error); |
| 345 | if (error instanceof TitanBotError) throw error; |
| 346 | throw new TitanBotError( |
| 347 | `Failed to add levels: ${error.message}`, |
| 348 | ErrorTypes.DATABASE, |
| 349 | 'Could not add levels at this time.' |
| 350 | ); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | export async function removeLevels(client, guildId, userId, levels) { |
| 355 | try { |
no test coverage detected