简体   繁体   English

已解决:DiscordJS v14:预期为未定义或 null,预期为 bigint 原语,预期为数字原语,预期为字符串原语

[英]SOLVED: DiscordJS v14: Expected undefined or null, Expected a bigint primitive, Expected a number primitive, Expected a string primitive

I am trying to make a ban command and this error keeps popping up:我正在尝试执行禁止命令,但此错误不断弹出:

D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:47
    throw this.error;
    ^

CombinedError: Received one or more errors
    at UnionValidator.handle (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:1058:23)
    at UnionValidator.parse (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:113:88)
    at validateDefaultMemberPermissions (D:\projs\js\MASST\node_modules\@discordjs\builders\dist\index.js:877:36)
    at MixedClass.setDefaultMemberPermissions (D:\projs\js\MASST\node_modules\@discordjs\builders\dist\index.js:1345:29)
    at Object.<anonymous> (D:\projs\js\MASST\commands\ban.js:15:4)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Module.require (node:internal/modules/cjs/loader:1028:19) {
  errors: [
    ValidationError: Expected undefined or null
        at NullishValidator.handle (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:718:79)
        at NullishValidator.run (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:99:23)
        at UnionValidator.handle (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:1053:32)
        at UnionValidator.parse (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:113:88)
        at validateDefaultMemberPermissions (D:\projs\js\MASST\node_modules\@discordjs\builders\dist\index.js:877:36)
        at MixedClass.setDefaultMemberPermissions (D:\projs\js\MASST\node_modules\@discordjs\builders\dist\index.js:1345:29)
        at Object.<anonymous> (D:\projs\js\MASST\commands\ban.js:15:4)
        at Module._compile (node:internal/modules/cjs/loader:1126:14)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
        at Module.load (node:internal/modules/cjs/loader:1004:32) {
      validator: 's.nullish',
      given: [ 2048n, 65536n, 4n ]
    },
    ValidationError: Expected a bigint primitive
        at BigIntValidator.handle (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:520:70)
        at BigIntValidator.run (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:99:23)
        at UnionValidator.handle (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:1053:32)
        at UnionValidator.parse (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:113:88)
        at validateDefaultMemberPermissions (D:\projs\js\MASST\node_modules\@discordjs\builders\dist\index.js:877:36)
        at MixedClass.setDefaultMemberPermissions (D:\projs\js\MASST\node_modules\@discordjs\builders\dist\index.js:1345:29)
        at Object.<anonymous> (D:\projs\js\MASST\commands\ban.js:15:4)
        at Module._compile (node:internal/modules/cjs/loader:1126:14)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
        at Module.load (node:internal/modules/cjs/loader:1004:32) {
      validator: 's.bigint',
      given: [ 2048n, 65536n, 4n ]
    },
    ValidationError: Expected a number primitive
        at NumberValidator.handle (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:866:70)
        at NumberValidator.run (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:99:23)
        at UnionValidator.handle (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:1053:32)
        at UnionValidator.parse (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:113:88)
        at validateDefaultMemberPermissions (D:\projs\js\MASST\node_modules\@discordjs\builders\dist\index.js:877:36)
        at MixedClass.setDefaultMemberPermissions (D:\projs\js\MASST\node_modules\@discordjs\builders\dist\index.js:1345:29)
        at Object.<anonymous> (D:\projs\js\MASST\commands\ban.js:15:4)
        at Module._compile (node:internal/modules/cjs/loader:1126:14)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
        at Module.load (node:internal/modules/cjs/loader:1004:32) {
      validator: 's.number',
      given: [ 2048n, 65536n, 4n ]
    },
    ValidationError: Expected a string primitive
        at StringValidator.handle (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:1672:70)
        at StringValidator.run (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:99:23)
        at UnionValidator.handle (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:1053:32)
        at UnionValidator.parse (D:\projs\js\MASST\node_modules\@sapphire\shapeshift\dist\index.js:113:88)
        at validateDefaultMemberPermissions (D:\projs\js\MASST\node_modules\@discordjs\builders\dist\index.js:877:36)
        at MixedClass.setDefaultMemberPermissions (D:\projs\js\MASST\node_modules\@discordjs\builders\dist\index.js:1345:29)
        at Object.<anonymous> (D:\projs\js\MASST\commands\ban.js:15:4)
        at Module._compile (node:internal/modules/cjs/loader:1126:14)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
        at Module.load (node:internal/modules/cjs/loader:1004:32) {
      validator: 's.string',
      given: [ 2048n, 65536n, 4n ]
    }
  ]
}

ban.js:禁令.js:

const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');

