mongoose TypeError选项“role”的类型为8;预期为3

gz5pxeao  于 2022-11-13  发布在  Go
关注(0)|答案(1)|浏览(137)

因此,我使用斜杠命令选项创建了一个角色添加和角色删除命令。
我保存了一个名为role的参数,它来自斜杠命令选项addRoleOption。在运行斜杠命令并指向我试图从我提到的用户中添加/删除的角色时,我得到如下所示的错误。
我把这个选项设置为string(但只对角色ID有效),它运行得很好。但我希望它是一个角色选项,这样使用机器人的用户就不必试图找到角色ID。
代码:

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);
        }
        }
    }
}

错误:

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>
dgtucam1

dgtucam11#

发现了这个问题,这是一个非常nooby的问题。不是我检查我的代码正确。
我已经复制并粘贴了静音命令的代码,只是调整了一下。
我忘了修改一点代码。
而不是顺水推舟。

const role options.getString("role");

它必须是

const role options.getRole("role");

相关问题