nathanael1109
nathanael1109
DIAdiscord.js - Imagine an app
Created by nathanael1109 on 7/4/2023 in #djs-questions
messageUpdate Event
Hello, I'm trying to do a logging system for my bot and I ran into a problem when it cames to message updates. oldMessage returns the right thing but newMessage returns undefined. Here is my code :
const { Client, EmbedBuilder, Message } = require('discord.js');
const logsConfigSchema = require('../../models/logsConfig');

/**
*
* @param {Client} client
* @param {Message} message
* @param {Message} oldMessage
* @param {Message} newMessage
*
*/

module.exports = async (message, oldMessage, newMessage) => {
console.log('\n\n\nMessage modifié');
console.log(`\n\n\nAncien message: ${oldMessage}`);
console.log(`\n\n\nNouveau message: ${newMessage}`);
if (!oldMessage.author) return;
if (oldMessage.author.bot) return;
const logsConfig = await logsConfigSchema.findOne({ Guild: oldMessage.guild.id });
if (!logsConfig) return;
if (logsConfig.MessageLogsChannel === 'none') return;
const logChannel = oldMessage.guild.channels.cache.get(logsConfig.MessageLogsChannel);
if (!logChannel) return;

const embed = new EmbedBuilder()
.setTitle(':wastebasket: Message modifié')
.setDescription(`Un message a été modifié sur le serveur.`)
.addFields(
{ name: '👨 Auteur', value: `${oldMessage.author}`, inline: false },
{ name: '🆔 Id du message', value: oldMessage.id, inline: false },
{ name: '📄 Ancien contenu', value: `>>> ${oldMessage}`, inline: false },
{ name: '📄 Nouveau contenu', value: `>>> ${newMessage}`, inline: false },
)
.setColor('#00ffff');

await logChannel.send({ embeds: [embed] });
}
const { Client, EmbedBuilder, Message } = require('discord.js');
const logsConfigSchema = require('../../models/logsConfig');

/**
*
* @param {Client} client
* @param {Message} message
* @param {Message} oldMessage
* @param {Message} newMessage
*
*/

module.exports = async (message, oldMessage, newMessage) => {
console.log('\n\n\nMessage modifié');
console.log(`\n\n\nAncien message: ${oldMessage}`);
console.log(`\n\n\nNouveau message: ${newMessage}`);
if (!oldMessage.author) return;
if (oldMessage.author.bot) return;
const logsConfig = await logsConfigSchema.findOne({ Guild: oldMessage.guild.id });
if (!logsConfig) return;
if (logsConfig.MessageLogsChannel === 'none') return;
const logChannel = oldMessage.guild.channels.cache.get(logsConfig.MessageLogsChannel);
if (!logChannel) return;

const embed = new EmbedBuilder()
.setTitle(':wastebasket: Message modifié')
.setDescription(`Un message a été modifié sur le serveur.`)
.addFields(
{ name: '👨 Auteur', value: `${oldMessage.author}`, inline: false },
{ name: '🆔 Id du message', value: oldMessage.id, inline: false },
{ name: '📄 Ancien contenu', value: `>>> ${oldMessage}`, inline: false },
{ name: '📄 Nouveau contenu', value: `>>> ${newMessage}`, inline: false },
)
.setColor('#00ffff');

await logChannel.send({ embeds: [embed] });
}
And here is my eventHandler :
const path = require('path');
const getAllFiles = require('../utils/getAllFiles');

module.exports = (client) => {
const eventFolders = getAllFiles(path.join(__dirname, '..', 'events'), true);

for (const eventFolder of eventFolders) {
let eventFiles = getAllFiles(eventFolder);
eventFiles.sort();

const eventName = eventFolder.replace(/\\/g, '/').split('/').pop();

client.on(eventName, async (arg) => {
for (const eventFile of eventFiles) {
const eventFunction = require(eventFile);
await eventFunction(client, arg);
}
});
}
};
const path = require('path');
const getAllFiles = require('../utils/getAllFiles');

module.exports = (client) => {
const eventFolders = getAllFiles(path.join(__dirname, '..', 'events'), true);

for (const eventFolder of eventFolders) {
let eventFiles = getAllFiles(eventFolder);
eventFiles.sort();

const eventName = eventFolder.replace(/\\/g, '/').split('/').pop();

client.on(eventName, async (arg) => {
for (const eventFile of eventFiles) {
const eventFunction = require(eventFile);
await eventFunction(client, arg);
}
});
}
};
5 replies