最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ValidationError > s.string Expected a string primitive Received: | undefined - Stack Overflow

programmeradmin3浏览0评论

Im using discord js to make a multi-purpose discord bot for my server, but its giving me this error:

ValidationError: Expected a string primitive

It was working fine yesterday but i forgot to save something and i dont know what.

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;

//  mand handler

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

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

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

console.log(slashCommands);

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

//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 mand = args[0];
  if (client.messageCommands.get(mand)) {
    let Command = client.messageCommands.get(mand);
    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 mands for guild ' + guildId))
    .catch(console.error);
  console.log('bot is online!');
  client.user.setStatus('idle');
});
client.login(token);

im sure there's nothing wrong with any of the mand files because it was working fine yesterday. there's 100% an error in this line slashCommands.push(mand.toJSON()); but I cant seem to fix it. The mand.toJSON() console logs just fine but gives an error while trying to push into the list

Im using discord js to make a multi-purpose discord bot for my server, but its giving me this error:

ValidationError: Expected a string primitive

It was working fine yesterday but i forgot to save something and i dont know what.

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;

//  mand handler

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

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

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

console.log(slashCommands);

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

//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 mand = args[0];
  if (client.messageCommands.get(mand)) {
    let Command = client.messageCommands.get(mand);
    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 mands for guild ' + guildId))
    .catch(console.error);
  console.log('bot is online!');
  client.user.setStatus('idle');
});
client.login(token);

im sure there's nothing wrong with any of the mand files because it was working fine yesterday. there's 100% an error in this line slashCommands.push(mand.toJSON()); but I cant seem to fix it. The mand.toJSON() console logs just fine but gives an error while trying to push into the list

Share Improve this question edited Aug 10, 2022 at 8:35 vimuth 5,63248 gold badges92 silver badges124 bronze badges asked Aug 10, 2022 at 7:18 Ishaan JainIshaan Jain 631 gold badge1 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

Ran into the same issue, turns out options (i.e. addUserOption) require a description. Point is it's really confusing as this error shows up when doing mand.data.toJSON(). If you are dynamically loading the mand files as described in the guide and running into this issue, then try manually doing a require to trigger the validation beforehand.

Try using mand.data.toJSON() as mand is a nested object with a data and an execute key.

I've fixed it, there was a url in the json which seemed to be causing some issue, I removed the file with the url and its working now

发布评论

评论列表(0)

  1. 暂无评论