首页 > 解决方案 > 为什么我会出错?TypeError:“无法读取未定义的属性‘执行’”

问题描述

在 main.js 中(不是那里的所有内容,但有什么问题):

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'ping'){
        client.commands.get('ping').execute(message, args);
    }  else if (command == 'hello'){
        client.commands.get('hello').execute(message, args);
    }  else if (command == 'command'){
        client.commands.get('command').execute(message, args, Discord);
    }  else if (command == 'clear'){
        client.commands.get('clear').execute(message, args);
    }  else if (command == 'kick'){
        client.commands.get('kick').execute(message, args);
    }  else if (command == 'ban'){
        client.commands.get('ban').execute(message, args);
    }  else if (command == 'mute'){
        client.commands.get('mute').execute(message,args);
    }  else if (command == 'wake'){
        client.commands.get('wake up').execute(message,args);
    }  else if (command == 'play'){
        client.commands.get('play').execute(message, args);
    }  else if (command == 'leave'){
        client.commands.get('leave').execute(message, args);
    }  else if (command == 'coin'){
        client.commands.get('coin').execute(message, args, Discord);
    }  else if (command == 'avatar'){
        client.commands.get('avatar').execute(client, message, args);
    }  else if (command == 'weather'){
        client.commands.get('weather').execute(client, message, args);
    }  else if (command == 'slowmode'){
        client.commands.get('slowmode').execute(message, args);
    }  else if (command == 'youtube'){
        client.commands.get('youtube').execute(message, args);
    }  else if (command == 'kill'){
        client.commands.get('kill').execute(message, args);
    }  else if (command == 'beep'){
        client.commands.get('beep').execute(message, args, Discord);
    }  else if (command == 'spam'){
        client.commands.get('spam').execute(message, args, Discord);
    }  else if (command == 'reactionrole'){
        client.commands.get('reactionrole').execute(message, args, Discord);
    } 
});

reactrole.js 中的内容:

module.exports = {
    name: 'reactionrole',
    description: "Sets up a reaction role message!",
    async execute(message, args, Discord, client) {
        const channel = '│announcements';
        const yellowTeamRole = message.guild.roles.cache.find(role => role.name === "New Role");
 
        const pepepog = 'pepepog';
 
        let embed = new Discord.MessageEmbed()
            .setColor('#e42643')
            .setTitle('Want A Custom Role?')
            .setDescription('Want Your own Custom Role? Yea, Thought So React In The Next 30 Minutes To Get Your Own Custom Role! @dioZ Will Make It When He Is Not Busy!!'
                + `${pepepog} for custom role :))`);
 
        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(pepepog);
 
        client.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.message.channel.id == channel) {
                if (reaction.emoji.name === pepepog) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(yellowTeamRole);
                }
            } else {
                return;
            }
 
        });
 
        client.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.name === pepepog) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(yellowTeamRole);               }
            } else {
                return;
            }
        });
    }
 
}   

标签: javascriptnode.jsdiscord.js

解决方案


您的问题解决起来超级简单,您将事件和命令相互混淆,请移步

 client.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.message.channel.id == channel) {
                if (reaction.emoji.name === pepepog) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(yellowTeamRole);
                }
            } else {
                return;
            }
 
        });
 
        client.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.name === pepepog) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(yellowTeamRole);               }
            } else {
                return;
            }
        });
    }

 // To your mainfile as it is an event and furthermore check if reactionrole.js is being exported properly~! Hope it helps

推荐阅读