Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

javascript - How to fix this error I am getting with my discord bot

This is my reactionrole.js file, I am getting an error and have no idea how to fix this, I am new to javascript coding in general, I have a command handler for the bot instead of putting all my commands in my main.js file (it previously was all in the main.js file). I moved all the commands to their own files to try make the main.js smaller, all other features of my bot are working after doing so but not my reaction roles.

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!

' +
                `${yellowTeamEmoji} for yellow team
` +
                `${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;
            }
        });
    }


}

Trying to make reaction roles for my discord bot but it's throwing this error when I use the command.

(node:11292) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'roles' of undefined
at Object.execute (F:GitHubvBotcommands
eactionrole.js:7:46)
at module.exports (F:GitHubvBoteventsguildmessage.js:13:25)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (F:GitHubvBot
ode_modulesdiscord.jssrcclientactionsMessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (F:GitHubvBot
ode_modulesdiscord.jssrcclientwebsockethandlersMESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (F:GitHubvBot
ode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:384:31)
at WebSocketShard.onPacket (F:GitHubvBot
ode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:444:22)
at WebSocketShard.onMessage (F:GitHubvBot
ode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:301:10)
at WebSocket.onMessage (F:GitHubvBot
ode_moduleswslibevent-target.js:132:16)
at WebSocket.emit (events.js:315:20)
question from:https://stackoverflow.com/questions/65862107/how-to-fix-this-error-i-am-getting-with-my-discord-bot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was executing client at the end instead of executing it first..

Issue:

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

Solution:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...