OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to remove a role from a user using the ID & check if they have a certain role using the ID discord.js v14

  • Thread starter Thread starter Camcrapb12
  • Start date Start date
C

Camcrapb12

Guest
I have searched EVERYWHERE, and cannot find the answer.

So basically I'm making a role give & role remove command. I want to check if they already have the role, so I can stop the code. But it seems that everything I put after .roles just stops working. For example, I've tried .has,.remove, .cache, and .Contains, none of those worked. Does anyone have a solution?

I'm a beginner at coding so I apologize if anything is just flat-out stupid.

Code:
module.exports = {
    name: 'roleremove',
    description: 'Removes a specified role from a user.',
    // devOnly: Boolean,
    // testOnly: Boolean,
        // deleted: true,
    options: [
        {
            name: 'user',
            description: 'The user to remove the role from.',
            required: true,
            type: ApplicationCommandOptionType.User,
        },
        {
            name: 'role',
            description: 'The role you want to remove',
            required: true,
            type: ApplicationCommandOptionType.Role,
        }
    ],

    callback: async (client, interaction) => {
        const targetUser = interaction.options.get('user').value;
        const targetRole = interaction.options.get('role').value;
        const alreadyHasRole = targetUser.roles.Contains(targetRole)
        if (!alreadyHasRole) {
          interaction.reply({ content: `❌ | Could not remove ${targetUser}'s role because they dont have that role.`, ephemeral: true });
        } else {
          await targetUser.roles.remove(targetRole);
          interaction.reply({ content: `✅ | Successfully removed ${targetUser}'s role.`, ephemeral: true});
        }
      }
    };```

<p>I have searched EVERYWHERE, and cannot find the answer.</p>
<p>So basically I'm making a role give & role remove command. I want to check if they already have the role, so I can stop the code. But it seems that everything I put after <code>.roles</code> just stops working. For example, I've tried <code>.has</code>,<code>.remove</code>, <code>.cache</code>, and <code>.Contains</code>, none of those worked. Does anyone have a solution?</p>
<p>I'm a beginner at coding so I apologize if anything is just flat-out stupid.</p>
<pre><code>
module.exports = {
name: 'roleremove',
description: 'Removes a specified role from a user.',
// devOnly: Boolean,
// testOnly: Boolean,
// deleted: true,
options: [
{
name: 'user',
description: 'The user to remove the role from.',
required: true,
type: ApplicationCommandOptionType.User,
},
{
name: 'role',
description: 'The role you want to remove',
required: true,
type: ApplicationCommandOptionType.Role,
}
],

callback: async (client, interaction) => {
const targetUser = interaction.options.get('user').value;
const targetRole = interaction.options.get('role').value;
const alreadyHasRole = targetUser.roles.Contains(targetRole)
if (!alreadyHasRole) {
interaction.reply({ content: `❌ | Could not remove ${targetUser}'s role because they dont have that role.`, ephemeral: true });
} else {
await targetUser.roles.remove(targetRole);
interaction.reply({ content: `✅ | Successfully removed ${targetUser}'s role.`, ephemeral: true});
}
}
};```
</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top