【问题标题】:How to fix this error I am getting with my discord bot如何解决我的不和谐机器人遇到的这个错误
【发布时间】:2021-04-27 22:05:54
【问题描述】:

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


}

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

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

【问题讨论】:

  • 我猜message.guild 是未定义的。您需要进行更多调试才能找出原因。

标签: javascript discord


【解决方案1】:

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

问题:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-05
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 2021-09-22
    • 2022-06-22
    • 2021-08-19
    相关资源
    最近更新 更多