const botPerms = [
    PermissionFlagsBits.SendMessages,
    PermissionFlagsBits.ReadMessageHistory,
    PermissionFlagsBits.BanMembers,
];

module.exports = {
    data: new SlashCommandBuilder()
        // Command name. Example: /choices
        .setName('ban')
        // Command's description. Shows up in commands menu once / is typed.
        .setDescription('Bans specified user')
        .setDefaultMemberPermissions(botPerms)
        .addUserOption(option =>
            option
                .setName('user')
                .setDescription('User to ban.')
                .setRequired(true),
        )
        .addStringOption(option =>
            option
                .setName('reason')
                .setDescription('Why was the user banned?')
                .setRequired(true),
        ),

    async execute(interaction) {
        if (interaction.guild.members.me.permissions.has(botPerms)) {
            const user = interaction.options.getUser('user');
            const reason = interaction.options.getString('reason');

            if (user.bannable === true) {
                interaction.guild.members.ban(user);
                interaction.reply('Banned!');

                try {
                    user.send(`You have been banned from ${interaction.guild.name}!\nReason::${reason}`);
                }

                catch (error) {
                    interaction.followUp('Failed to DM banned user.');
                }
            }
        }
    },
};

deploy-commands.js:部署-commands.js:

const { REST, Routes } = require('discord.js');
const { clientId, token } = require('./config.json');
const fs = require('node:fs');

const commands = [];
// Grab all the command files from the commands directory you created earlier
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    commands.push(command.data.toJSON());
}

// Construct and prepare an instance of the REST module
const rest = new REST({ version: '10' }).setToken(token);

// and deploy your commands!
(async () => {
    try {
        console.log(`Started refreshing ${commands.length} application (/) commands.`);

        // The put method is used to fully refresh all commands in the guild with the current set
        const data = await rest.put(
            Routes.applicationCommands(clientId),
            { body: commands },
        );

        console.log(`Successfully reloaded ${data.length} application (/) commands.`);
    }
    catch (error) {
        // And of course, make sure you catch and log any errors!
        console.error(error);
    }
})();

What's wrong?怎么了? I as well couldn't find anything about it on the inte.net.我在 inte.net 上也找不到任何相关信息。 The logs don't really help, but I for sure know the error is caused by ban.js.日志并没有真正帮助,但我肯定知道错误是由 ban.js 引起的。 No idea how though.不知道如何。

node deploy-commands.js and node init.js — both gave out same error. node deploy-commands.jsnode init.js都给出了同样的错误。 I am really clueless about it.我真的是一窍不通。

Firstly, you are not handling your error.首先,你没有处理你的错误。 You'll get a nicely formatted error if you catch it... so catch it!如果你抓住它,你会得到一个格式很好的错误......所以抓住它!

Moving on to the problem, .setDefaultMemberPermissions(botPerms) is the issue.继续讨论问题, .setDefaultMemberPermissions(botPerms)是问题所在。 Let's explain the logs: it says it expected undefined or null , a bigint , a number or a string .让我们解释一下日志:它说它期望undefinednullbigintnumberstring However, you gave [2048n, 65536n, 4n] .但是,您给了[2048n, 65536n, 4n] An array, of course, is none of these.当然,数组不是这些。 This is why you get the error.这就是您收到错误的原因。

You cannot give a list of permissions here.您不能在此处提供权限列表。 You should do const botPerms = PermissionFlagsBits.SendMessages | PermissionFlagsBits.ReadMessageHistory | PermissionFlagsBits.BanMembers你应该做const botPerms = PermissionFlagsBits.SendMessages | PermissionFlagsBits.ReadMessageHistory | PermissionFlagsBits.BanMembers const botPerms = PermissionFlagsBits.SendMessages | PermissionFlagsBits.ReadMessageHistory | PermissionFlagsBits.BanMembers const botPerms = PermissionFlagsBits.SendMessages | PermissionFlagsBits.ReadMessageHistory | PermissionFlagsBits.BanMembers instead. const botPerms = PermissionFlagsBits.SendMessages | PermissionFlagsBits.ReadMessageHistory | PermissionFlagsBits.BanMembers代替。 The | | is a bitwise OR which joins the permissions together, setting the default member permissions to all of these.是将权限连接在一起的按位或,将默认成员权限设置为所有这些。

Here is the relevant documentation link: https://discordjs.dev/docs/packages/builders/main/SlashCommandBuilder:Class#setDefaultMemberPermissions这里是相关文档链接: https://discordjs.dev/docs/packages/builders/main/SlashCommandBuilder:Class#setDefaultMemberPermissions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM