NodeJS 部署斜杠命令时出错Discord. js V13 [已关闭]

hlswsv35  于 2023-01-08  发布在  Node.js
关注(0)|答案(1)|浏览(159)

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
3天前关闭。
Improve this question
在我的bots命令中添加了葡萄牙语选项和描述后,它就不再注册它们了。我的discord.js版本是13.7.0,我的node版本是17.2.0。对于命令选项名称/描述,有没有我可能违反的标准,比如在其中使用拉丁字符或空格?
Deploy.js:

const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const dotenv = require('dotenv').config();

const staging = process.argv.includes('--staging');
const reset = process.argv.includes('--reset');

const commands = [];
const commandFiles = fs.readdirSync('./commands').filter((file) => file.endsWith('.js'));

if (!reset) {
    for (const file of commandFiles) {
        const command = require(`./commands/${file}`);
        commands.push(command.data.toJSON());
    }
}

const rest = new REST({ version: '9' }).setToken(process.env.token);

(async () => {
    try {
        if (!reset) {
            if (staging) {
                await rest.put
                (
                    Routes.applicationGuildCommands(process.env.clientId, process.env.guildId),
                    { body: commands },
                );

                console.log('Successfully registered staging commands.');
            }
            else {
                await rest.put
                (
                    Routes.applicationCommands(process.env.clientId),
                    { body: commands },
                );

                console.log('Successfully registered global commands.');
            }
        }
        else {
            await rest.put
            (
                Routes.applicationGuildCommands(process.env.clientId, process.env.guildId),
                { body: commands },
            );

            await rest.put
            (
                Routes.applicationCommands(process.env.clientId),
                { body: commands },
            );

            console.log('Successfully reset all commands.');
        }
    }

爱情兼容性命令(以显示为例):

const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageEmbed } = require('discord.js');
const { Client, Collection, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const dotenv = require('dotenv').config();

module.exports =
{
    async execute(interaction) {

        const user = interaction.options.getUser('target');

        const id = user.id;

        const authorID = interaction.user.id;

        const randomValue = Math.floor(Math.random() * 100);

        const locales = {
            "pt-BR": `A compatibilidade entre <@${id}> e <@${authorID}> é ${randomValue}%!`,
        };

        await interaction.reply({ ephemeral: false, content: locales[interaction.locale] ?? `The compatibility between <@${id}> and <@${authorID}> is ${randomValue}%!` });

        if (randomValue == 100)
        {
            const locales100 = {
                "pt-BR": `Parabéns, vocês são 100% compativeis!`,
            };

            await interaction.followUp({ephemeral: false, content: locales100[interaction.locale] ?? `Congrats! You guys are 100% compatible!!!`});
        }
       
    },

    data: new SlashCommandBuilder()
        .setName('lovecompatibility').setNameLocalizations({ "pt-BR": 'compatibilidadeamorosa', })
        .setDescription('check\'s your compatibility with others!').setDescriptionLocalizations({ "pt-BR": 'Vê a tua compatibilidade com outros!', })
        .addUserOption((option) => option.setName('target').setNameLocalizations({ "pt-BR": 'alvo', }).setDescription('Select a user to check your compatibility with them!').setDescriptionLocalizations({ "pt-BR": 'Seleciona um utilizador para ver a tua compatibilidade com eles!', }).setRequired(true)),
};

错误:

DiscordAPIError[50035]: Invalid Form Body
0.options[0].name_localizations.pt-BR[STRING_TYPE_REGEX]: String value did not match validation regex.
    at SequentialHandler.runRequest (D:\Git Repos\source\repos\ShirouBot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.js:198:23)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (D:\Git Repos\source\repos\ShirouBot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.js:99:20)
    at async D:\Git Repos\source\repos\ShirouBot\deploy.js:25:5 {
  rawError: {
    code: 50035,
    errors: { '0': [Object] },
    message: 'Invalid Form Body'
  },
  code: 50035,
  status: 400,
  method: 'put',
  url: 'https://discord.com/api/v9/applications/692585507384983622/guilds/692741333936373901/commands'
}

提前感谢您的帮助!

oaxa6hgo

oaxa6hgo1#

我找到了答案。我在我的化身命令上的一个本地化选项名称中有一个空格。
以下是之前的情况(当它导致错误时):

data: new SlashCommandBuilder()
        .setName('avatar').setNameLocalizations({ "pt-BR": 'fotodeperfil', })
        .setDescription('Gets the avatar of whoever you mention').setDescriptionLocalizations({ "pt-BR": 'Pega o avatar de quem mencionares', })
        .addUserOption((option) => option.setName('mentioneduser').setNameLocalizations({ "pt-BR": 'utilizador mencionado', }).setDescription('Select a user to get their avatar').setDescriptionLocalizations({ "pt-BR": 'Seleciona o utilizador do avatar que queres roubar', })),
};

以下是固定版本:

data: new SlashCommandBuilder()
        .setName('avatar').setNameLocalizations({ "pt-BR": 'fotodeperfil', })
        .setDescription('Gets the avatar of whoever you mention').setDescriptionLocalizations({ "pt-BR": 'Pega o avatar de quem mencionares', })
        .addUserOption((option) => option.setName('mentioneduser').setNameLocalizations({ "pt-BR": 'utilizadormencionado', }).setDescription('Select a user to get their avatar').setDescriptionLocalizations({ "pt-BR": 'Seleciona o utilizador do avatar que queres roubar', })),
};

通过删除空格,本地化的名称符合注册discord.js命令时需要满足的格式要求,即命令或选项名称中不能有空格。

相关问题