【问题标题】:(node:30) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'on' of undefined(节点:30)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“on”
【发布时间】:2021-11-03 03:20:43
【问题描述】:

我不明白这里有什么问题?它一直告诉我

(node:30) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'on' of undefined

是因为我将client.on 事件放在命令中而不是机器人核心文件中吗?我尝试将两个client.on 事件都放在那里,但当我执行命令时,它仍然没有给我配置的角色。

const Discord = require("discord.js");
const {MessageEmbed} = require('discord.js')
const bot = new Discord.Client();


module.exports = {
    name: 'games',
    description: "Select the games",
    async execute(message, args, Discord, bot) {
        if(message.member.roles.cache.has('552561546039918593')) {
        const channel = '905072473899409438';
        const gifsRole = message.guild.roles.cache.find(role => role.name === "GIFS");
        const emojiRole = message.guild.roles.cache.find(role => role.name === "EMOJIS");
  
        const gifs = ('<:hersheys:823607413843951646>');
        const emoji = ('<:glokez:858101005512867892>');
        const gifsEmoij = ('823607413843951646');
        const emojiEmoij = ('858101005512867892');
  
        let embed = new MessageEmbed()
            .setColor('#e42643')
            .setTitle('<a:sparkles1:905070246610739240>  **Role Selection**')
            .setDescription(`Choose what roles you want to access!\n\n`
                + `${gifs}   **(**    GIFS                       **)**\n`
                + `${emoji}   **(**    EMOJIS     **)**`);
        
        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(gifsEmoij);
        messageEmbed.react(emojiEmoij);
  
        bot.on('messageReactionAdd', async (reaction, user) => {
  
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;
  
            if (reaction.emoji.id === gifsEmoji) {
                await reaction.message.guild.members.cache.get(user.id).roles.add(gifsRole);
            }
            if (reaction.emoji.id === emojiEmoji) {
                await reaction.message.guild.members.cache.get(user.id).roles.add(emojiRole);
            }
            else {
            return;
        }
  
        });
  
        bot.on('messageReactionRemove', async (reaction, user) => {
  
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;
  
  
            if (reaction.message.channel.id == channel) {
                if (reaction.emoji.id === gifsEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(gifsRole);
                }
                if (reaction.emoji.id === emojiEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(emojiRole);
                }
            } else {
                return;
            }
        });
    }}}

【问题讨论】:

    标签: node.js discord


    【解决方案1】:

    在文件的顶部,创建一个标识符为 bot 的变量:

    const bot = new Discord.Client();
    

    execute 函数的定义中,您还定义了一个名为“bot”的局部变量:

    async execute(message, args, Discord, bot) {
    

    当您调用execute("Hello World", [], someDiscordClient) 时,bot 将在您的函数中未定义。它不会“回退”到上限范围内的值。由于 undefined 没有名为 'on' 的属性——事实上,它根本没有属性——你会得到“Cannot read property 'on' of undefined”。

    您可能希望从参数列表中删除 bot

    或者,你可以这样做,如果你想要 option 而不是 obligation 来设置变量:

    async execute(message, args, Discord, botInstance = bot) {
         // Use the value given or, if not set, use the fallback.
    
         // ... rest of your code
         botInstance.on('messageReactionAdd', (etcetera)
    

    【讨论】:

    • 好吧,但我已将机器人重命名为“客户端”并首先添加它,因为当我在服务器中执行命令时它给了我这个错误(node:30) UnhandledPromiseRejectionWarning: ReferenceError: client is not defined。我意味着它仍然发送嵌入和所有内容,但它没有给我任何角色,我不知道为什么,我已经更新了很多代码,这是代码sourceb.in/B66stl6jPT
    • 您应该更新您的原始问题(或关闭此问题并打开一个新问题)——现在您已经解决了第一个问题,您遇到了一个非常不同的问题(关于 Discord 的 api 的使用) )。您应该更新修改后的代码并包含一些 console.log 语句以帮助其他人知道您的问题在哪里
    • 好的,谢谢。我会开一个新的。
    猜你喜欢
    • 2021-10-10
    • 2020-08-18
    • 2020-12-11
    • 2019-12-15
    • 2021-08-31
    • 2021-09-01
    • 2021-07-05
    • 1970-01-01
    • 2020-12-27
    相关资源
    最近更新 更多