Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i can't assign a role to an user, via my code

const Discord = require("discord.js");
require('dotenv').config();

module.exports = {
  name: "manuallyverify",
  description: "Manually verifies an User.",

  limits: {
    owner: false,
    permission: ["MANAGE_ROLES"],
    cooldown: "10s",
  },

  options: [
    {
      name: "user",
      description: "The User that will be affected by this action.",
      type: 6,
      required: true,
    }
  ],

  run: (client, interaction) => {
    let member = interaction.options.getUser("user").toString()
    let role = interaction.guild.roles.cache.find(role => role.id == process.env.verified_role_id)
    let embed = new Discord.MessageEmbed()
      .setTitle(":white_check_mark: ・ Manually Verified the User.")
      .addFields(
        {
          name: "User affected",
          value: interaction.options.getUser("user").toString(),
          inline: true,
        },

        {
          name: "Role given",
          value: `${role}`,
          inline: true,
        }
      )
      .setColor("#95eda4");

    try {
      interaction.guild.member.roles.cache.add(role)
      interaction.reply({ embeds:  });
    } catch (e) {
      interaction.reply({content:`I encountered an error: ${e}`, ephemeral: true});
    }

  },
};

so, this is my code, i’m making a simple verification bot that will be a template on github , but when i run the command, it gives me this error:

TypeError: Cannot read properties of undefined (reading ‘roles’)

so, i guess the error is because something is undefinited between interaction.guild, or guild.member. i didnt code for a while so i’m probably missing / forgetting something
also, sorry if my framework is bad

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

edit: i got this code to work thanks to a guy in the comments, so, thanks for coming here and have a good day

>Solution :

There isn’t any property called interaction.guild.member and I assume what you are trying to do is to add the role to the user provided. Therefore, what you could do is use interaction.options.getMember("user") to get the user in the option and then add the role to that GuildMember. A small example:

const Discord = require("discord.js");
require('dotenv').config();

module.exports = {
  name: "manuallyverify",
  description: "Manually verifies an User.",

  limits: {
    owner: false,
    permission: ["MANAGE_ROLES"],
    cooldown: "10s",
  },

  options: [
    {
      name: "user",
      description: "The User that will be affected by this action.",
      type: 6,
      required: true,
    }
  ],

  run: (client, interaction) => {
    let memberToAddRole = interaction.options.getMember("user")
    let role = interaction.guild.roles.cache.find(role => role.id == process.env.verified_role_id)
    let embed = new Discord.MessageEmbed()
      .setTitle(":white_check_mark: ・ Manually Verified the User.")
      .addFields(
        {
          name: "User affected",
          value: memberToAddRole.user.toString(),
          inline: true,
        },

        {
          name: "Role given",
          value: `${role}`,
          inline: true,
        }
      )
      .setColor("#95eda4");

    try {
      memberToAddRole.roles.cache.add(role)
      interaction.reply({ embeds:  });
    } catch (e) {
      interaction.reply({content:`I encountered an error: ${e}`, ephemeral: true});
    }

  },
};

Note: the code you provided was a bit inefficient and redundant in some parts, so I have changed that accordingly

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading