fix(events/messages): MessageCreate now create the user in the database if is doesn't exist

This commit is contained in:
Raphael 2025-10-26 22:31:01 +01:00 committed by Raphaël
parent 19babd28f1
commit cb08d8e062

View file

@ -20,7 +20,9 @@ export default {
async execute(message: Message) { async execute(message: Message) {
if (message.author.bot || !message.guildId || !canGainXp(message.author.id)) return; if (message.author.bot || !message.guildId || !canGainXp(message.author.id)) return;
const Author: UserPrisma | null = await prisma.user.findUnique({ const Author: UserPrisma | null = await prisma.user.findUnique({
where: { id: message.author.id }, where: {
id: message.author.id,
},
}); });
if (!Author) { if (!Author) {
await prisma.user.create({ await prisma.user.create({
@ -29,24 +31,29 @@ export default {
}, },
}); });
} }
let guildUser: GuildUserPrisma | null = await prisma.guildUser.findUnique({ const guildUser: GuildUserPrisma = await prisma.guildUser.upsert({
where: { where: {
userId_guildId: { userId_guildId: {
userId: message.author.id, userId: message.author.id,
guildId: message.guildId, guildId: message.guildId,
}, },
}, },
}); update: {},
if (!guildUser) { create: {
guildUser = await prisma.guildUser.create({ xp: 0,
data: { level: 0,
userId: message.author.id, user: {
guildId: message.guildId, connect: {
xp: 0, id: message.author.id,
level: 0, },
}, },
}); guild: {
} connect: {
id: message.guildId,
},
},
},
});
const gainXp: number = Math.abs(message.content.length - Math.round(Math.random() * 13)) % 7; const gainXp: number = Math.abs(message.content.length - Math.round(Math.random() * 13)) % 7;
const newXp: number = guildUser.xp + gainXp; const newXp: number = guildUser.xp + gainXp;
let newLevel: number = guildUser.level; let newLevel: number = guildUser.level;
@ -57,7 +64,6 @@ export default {
`🎉 | Félicitations <@${message.author.id}>, tu es maintenant niveau **${newLevel}** !`, `🎉 | Félicitations <@${message.author.id}>, tu es maintenant niveau **${newLevel}** !`,
); );
} }
console.log(`${message.author.username} | ${newLevel} -> ${newXp} [${requiredXp}]`);
await prisma.guildUser.update({ await prisma.guildUser.update({
where: { where: {
userId_guildId: { userId_guildId: {