繁体   English   中英

如何解决我的 discord 机器人遇到的这个错误

[英]How to fix this error I am getting with my discord bot

这是我的 reactrole.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;
            }
        });
    }


}

尝试为我的 discord 机器人创建反应角色,但是当我使用该命令时它会抛出此错误。

(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)

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

问题:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM