ValidationError > s.string 需要一个字符串原语 收到:| 不明确的

Ish*_*ain 6 javascript rest node.js discord discord.js

我使用不和谐js为我的服务器制作一个多用途不和谐机器人,但它给了我这个错误:

ValidationError:需要一个字符串基元

昨天工作正常,但我忘记保存一些东西,我不知道保存什么。

const fs = require('node:fs');
const path = require('node:path');
const {
  Client,
  GatewayIntentBits,
  Partials,
  Collection,
} = require("discord.js");

const { Player } = require('discord-player');
const { Routes } = require('discord-api-types/v10');
const { token, prefix, guildId, clientId } = require('./config.json');
const { REST } = require('@discordjs/rest');
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.GuildPresences,
    GatewayIntentBits.GuildVoiceStates
  ],
  partials: [
    Partials.Channel,
    Partials.Message,
    Partials.User,
    Partials.GuildMember,
  ],
});

client.player = new Player(client, {
  ytdlOptions: {
    quality: "highestaudio",
    highWaterMark: 1 << 25
  }
});

module.exports = client;

//  command handler

//slash commands 
const slashCommands = [];
client.slashCommands = new Collection();

const commandsPath = path.join(__dirname, "commands"); // E:\yt\discord bot\js\intro\commands
const slash_commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('S.js'));
for(const file of slash_commandFiles)
{
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);

    client.slashCommands.set(command.data.name, command);
    slashCommands.push(command.toJSON());
}

console.log(slashCommands);

//message commands
client.messageCommands = new Collection();
const message_commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('M.js'));
for (const file of message_commandFiles) {
  const filePath = path.join(commandsPath, file);
  const command = require(filePath);
  // Set a new item in the Collection
  // With the key as the command name and the value as the exported module
  client.messageCommands.set(command.Name, command);
}

//event handler
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
  const filePath = path.join(eventsPath, file);
  const event = require(filePath);
  if (event.once) {
    client.once(event.name, (...args) => event.execute(...args));
  } else {
    client.on(event.name, (...args) => event.execute(...args));
  }
}

// messageCommand handler

client.on('messageCreate', (message) => {
  const args = message.content.slice(prefix.length).split(' ');
  const command = args[0];
  if (client.messageCommands.get(command)) {
    let Command = client.messageCommands.get(command);
    Command.execute(message);
  }
});

client.on('ready', () => {
  const rest = new REST({ version: '9' }).setToken(token);
  rest.put(Routes.applicationGuildCommands(clientId, guildId),
    { body: slashCommands })
    .then(() => console.log('Successfully updated commands for guild ' + guildId))
    .catch(console.error);
  console.log('bot is online!');
  client.user.setStatus('idle');
});
client.login(token);

Run Code Online (Sandbox Code Playgroud)

我确信任何命令文件都没有问题,因为它昨天工作正常。这行有 100% 错误lashCommands.push(command.toJSON()); 但我似乎无法修复它。command.toJSON() 控制台日志记录得很好,但在尝试推入列表时出现错误

moi*_*dol 9

遇到了同样的问题,结果选项(即addUserOption)需要描述。重点是,这真的很令人困惑,因为在执行此操作时会出现此错误command.data.toJSON()。如果您按照指南中所述动态加载命令文件并遇到此问题,请尝试手动执行require预先触发验证。


小智 0

尝试使用command.data.toJSON()带有a和keycommand的嵌套对象。dataexecute