messageDelete
Everything about the messageDelete Event.
The messageDelete event is called whenever a user deletes a message (in a server that the bot is in.) and this can be used to log those deleted messages.
Making a Logging System
The first thing we want to do is create a directory to hold our files. So inside the events directory, create a new folder, and name it messageDelete.
Inside this folder, you can create your file and name it whatever you like. For this tutorial, we will call it log-message.js.
The first thing we need to do is export a function for CommandKit to listen to, so:
module.exports = (message) => {
// Write Code Here
}Inside our exports function, we need to first check if the deleted message is from a bot. This is to stop spamming in the console and won't log if the message is from a bot.
module.exports = (message, client, handler) => {
if (message.author.bot) {
return;
} else {
// Code Here
}This will check every deleted message to see if it is from a bot or not. If the if statement returns true (aka, the author is a bot) it will not log the message.
We now need to define the data that we will use inside our command, so we need to do the following:
module.exports = (message, client, handler) => {
if (message.author.bot) {
return;
} else {
const deletedMessage = message.content;
const deletedAuthor = message.author;
const deletedChannel = message.channel;
}This will save the content of the deleted message, the author of the deleted message, and the channel in which the message was deleted in.
And our logging system is done. In the next section, we will add the function for logging our deleted data to the console.
Logging the Message
This is the simplest part of the entire logging system, and it only includes adding one more line to our code.
module.exports = (message, client, handler) => {
if (message.author.bot) {
return;
} else {
const deletedMessage = message.content;
const deletedAuthor = message.author;
const deletedChannel = message.channel;
console.log(`${deletedAuthor.username} deleted a message in ${deletedChannel.name}.
Content: ${deletedMessage}`)
}And our system is done. Add this code to your file, restart your bot, and it will now log messages that are deleted in the console.
Last updated