Having a problem with reactionRole discord bot

So I recently started to make a discord bot, I’m new to coding but I do have some basic!
So I mainly made the code and it’s working at 90%. The only problem is that when it comes to add or remove the roll. So when the user clicks the emote to get the role the bot reacts to the click but can’t define ‘guild’ so it doesn’t give the role.

module.exports = {
    name: 'reactionrole',
    description: "Best thing!!!",
    async execute(message, args, Discord, bot) {

        const channel = 'the id channel';
        const modRole = message.guild.roles.cache.find(role => role.name === "Mod");
        const defaultRole = message.guild.roles.cache.find(role => role.name === "El Gars");

        const modEmoji = '🐔';
        const defaultEmoji = '💩';

//========== My embed message with the info ========//
        let embed = new Discord.MessageEmbed()
            .setColor('#e42643')
            .setTitle(`just thing`)
            .setDescription(`just thing\n\n`
                + `${modEmoji} bla bla bla\n`
                + `${defaultEmoji} bla bla bla`);

        let messageEmbed = await message.channel.send(embed);
//========== Letting my bot add the reaction ======//
        messageEmbed.react(modEmoji);
        messageEmbed.react(defaultEmoji);

//========== Here to add the role =================//
        bot.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 === modEmoji) {
                    await reaction.message, guild, members.cache.get(user.id).roles.add(modRole);
                }
                if (reaction.emoji.name === defaultEmoji) {
                    await reaction.message, guild, members.cache.get(user.id).roles.add(defaultRole);
                }
            } else {
                return;
            }
        });

//========= Here to remove the role ============//
        bot.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 === modEmoji) {
                    await reaction.message, guild, members.cache.get(user.id).roles.remove(modRole);
                }
                if (reaction.emoji.name === defaultEmoji) {
                    await reaction.message, guild, members.cache.get(user.id).roles.remove(defaultRole);
                }
            } else {
                return;
            }
        });

    }
}

//========== In my main.js =========//


const Discord = require("discord.js");
const config = require("./config.json");
const fs = require('fs');
const bot = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION"] });

bot.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
  const command = require(`./commands/${file}`);

  bot.commands.set(command.name, command)
}

bot.on("ready", () => {
  console.log('Bot has started.');
});

bot.on('guildMemberAdd', guildMember => {
  let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'El Gars');

  guildMember.roles.add(welcomeRole);
  guildMember.guild.channels.cache.get('id channel').send(`Yo <@${guildMember.user.id}> kes tu criss icit!`)
});

bot.on('message', message => {
  if (!message.content.startsWith(config.prefix) || message.author.bot) return;

  const args = message.content.slice(config.prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();

  if (command === 'command') {
    bot.commands.get('command').execute(message, args, Discord);
  }

  if (command === 'clear') {
    bot.commands.get('clear').execute(message, args);
  } else if (command === 'ban') {
    bot.commands.get('ban').execute(message, args);
  } else if (command === 'kick') {
    bot.commands.get('kick').execute(message, args);
  } else if (command === 'mute') {
    bot.commands.get('mute').execute(message, args);
  } else if (command === 'unmute') {
    bot.commands.get('unmute').execute(message, args);
  } else if (command === 'reactionrole') {
    bot.commands.get('reactionrole').execute(message, args, Discord, bot);
  }


});



bot.login(config.token);

Here the error:
This is a peek at my error! 😀

17 thoughts on “Having a problem with reactionRole discord bot”

Leave a Comment