【问题标题】:(node:2072) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined Discord js | Mongoose | MongoDB(节点:2072) UnhandledPromiseRejectionWarning:TypeError:无法读取未定义 Discord js 的属性“发送”|猫鼬 | MongoDB
【发布时间】:2021-08-16 21:46:23
【问题描述】:

我正在尝试使用事件处理程序在 discord js 上制作简单的欢迎消息。如果我在主文件上执行它会起作用,但是当我在这里尝试时它不起作用。代码如下:

const profileModel = require('../../database/models/profileSchema');
let WelcomeSchema = require(`../../database/models/welcomeSchema`)
const Discord = require(`discord.js`)
const mongoose = require(`mongoose`)

module.exports = (client, member, GuildMember) => {
 WelcomeSchema.findOne({ guildID: member.guild.id}, async (err, data, user) => {

    
    console.log(member.guild.id)
    if(!data) return;
    const channel = await client.channels.cache.find(x => x.id === `${data.WelcomeChannel}`)
    channel.send(`Welcome ${member}, ${data.WelcomeMsg}`)
  

})
}
Below Is My Event Handler (keep in mind the code above had the file name of, guildMemberAdd)
fs.readdirSync(`./events`).forEach(dirs => {
    const events = fs.readdirSync(`./events/${dirs}`).filter(file => file.endsWith(`.js`))
    for (const file of events) {
        console.log(`Loading discord.js event ${file}`);
        const event = require(`./events/${dirs}/${file}`);
        client.on(file.split(".")[0], event.bind(null, client));
    }
});

以下是完整的错误

(node:2072) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined
    at E:\Software\Github Repository shit\EA-BOT\events\Other\guildMemberAdd.js:15:13
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:2072) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2072) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Below Is Line 15 (where the error was at)
(line 14) const channel = await client.channels.cache.find(x => x.id === `${data.WelcomeChannel}`)
 (line 15)   channel.send(`Welcome ${member}, ${data.WelcomeMsg}`)

【问题讨论】:

    标签: mongodb mongoose discord discord.js


    【解决方案1】:

    错误是不言自明的。 Cannot read property 'send' of undefined 您正在尝试调用未定义的 .send() 方法 - 意思是 channel 未定义。如果我们看一下第 14 行,合乎逻辑的解释是没有通道,为此

    x.id === `${data.WelcomeChannel}`
    

    是真的。 用于您的目的的if (!data) return; 行不够具体。 data 似乎存在,但 data.WelcomeChannel 不存在,或者 ID 不存在。

    尝试使用if (!data.WelcomeChannel) return; 行,看看这是否会退出您的代码。然后仔细检查data.WelcomeChannel 是正确的频道ID(如果它实际上是id,而不是频道object)。

    【讨论】:

      【解决方案2】:

      而不是使用这个:

      const channel = await client.channels.cache.find(x => x.id === `${data.WelcomeChannel}`)
      

      要获取您的 Discord.channel,请尝试执行以下操作:

      const channel = await client.channels.cache.get(data.WelcomeChannel)
      

      注意:我无法自己测试修复,因为我目前正在上学,但这可能会解决您的问题。

      【讨论】:

        猜你喜欢
        • 2021-04-09
        • 1970-01-01
        • 2021-10-10
        • 2020-08-18
        • 2020-12-11
        • 1970-01-01
        • 1970-01-01
        • 2019-12-15
        • 2021-08-31
        相关资源
        最近更新 更多