如何修复我的不和谐机器人遇到的这个错误

时间:2021-01-23 17:16:59

标签: javascript discord

这是我的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)

1 个答案:

答案 0 :(得分:0)

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

问题:

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)