Home > Enterprise >  ValidationError > s.string Expected a string primitive Received: | undefined
ValidationError > s.string Expected a string primitive Received: | undefined

Time:08-13

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;

//  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);

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

CodePudding user response:

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

CodePudding user response:

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

  • Related