MCPcopy Index your code
hub / github.com/codebymitch/TitanBot / addLevels

Function addLevels

src/services/leveling.js:302–352  ·  view source on GitHub ↗
(client, guildId, userId, levels)

Source from the content-addressed store, hash-verified

300}
301
302export 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
354export async function removeLevels(client, guildId, userId, levels) {
355 try {

Callers 1

executeFunction · 0.90

Calls 4

calculateTotalXpFunction · 0.85
getLevelingConfigFunction · 0.70
getUserLevelDataFunction · 0.70
saveUserLevelDataFunction · 0.70

Tested by

no test coverage detected