refactor(events/client): now using the new log system

This commit is contained in:
Raphael 2025-10-26 17:49:46 +01:00 committed by Raphaël
parent 6b62e8e24e
commit a02dfb629e
3 changed files with 31 additions and 30 deletions

View file

@ -1,6 +1,7 @@
import { Events, EmbedBuilder, Guild, GuildChannel, Invite } from 'discord.js';
import { prisma } from '@lib/prisma';
import { Bot as BotPrisma } from '@prisma/client';
import { log } from '@lib/log';
async function getGuildInvite(guild: Guild): Promise<string> {
try {
@ -22,7 +23,7 @@ async function getGuildInvite(guild: Guild): Promise<string> {
return invite.url;
}
catch (err) {
console.warn(`⚠️ Unable to create an invitation for the ${guild.id} : ${err as Error}`);
log.warn(err, `Unable to create an invitation for the ${guild.id}`);
return 'No invite available';
}
}
@ -69,9 +70,7 @@ export default {
}
}
catch (err) {
console.error(
`\t⚠ | Cannot get the database connection!\n\t\t(${err as Error}).`,
);
log.error(err, 'Cannot get the database connection');
}
const botData: BotPrisma | null = await prisma.bot.findUnique({
where: {
@ -88,12 +87,14 @@ export default {
const buyerNotification: EmbedBuilder = new EmbedBuilder()
.setTitle(`${guild.client.user.username} joined a new server`)
.setColor('#663399')
.setDescription(`
.setDescription(
`
Name: ${guild.name}
Owner id: ${guild.ownerId}
Invite: ${guild.vanityURLCode || await getGuildInvite(guild)}
Invite: ${guild.vanityURLCode || (await getGuildInvite(guild))}
Member: ${guild.memberCount}
`)
`,
)
.setTimestamp();
await Promise.all(
botData.buyers.map(async (buyer) => {
@ -101,14 +102,12 @@ export default {
const user = await guild.client.users.fetch(buyer.id);
const dm = await user.createDM();
await dm.send({
embeds: [
buyerNotification,
],
embeds: [buyerNotification],
});
await new Promise(res => setTimeout(res, 1000));
await new Promise((res) => setTimeout(res, 1000));
}
catch (err) {
console.warn(`⚠️ | ${buyer.id} : ${err as Error}`);
log.warn(err, `Unable to fetch ${buyer.id} user`);
return;
}
}),

View file

@ -1,6 +1,7 @@
import { Events, EmbedBuilder, Guild } from 'discord.js';
import { Events, EmbedBuilder, Guild, User } from 'discord.js';
import { prisma } from '@lib/prisma';
import { Bot as BotPrisma } from '@prisma/client';
import { log } from '@lib/log';
export default {
name: Events.GuildDelete,
@ -23,26 +24,26 @@ export default {
.setFooter({
text: guildData.footer,
})
.setDescription(`
.setDescription(
`
Name: ${guild.name}
Owner id: ${guild.ownerId}
Member: ${guild.memberCount}
`)
`,
)
.setTimestamp();
await Promise.all(
botData.buyers.map(async (buyer) => {
botData.buyers.map(async (buyer: User) => {
try {
const user = await guild.client.users.fetch(buyer.id);
const dm = await user.createDM();
await dm.send({
embeds: [
buyerNotification,
],
embeds: [buyerNotification],
});
await new Promise(res => setTimeout(res, 1000));
await new Promise((res) => setTimeout(res, 1000));
}
catch (err) {
console.warn(`⚠️ | ${buyer.id} : ${err as Error}`);
log.warn(err, `Not able to fetch user ${buyer.id}`);
return;
}
}),

View file

@ -10,11 +10,14 @@ export default {
},
});
if (guildData.logServer) {
let toPrint: string = 'The update of the guild had changes theses thing\n';
let toPrint: string =
'The update of the guild had changes theses thing\n';
const logChannel = await newGuild.client.channels
.fetch(guildData.logServer)
.catch(() => null);
if (!logChannel || !logChannel.isTextBased()) {return;}
if (!logChannel || !logChannel.isTextBased()) {
return;
}
if (oldGuild.name !== newGuild.name) {
toPrint += `- Name:\n\`${oldGuild.name}\` => \`${newGuild.name}\`\n`;
}
@ -46,9 +49,7 @@ export default {
})
.setDescription(toPrint);
logChannel.send({
embeds: [
toRep,
],
embeds: [toRep],
});
}
},