(client, guildId, userId, levels)
| 352 | } |
| 353 | |
| 354 | export async function removeLevels(client, guildId, userId, levels) { |
| 355 | try { |
| 356 | const levelingConfig = await getLevelingConfig(client, guildId); |
| 357 | if (!levelingConfig?.enabled) { |
| 358 | throw new TitanBotError( |
| 359 | 'Leveling system is disabled on this server', |
| 360 | ErrorTypes.CONFIGURATION, |
| 361 | 'The leveling system is currently disabled on this server.' |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | if (!Number.isInteger(levels) || levels <= 0) { |
| 366 | throw new TitanBotError( |
| 367 | `Invalid level amount: ${levels}`, |
| 368 | ErrorTypes.VALIDATION, |
| 369 | 'You must remove a positive number of levels.' |
| 370 | ); |
| 371 | } |
| 372 | |
| 373 | const userData = await getUserLevelData(client, guildId, userId); |
| 374 | const newLevel = Math.max(MIN_LEVEL, userData.level - levels); |
| 375 | |
| 376 | const newXp = 0; |
| 377 | const newTotalXp = calculateTotalXp(newLevel, newXp); |
| 378 | |
| 379 | userData.level = newLevel; |
| 380 | userData.xp = newXp; |
| 381 | userData.totalXp = newTotalXp; |
| 382 | |
| 383 | await saveUserLevelData(client, guildId, userId, userData); |
| 384 | |
| 385 | logger.info(`Removed ${levels} levels from user ${userId} in guild ${guildId}`); |
| 386 | return userData; |
| 387 | } catch (error) { |
| 388 | logger.error(`Error removing levels for user ${userId}:`, error); |
| 389 | if (error instanceof TitanBotError) throw error; |
| 390 | throw new TitanBotError( |
| 391 | `Failed to remove levels: ${error.message}`, |
| 392 | ErrorTypes.DATABASE, |
| 393 | 'Could not remove levels at this time.' |
| 394 | ); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | export async function setUserLevel(client, guildId, userId, level) { |
| 399 | try { |
no test coverage detected