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.
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});
}
}
};```
Nevermind. All I had to do was change the variables for 'targetUser' and 'targetRole' to interaction.options.getMember('user') //(my option name)
and interaction.options.getRole('role')
.
Then, just remove the alreadyHasRole
variable and make the if statement say if (!targetUser.roles.cache.has(targetRole.id)) {
.
After that, everything worked.
Heres my code if it helps:
const targetRole = interaction.options.getRole('role');
// const alreadyHasRole = targetUser.roles.Contains(targetRole)
if (!targetUser.roles.cache.has(targetRole.id)) {
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});
}