Discord js找不到发送错误消息的通道



根据发送给它的消息,我有以下代码编写文本:

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);
}

该代码给出以下错误:

TypeError:无法读取未定义的属性"send">

我不知道如何修复此错误或正确查找频道。我已尝试使用getfind

提前谢谢。

尝试通过ID获取通道。fetch()返回一个promise,一旦解决,您将收到ID为errChannelID的通道(我在下面将其命名为ch(。因此,如果具有该ID的通道存在,您可以使用其send()方法发送消息,如果不存在,则promise将被拒绝,并且catch()方法中的函数将运行:

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

请确保errChannelID是一个有效的雪花,并且是一个字符串。如果您将其存储为整数,并且它大于Number.MAX_SAFE_INTEGER(2^53(,则它将无法在数字类型中表示,并将通过JavaScript转换为其他内容。