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,17 +1,18 @@
import { Events, EmbedBuilder, Guild, GuildChannel, Invite } from 'discord.js'; import { Events, EmbedBuilder, Guild, GuildChannel, Invite } from 'discord.js';
import { prisma } from '@lib/prisma'; import { prisma } from '@lib/prisma';
import { Bot as BotPrisma } from '@prisma/client'; import { Bot as BotPrisma } from '@prisma/client';
import { log } from '@lib/log';
async function getGuildInvite(guild: Guild): Promise<string> { async function getGuildInvite(guild: Guild): Promise<string> {
try { try {
if (guild.vanityURLCode) { if (guild.vanityURLCode) {
return `https://discord.gg/${guild.vanityURLCode}`; return `https://discord.gg/${guild.vanityURLCode}`;
} }
const channel : GuildChannel = guild.channels.cache const channel: GuildChannel = guild.channels.cache
.filter( .filter(
(ch): ch is GuildChannel => (ch): ch is GuildChannel =>
ch.isTextBased() && ch.isTextBased() &&
!!ch.permissionsFor(guild.members.me!).has('CreateInstantInvite'), !!ch.permissionsFor(guild.members.me!).has('CreateInstantInvite'),
) )
.first(); .first();
const invite: Invite = await channel.createInvite({ const invite: Invite = await channel.createInvite({
@ -22,7 +23,7 @@ async function getGuildInvite(guild: Guild): Promise<string> {
return invite.url; return invite.url;
} }
catch (err) { 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'; return 'No invite available';
} }
} }
@ -69,9 +70,7 @@ export default {
} }
} }
catch (err) { catch (err) {
console.error( log.error(err, 'Cannot get the database connection');
`\t⚠ | Cannot get the database connection!\n\t\t(${err as Error}).`,
);
} }
const botData: BotPrisma | null = await prisma.bot.findUnique({ const botData: BotPrisma | null = await prisma.bot.findUnique({
where: { where: {
@ -88,27 +87,27 @@ export default {
const buyerNotification: EmbedBuilder = new EmbedBuilder() const buyerNotification: EmbedBuilder = new EmbedBuilder()
.setTitle(`${guild.client.user.username} joined a new server`) .setTitle(`${guild.client.user.username} joined a new server`)
.setColor('#663399') .setColor('#663399')
.setDescription(` .setDescription(
`
Name: ${guild.name} Name: ${guild.name}
Owner id: ${guild.ownerId} Owner id: ${guild.ownerId}
Invite: ${guild.vanityURLCode || await getGuildInvite(guild)} Invite: ${guild.vanityURLCode || (await getGuildInvite(guild))}
Member: ${guild.memberCount} Member: ${guild.memberCount}
`) `,
)
.setTimestamp(); .setTimestamp();
await Promise.all( await Promise.all(
botData.buyers.map(async (buyer) => { botData.buyers.map(async (buyer) => {
try { try {
const user = await guild.client.users.fetch(buyer.id); const user = await guild.client.users.fetch(buyer.id);
const dm = await user.createDM(); const dm = await user.createDM();
await dm.send ({ await dm.send({
embeds: [ embeds: [buyerNotification],
buyerNotification,
],
}); });
await new Promise(res => setTimeout(res, 1000)); await new Promise((res) => setTimeout(res, 1000));
} }
catch (err) { catch (err) {
console.warn(`⚠️ | ${buyer.id} : ${err as Error}`); log.warn(err, `Unable to fetch ${buyer.id} user`);
return; 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 { prisma } from '@lib/prisma';
import { Bot as BotPrisma } from '@prisma/client'; import { Bot as BotPrisma } from '@prisma/client';
import { log } from '@lib/log';
export default { export default {
name: Events.GuildDelete, name: Events.GuildDelete,
@ -23,26 +24,26 @@ export default {
.setFooter({ .setFooter({
text: guildData.footer, text: guildData.footer,
}) })
.setDescription(` .setDescription(
`
Name: ${guild.name} Name: ${guild.name}
Owner id: ${guild.ownerId} Owner id: ${guild.ownerId}
Member: ${guild.memberCount} Member: ${guild.memberCount}
`) `,
)
.setTimestamp(); .setTimestamp();
await Promise.all( await Promise.all(
botData.buyers.map(async (buyer) => { botData.buyers.map(async (buyer: User) => {
try { try {
const user = await guild.client.users.fetch(buyer.id); const user = await guild.client.users.fetch(buyer.id);
const dm = await user.createDM(); const dm = await user.createDM();
await dm.send ({ await dm.send({
embeds: [ embeds: [buyerNotification],
buyerNotification,
],
}); });
await new Promise(res => setTimeout(res, 1000)); await new Promise((res) => setTimeout(res, 1000));
} }
catch (err) { catch (err) {
console.warn(`⚠️ | ${buyer.id} : ${err as Error}`); log.warn(err, `Not able to fetch user ${buyer.id}`);
return; return;
} }
}), }),

View file

@ -10,11 +10,14 @@ export default {
}, },
}); });
if (guildData.logServer) { 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 const logChannel = await newGuild.client.channels
.fetch(guildData.logServer) .fetch(guildData.logServer)
.catch(() => null); .catch(() => null);
if (!logChannel || !logChannel.isTextBased()) {return;} if (!logChannel || !logChannel.isTextBased()) {
return;
}
if (oldGuild.name !== newGuild.name) { if (oldGuild.name !== newGuild.name) {
toPrint += `- Name:\n\`${oldGuild.name}\` => \`${newGuild.name}\`\n`; toPrint += `- Name:\n\`${oldGuild.name}\` => \`${newGuild.name}\`\n`;
} }
@ -46,9 +49,7 @@ export default {
}) })
.setDescription(toPrint); .setDescription(toPrint);
logChannel.send({ logChannel.send({
embeds: [ embeds: [toRep],
toRep,
],
}); });
} }
}, },