'Discord.js button reaction role

I've been trying to make a reaction role in my bot but I keep getting this error

interaction.guild.roles.fetch(mustard).then(role => member.roles.add(role)).catch(console.error()) ^ TypeError: Cannot read properties of undefined (reading 'guild')

This is the full code:

const { 
  Client, 
  Message,
  MessageEmbed, 
  MessageActionRow, 
  MessageButton, 
  } = require('discord.js');
  const cooldown = new Set();

module.exports = {
  name: "quickroles",
  aliases: ["roles"],
  permissions: ["ADMINISTRATOR"],
  /**
   * @param {Client} client
   * @param {Message} message
   * @param {String[]} args
   */
  run: async (client, message, args, interaction) => {

    if(cooldown.has(message.author.id)) {
        message.reply(`Please wait! You're on a default cooldown of 5 seconds!`)
      } else {

    const row = new MessageActionRow().addComponents(
      new MessageButton()
      .setCustomId("pin")
      .setLabel("Pink")
      .setEmoji('🟥')
      .setStyle("SECONDARY"),
      new MessageButton()
      .setCustomId("mus")
      .setLabel("Mustard")
      .setEmoji('🟨')
      .setStyle("SECONDARY"),
      new MessageButton()
      .setCustomId("ski")
      .setLabel("Sky")
      .setEmoji('🟦')
      .setStyle("SECONDARY")
    )

    let pink = '927403952348221449'
    let mustard = '927403952427921412'
    let sky = '927403952411140137'

    let iuser = message.mentions.users.first() || message.author

    let embed = new MessageEmbed()
    .setTitle(`Color Roles`)
    .setDescription('🟥 | Pink\n🟨 | Mustard\n🟦 | Sky')
    .setURL("https://www.youtube.com/watch?v=rzVhR7ioqL4")
    .setColor('#ffdb58')

    const m = await message.channel.send({ embeds: [embed], components: [row] })

    const filter = (interaction) => {
      if(interaction.user.id === iuser.id) return true;
      return interaction.reply({content: "This isn't for u", ephemeral: true})
    }

    const collector = message.channel.createMessageComponentCollector({
      filter,
      max: 1,
    });

    collector.on("end", (ButtonInteraction) => {
      const id = ButtonInteraction.first().customId;

      let pp = new MessageEmbed()
    .setDescription(`You now have the <@&927403952348221449> role`)
    .setColor('#ffc0cb')

     m.edit(pp)

     let mm = new MessageEmbed()
    .setDescription(`You now have the <@&927403952427921412> role`)
    .setColor('#ffdb58')

     m.edit(mm)

     let ss = new MessageEmbed()
    .setDescription(`You now have the <@&927403952411140137> role`)
    .setColor('#ffdb58')

     m.edit(ss)

      if(id === "pin") {
        interaction.guild.roles.fetch(pink).then(role => member.roles.add(role)).catch(console.error())
        return ButtonInteraction.first().reply({embeds: [pp]}) 
      }else if(id === "mus") {
        interaction.guild.roles.fetch(mustard).then(role => member.roles.add(role)).catch(console.error())
        return ButtonInteraction.first().reply({embeds: [pp]}) 
      }else if(id === "ski") {
        interaction.guild.roles.fetch(sky).then(role => member.roles.add(role)).catch(console.error())
        return ButtonInteraction.first().reply({embeds: [pp]}) 
      }
    })
  } cooldown.add(message.author.id);
 setTimeout(() => {
   cooldown.delete(message.author.id)
 }, 5000)}
 
};

I'm pretty much new in coding so hopefully, you could help me figure out the problem and make the code work fully 😄



Solution 1:[1]

I think you didn't define "guild", try instead of

const {
    Client, 
    Message,
    MessageEmbed, 
    MessageActionRow, 
    MessageButton, 
} = require('discord.js');

with the following:

const { 
    Client,
    Guild,
    guild 
    Message,
    MessageEmbed, 
    MessageActionRow, 
    MessageButton, 
} = require('discord.js');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyler2P