(client, guildId, userId)
| 181 | } |
| 182 | |
| 183 | export async function getUserLevelData(client, guildId, userId) { |
| 184 | try { |
| 185 | if (!guildId || !userId) { |
| 186 | throw new TitanBotError( |
| 187 | 'Guild ID and User ID are required', |
| 188 | ErrorTypes.VALIDATION |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | const key = `${guildId}:leveling:users:${userId}`; |
| 193 | const data = await client.db.get(key); |
| 194 | |
| 195 | if (!data) { |
| 196 | return { |
| 197 | xp: 0, |
| 198 | level: 0, |
| 199 | totalXp: 0, |
| 200 | lastMessage: 0, |
| 201 | rank: 0 |
| 202 | }; |
| 203 | } |
| 204 | |
| 205 | return { |
| 206 | xp: Math.max(0, data.xp || 0), |
| 207 | level: Math.max(0, Math.min(data.level || 0, MAX_LEVEL)), |
| 208 | totalXp: Math.max(0, data.totalXp || 0), |
| 209 | lastMessage: data.lastMessage || 0, |
| 210 | rank: data.rank || 0 |
| 211 | }; |
| 212 | } catch (error) { |
| 213 | logger.error(`Error getting user level data for ${userId}:`, error); |
| 214 | if (error instanceof TitanBotError) throw error; |
| 215 | throw new TitanBotError( |
| 216 | `Failed to fetch user data: ${error.message}`, |
| 217 | ErrorTypes.DATABASE, |
| 218 | 'Could not fetch level data at this time.' |
| 219 | ); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | export async function saveUserLevelData(client, guildId, userId, data) { |
| 224 | try { |
no test coverage detected