【发布时间】:2022-01-10 18:09:48
【问题描述】:
Client.once('ready', () => {
console.log('Bot online')
});
Client.on("message", message => {
if (message.channel.id = '915302302682873909') {
const logChannel = Client.channels.cache.find(channel => channel.id === "915302371519770664");
//send author
logChannel.send(`${message.author}` + ": ")
.then(message => console.log(`Sent message: ${message.content}`));
//send message content
logChannel.send(message.content)
.then(message => console.log(`Sent message: ${message.content}`));
}
})
如果我启动我的机器人并发送一条消息以查看它是否工作,它会发送`${message.author}` + ": ",但随后立即崩溃说TypeError: Cannot read properties of undefined (reading 'send')
真正奇怪的是,有时(我不知道什么时候)它会在崩溃之前同时发送 `${message.author}` + ": " 和 message.content。
【问题讨论】:
-
表示找不到频道。不要在缓存中查找频道,而是尝试获取频道。
const logChannel = Client.channels.fetch('915302371519770664');。或者,如果您找不到频道,请返回该功能。if(!logChannel) return; -
如果我这样做,它表示 logChannel.send 不是函数
-
当您获取一个新频道时,请确保在异步函数中使用
await解决其承诺,或者将频道保存在.then回调函数中 -
另外,除了第一个发送方法之外,为什么还要使用模板文字?
-
我之前一直在寻找其他解决方案并尝试了一些东西,但后来没有将其删除
标签: javascript discord.js