【问题标题】:Error: Invalid string format | const command = require(`${file}`);错误:无效的字符串格式 | const command = require(`${file}`);
【发布时间】:2022-08-14 08:51:23
【问题描述】:

我尝试了很多方法,但仍然没有成功...

这是错误:

Error: Invalid string format
    at Object.run (/home/runner/discordgame-1/node_modules/@sapphire/shapeshift/dist/index.js:1281:64)
    at /home/runner/discordgame-1/node_modules/@sapphire/shapeshift/dist/index.js:113:66
    at Array.reduce (<anonymous>)
    at StringValidator.parse (/home/runner/discordgame-1/node_modules/@sapphire/shapeshift/dist/index.js:113:29)
    at validateName (/home/runner/discordgame-1/node_modules/@discordjs/builders/dist/index.js:885:17)
    at MixedClass.setName (/home/runner/discordgame-1/node_modules/@discordjs/builders/dist/index.js:957:5)
    at Object.<anonymous> (/home/runner/discordgame-1/commands/ping.js:5:6)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)

代码:

for (const file of commandFiles) {
    const command = require(`${file}`);
    commands.push(command.data.toJSON());
  client.commands.set(command.data.name, command);
}

const command = require(${file}); 一定有问题

我第一次使用stackoverflow,因为我真的找不到解决方案。

如果您想了解有关此问题的更多信息,请询问我。

  • 你检查过file 的值吗? commands/ping.js 的第五行是什么?

标签: discord.js


【解决方案1】:

我在学习 discordjs 时遇到了同样的问题。我试图创建一些不和谐的命令,问题是我在setName() 中为命令选择的名称,我使用的是大写字母:

module.exports = {
    data: new SlashCommandBuilder()
        .setName('membersQuantity') // the uppercase Q is the problem
        .setDescription('members quantity'),
    async execute(interaction) {
        await interaction.reply('test');
    },
};

所以我只是改变了它:

module.exports = {
    data: new SlashCommandBuilder()
        .setName('membersquantity') //no uppercase characters anymore
        .setDescription('members quantity'),
    async execute(interaction) {
        await interaction.reply('test');
    },
};

它解决了我的问题。

斜杠命令的名称中可能有更多不允许的字符,所以我建议始终只使用小写字符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-23
    • 2021-11-12
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 2011-01-27
    相关资源
    最近更新 更多