Bonjour, donc je fait un bot qui log ce qui se passe sur un serveur et l'envoie dans l'autre mais je reçois toujours beaucoup d'erreurs, et j'ai celle ci depuis pas mal de temps (je pense d'ailleurs que je n'ai pas mis beaucoups des intents que j'ai importés)
Ma version de discord.js: v13.0.0
const { Client, Intents } = require('discord.js');
const sourceServerId = 'le serveur que je log';
const destinationServerId = 'le serveur ou j'envoi les logs';
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_BANS,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_INTEGRATIONS,
Intents.FLAGS.GUILD_WEBHOOKS,
Intents.FLAGS.GUILD_INVITES,
Intents.FLAGS.GUILD_VOICE_STATES,
Intents.FLAGS.GUILD_PRESENCES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MESSAGE_TYPING,
Intents.FLAGS.DIRECT_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_TYPING,
],
})
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
})
client.on('raw', packet => {
if (![
'MESSAGE_CREATE', 'MESSAGE_DELETE', 'MESSAGE_UPDATE', 'CHANNEL_CREATE', 'CHANNEL_DELETE', 'GUILD_MEMBER_ADD',
'GUILD_MEMBER_REMOVE', 'GUILD_MEMBER_UPDATE', 'GUILD_BAN_ADD', 'GUILD_BAN_REMOVE', 'GUILD_ROLE_CREATE',
'GUILD_ROLE_DELETE', 'GUILD_ROLE_UPDATE', 'INVITE_CREATE', 'INVITE_DELETE', 'VOICE_STATE_UPDATE',
'CHANNEL_UPDATE', 'CHANNEL_PINS_UPDATE', 'MESSAGE_BULK_DELETE'
].includes(packet.t)) return;
})
const event = packet.t;
const data = packet.d;
const destinationServer = client.guilds.cache.get(destinationServerId);
const logChannel = destinationServer.channels.cache.find(channel => channel.name === 'event-logs');
if (logChannel) {
let message = '';
let title = '';
let color = '';
switch (event) {
case 'MESSAGE_CREATE':
if (data.guild_id !== sourceServerId) return;
message = data.content;
title = `Message sent by ${data.author.username}`;
color = 'GREEN';
const channel1 = destinationServer.channels.cache.get(data.channel_id);
logChannel.send(`\`${title}\` sent in ${channel.toString()}:\n${message}`, {color: color});
break;
case 'MESSAGE_DELETE':
if (data.guild_id !== sourceServerId) return;
message = `A message was deleted in <#${data.channel_id}>`;
title = 'Message deleted';
color = 'RED';
logChannel.send(`\`${title}\`:\n${message}`, {color: color});
break;
case 'MESSAGE_UPDATE':
if (data.guild_id !== sourceServerId) return;
message = `**Old message:** ${data.content}`;
title = `Message edited by ${data.author.username}`;
color = 'YELLOW';
const channel = destinationServer.channels.cache.get(data.channel_id);
logChannel.send(`\`${title}\` sent in ${channel.toString()}:\n${message}`, {color: color});
break;
case 'CHANNEL_CREATE':
message = `A channel named **${data.name}** was created`;
title = 'Channel created';
color = 'GREEN';
logChannel.send(`\`${title}\`:\n${message}`, {color: color});
break;
case 'CHANNEL_DELETE':
message = `A channel named **${oldChannel.name}** was renamed to **${newChannel.name}** in the server **${oldChannel.guild.name}**.`;
break;
}
}
client.on('raw', async event => {
const eventName = event.t;
if (eventName === 'MESSAGE_REACTION_ADD') {
} else if (eventName === 'CHANNEL_CREATE') {
message = `A new channel named **${event.d.name}** was created.`;
logEvent(message);
} else if (eventName === 'CHANNEL_UPDATE') {
message = `A channel named **${event.d.name}** was updated.`;
logEvent(message);
} else if (eventName === 'CHANNEL_DELETE') {
message = `A channel named **${event.d.name}** was deleted.`;
logEvent(message);
} else if (eventName === 'GUILD_MEMBER_ADD') {
message = `A new member named **${event.d.user.username}** joined the server.`;
logEvent(message);
} else if (eventName === 'GUILD_MEMBER_REMOVE') {
message = `A member named **${event.d.user.username}** left the server.`;
logEvent(message);
}})
client.login('MTA3NzE3NTMxNjIyNjc3NzA4OQ.GHcpsy.jvesw5xlJcvqSOoOIr-JdUxOCiR89Gu0iKo_go');
Ce que je veux
Je veux que mon bot fonctionne et log touts les intents que j'ai mis
Ce que j'obtiens
cette erreur:
E:\bot\bot.js:38
const event = packet.t;
^
ReferenceError: packet is not defined
at Object.<anonymous> ←[90m(E:\bot\←[39mbot.js:38:17←[90m)←[39m
←[90m at Module._compile (node:internal/modules/cjs/loader:1159:14)←[39m
←[90m at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)←[39m
←[90m at Module.load (node:internal/modules/cjs/loader:1037:32)←[39m
←[90m at Module._load (node:internal/modules/cjs/loader:878:12)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)←[39m
←[90m at node:internal/main/run_main_module:23:47←[39m
Node.js v18.12.0
Ton packet (et tout ce qui suit jusque le deuxième évent "raw" est en dehors de ton premier event "raw"
client.on('raw', packet => {
if (![
'MESSAGE_CREATE', 'MESSAGE_DELETE', 'MESSAGE_UPDATE', 'CHANNEL_CREATE', 'CHANNEL_DELETE', 'GUILD_MEMBER_ADD',
'GUILD_MEMBER_REMOVE', 'GUILD_MEMBER_UPDATE', 'GUILD_BAN_ADD', 'GUILD_BAN_REMOVE', 'GUILD_ROLE_CREATE',
'GUILD_ROLE_DELETE', 'GUILD_ROLE_UPDATE', 'INVITE_CREATE', 'INVITE_DELETE', 'VOICE_STATE_UPDATE',
'CHANNEL_UPDATE', 'CHANNEL_PINS_UPDATE', 'MESSAGE_BULK_DELETE'
].includes(packet.t)) return;
const event = packet.t;
...
});
Je ne connais pas du tout discord mais l'erreur veut dire que packet
n'est pas initialisé, ce qui arrive peut-être suivant le type d'évènement.
Essaie ça (les exemples que j'ai pu trouver utilisent async
) :
client.on('raw', async packet => {
if (packet == null || typeof packet.t === "undefined" || typeof packet.d === "undefined"){
return;
}
// suite du code où t'es sûr que packet a les propriétés `t` et `d` dont tu as besoin
})
En effet, c'était ça le problème ^^ et JoolsMcFly désolé je n'avais pas bien compris ta réponse !