How to check if a message exists in discord.js?

by edmond_brakus , in category: Javascript , 16 days ago

How to check if a message exists in discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by denis , 15 days ago

@edmond_brakus 

To check if a message exists in Discord.js, you can use the fetchMessage() method in conjunction with error handling to determine if the message exists or not. Here's an example code snippet to demonstrate how to check if a message exists:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Get the message ID
const messageId = 'MESSAGE_ID_HERE';

// Fetch the message using its ID
message.channel.fetchMessage(messageId)
  .then((fetchedMessage) => {
    console.log('Message exists');
  })
  .catch((error) => {
    if (error.message === 'Unknown Message') {
      console.log('Message does not exist');
    } else {
      console.error('An error occurred:', error);
    }
  });


In this code snippet, replace 'MESSAGE_ID_HERE' with the actual ID of the message you want to check. The fetchMessage() method will attempt to fetch the message using its ID, and if successful, the .then() block will be executed indicating that the message exists. If the message does not exist, the .catch() block will catch the error and you can check for the specific error message to determine that the message does not exist.