【问题标题】:discord.js Cannot read properties of undefined (reading 'send') after sending the messagediscord.js 发送消息后无法读取未定义的属性(读取“发送”)
【发布时间】: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


【解决方案1】:

这意味着logChannel 未定义。

如果你的 id 是正确的,你可以这样定义它:

const logChannel = Client.channels.cache.get('915302371519770664') || Client.channels.fetch('915302371519770664');```

【讨论】:

    猜你喜欢
    • 2021-04-16
    • 2020-06-05
    • 2021-04-26
    • 1970-01-01
    • 2020-06-17
    • 2018-12-01
    • 2021-11-02
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多