首页 > 解决方案 > 如何解决我的不和谐机器人遇到的这个错误

问题描述

这是我的reactionrole.js 文件,我收到一个错误并且不知道如何解决这个问题,我一般是javascript 编码的新手,我有一个机器人的命令处理程序,而不是将我的所有命令都放在我的main.js 中文件(以前都在 main.js 文件中)。我将所有命令移到它们自己的文件中以尝试使 main.js 更小,这样做后我的机器人的所有其他功能都可以正常工作,但我的反应角色却没有。

module.exports = {
    name: 'reactionrole',
    description: 'Sets up a reaction role message.',
    async execute(message, args, Discord, client) {
        const channel = '802539365985157141';
        const yellowTeamRole = message.guild.roles.cache.find(role => role.name === "test");
        const blueTeamRole = message.guild.roles.cache.find(role => role.name === "test2");

        const yellowTeamEmoji = '';
        const blueTeamEmoji = '';

        let embed = new Discord.MessageEmbed()
            .setColor('#e42643')
            .setTitle('Choose a team to play on!')
            .setDescription('Choosing a team will allow you to interact with your teammates!\n\n' +
                `${yellowTeamEmoji} for yellow team\n` +
                `${blueTeamEmoji} for blue team`);

        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(yellowTeamEmoji);
        messageEmbed.react(blueTeamEmoji);

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


}

试图为我的不和谐机器人制作反应角色,但是当我使用该命令时它会抛出此错误。

(node:11292) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'roles' of undefined
at Object.execute (F:\GitHub\vBot\commands\reactionrole.js:7:46)
at module.exports (F:\GitHub\vBot\events\guild\message.js:13:25)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (F:\GitHub\vBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (F:\GitHub\vBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (F:\GitHub\vBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (F:\GitHub\vBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (F:\GitHub\vBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (F:\GitHub\vBot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:315:20)

标签: javascriptdiscord

解决方案


我在最后执行客户端而不是先执行它..

问题:

module.exports = {
name: 'reactionrole',
description: 'Sets up a reaction role message.',
async execute(Discord, message, args, client)

解决方案:

module.exports = {
name: 'reactionrole',
description: 'Sets up a reaction role message.',
async execute(client, message, args, Discord)

推荐阅读