Home > OS >  TypeError Option "role" is of type 8; expected 3
TypeError Option "role" is of type 8; expected 3

Time:10-24

So I am making a role add and role remove command using slash command options.

I have saved an argument called role which takes from the slash command option addRoleOption. Upon running the slash command and pointing in the role I am trying to add / remove from the user I have mentioned i get the error as shown below.

I set this option to string (but then only works with the role ID) and it works perfectly fine. But I would like it to be a role option so the user using the bot doesn't have to try and find the role ID.

CODE:

const { Client, SlashCommandBuilder, PermissionFlagBits, EmbedBuilder, PermissionFlagsBits } = require("discord.js");
const ms = require("ms");

module.exports = {
    data: new SlashCommandBuilder()
        .setName("role")
        .addSubcommand(subcommand => subcommand.setName("add").setDescription('Adds a role to a member.').addUserOption(option => option.setName("target").setDescription("Select the user you wish to add the role to.").setRequired(true)).addRoleOption(option => option.setName("role").setDescription("The role (id) you want to add to the user.")))
        .addSubcommand(subcommand => subcommand.setName("remove").setDescription('Removes a role from a member.').addUserOption(option => option.setName("target").setDescription("Select the user you wish to remove the role from.").setRequired(true)).addRoleOption(option => option.setName("role").setDescription("The role (id) you want to from from the user.")))
        .setDescription("Roles")
        .setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers),

    async execute(interaction) {
        const { guild, options } = interaction

        const user = options.getUser("target")
        const member = guild.members.cache.get(user.id);
        const role = options.getString("role");

        const errEmbed = new EmbedBuilder()
            .setDescription('Something went wrong. Please try again later.')
            .setColor(0xc72c3b)

        if(interaction.options.getSubcommand() === "add")
        {
            const successEmbed = new EmbedBuilder()
            .setTitle("**:white_check_mark: Added Role**")
            .setDescription(`Succesfully added the role to the  ${user}.`)
            .setColor(0x5fb041)
            .setTimestamp();

            
        if(!interaction.guild.members.me.permissions.has(PermissionFlagsBits.ModerateMembers))
                return interaction.reply({ embeds: [errEmbed], ephemeral: true });
            

        try {
            await member.roles.add(role);

            interaction.reply({ embeds: [successEmbed], ephemeral: true });
            //await guild.channels.cache.get('1031302408934015016').send(`${interaction.member.name} has muted ${user} for the time ${time} for the reason ${reason}`)
        } catch (err) {
            console.log(err);
        }
        }
        else if(interaction.options.getSubcommand() === "remove")
        {
            const successEmbed = new EmbedBuilder()
            .setTitle("**:white_check_mark: Removed Role**")
            .setDescription(`Succesfully removed the role from the  ${user}.`)
            .setColor(0x5fb041)
            .setTimestamp();

            
        if(!interaction.guild.members.me.permissions.has(PermissionFlagsBits.ModerateMembers))
                return interaction.reply({ embeds: [errEmbed], ephemeral: true });
            

        try {
            await member.roles.remove(role);

            interaction.reply({ embeds: [successEmbed], ephemeral: true });
            //await guild.channels.cache.get('1031302408934015016').send(`${interaction.member.name} has muted ${user} for the time ${time} for the reason ${reason}`)
        } catch (err) {
            console.log(err);
        }
        }
    }
}

ERROR:

C:\Users\Robin\Documents\Multi-Purpose Bot\node_modules\discord.js\src\structures\CommandInteractionOptionResolver.js:99
      throw new DiscordjsTypeError(ErrorCodes.CommandInteractionOptionType, name, option.type, type);
            ^

TypeError [CommandInteractionOptionType]: Option "role" is of type: 8; expected 3.
    at CommandInteractionOptionResolver._getTypedOption (C:\Users\Robin\Documents\Multi-Purpose Bot\node_modules\discord.js\src\structures\CommandInteractionOptionResolver.js:99:13)
    at CommandInteractionOptionResolver.getString (C:\Users\Robin\Documents\Multi-Purpose Bot\node_modules\discord.js\src\structures\CommandInteractionOptionResolver.js:160:25)
    at Object.execute (C:\Users\Robin\Documents\Multi-Purpose Bot\Commands\Moderation\role.js:17:30)
    at Object.execute (C:\Users\Robin\Documents\Multi-Purpose Bot\Events\interactions\interactionCreate.js:12:21)
    at Client.<anonymous> (C:\Users\Robin\Documents\Multi-Purpose Bot\Handlers\eventHandler.js:25:63)
    at Client.emit (node:events:527:28)
    at InteractionCreateAction.handle (C:\Users\Robin\Documents\Multi-Purpose Bot\node_modules\discord.js\src\client\actions\InteractionCreate.js:81:12)
    at Object.module.exports [as INTERACTION_CREATE] (C:\Users\Robin\Documents\Multi-Purpose Bot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (C:\Users\Robin\Documents\Multi-Purpose Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)
    at WebSocketShard.onPacket (C:\Users\Robin\Documents\Multi-Purpose Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:480:22) {
  code: 'CommandInteractionOptionType'
}
PS C:\Users\Robin\Documents\Multi-Purpose Bot> 

CodePudding user response:

Found the issue and it is a very nooby issue. Not me checking my code properly.

I had copied and pasted code from my mute command and just adjusted things.

I had forgotten to change a bit of code.

Instead of it being.

const role options.getString("role");

It needs to be

const role options.getRole("role");
  • Related