1
votes

I have the following code writing text depending on what message was sent to it:

var text = null;
try {
 commandFiles.forEach((file) => {
  if (text != null) {
   return;
  }
  text = file.functionSwitch(event, command, commandArgs);
 });
} catch(err) {
 text = "I seem to have hit a problem. Please let bot creator look at it.";
 const errChannel = client.channels.cache.find(channel => channel.id === errChannelID);
 errChannel.send(err.message);
}

The code gives the following error:

TypeError: Cannot read property 'send' of undefined

I am not sure how to fix this error or properly find the channel. I have attempted with both get and find.

Thank you in advance.

1

1 Answers

0
votes

Try to fetch the channel by its ID. fetch() returns a promise, and once it's resolved you'll receive the channel (I named it ch below) that has the ID errChannelID. So, if the channel with that ID exists, you can use its send() method to send the message, if it doesn't exist the promise is rejected and the function inside the catch() method runs:

client.channels
  .fetch(errChannelID)
  .then((ch) => ch.send(err.message))
  .catch(console.error);

Make sure that errChannelID is a valid snowflake and is a string. If you store it as an integer and it's larger than Number.MAX_SAFE_INTEGER (2^53) it will not be representable in the number type and will be converted to something else by JavaScript.