I get a weird error when executing deploy-commands.js

error i get:
Error deploying commands:
TypeError: command.data.toJSON is not a function
Started refreshing 12 application (/) commands.
Successfully reloaded 12 application (/) commands.
Error deploying commands:
TypeError: command.data.toJSON is not a function
Started refreshing 12 application (/) commands.
Successfully reloaded 12 application (/) commands.
19 Replies
d.js toolkit
d.js toolkitβ€’15mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - βœ… Marked as resolved by OP
NNKtv28
NNKtv28OPβ€’15mo ago
deploy-commands.js code:
// This file is for registering slash commands to every guild.
// You can run this file with `node deploy-commands.js` in your terminal.

const { REST, Routes } = require("discord.js");
const { clientId, token } = require("./config.json");
const fs = require("node:fs");
const { dir, error } = require("node:console");
const globals = require("./globals.js");

// subfolder handler
const commandFolders = fs.readdirSync("./commands");
const commands = [];
try {
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./commands/${folder}`).filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
if ('data' in command && 'execute' in command)
{
commands.push(command.data.toJSON());
}else{
console.log(`Command ${file} does not have data or execute property.`);
}
}
}
}catch(error)
{
console.log(`Error deploying commands: \n ${error}`)
}

const rest = new REST({ version: "10" }).setToken(token || process.env.BOT_Token);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

const data = await rest.put(Routes.applicationCommands(clientId), {
body: commands,
});

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
globals.sendWebhookError(error);
}
})();
// This file is for registering slash commands to every guild.
// You can run this file with `node deploy-commands.js` in your terminal.

const { REST, Routes } = require("discord.js");
const { clientId, token } = require("./config.json");
const fs = require("node:fs");
const { dir, error } = require("node:console");
const globals = require("./globals.js");

// subfolder handler
const commandFolders = fs.readdirSync("./commands");
const commands = [];
try {
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./commands/${folder}`).filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
if ('data' in command && 'execute' in command)
{
commands.push(command.data.toJSON());
}else{
console.log(`Command ${file} does not have data or execute property.`);
}
}
}
}catch(error)
{
console.log(`Error deploying commands: \n ${error}`)
}

const rest = new REST({ version: "10" }).setToken(token || process.env.BOT_Token);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

const data = await rest.put(Routes.applicationCommands(clientId), {
body: commands,
});

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
globals.sendWebhookError(error);
}
})();
i have more than 20 commands but it only loads 12 for some reason for example there is this command called set-welcome-channel.js:
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
const Guild = require('../../models/guild');

/*module.exports = {
data: new SlashCommandBuilder()
.setName("set-welcome-channel")
.setDescription("Sets the welcome channel.")
.addChannelOption(option => option
.setName('channel')
.setDescription('The channel to set as the welcome channel')
),
*/
module.exports = {
data: new SlashCommandBuilder()
.setName("set-welcome-channel")
.setDescription("Sets the welcome channel.")
.addChannelOption(option => option
.setName('channel')
.setDescription('The channel to set as the welcome channel')
),

execute(interaction)
{
const { options, member } = interaction;

if (!member.permissions.has(PermissionFlagsBits.Administrator))
{
return interaction.reply('You do not have permission to use this command.');
}

const channel = options.getChannel('channel');
const [ guild, created ] = Guild.findOrCreate({ where: { id: interaction.guild.id } });

if (!channel)
{
return interaction.reply('Please specify a channel.');
}else
{
interaction.reply(`Welcome channel set to ${channel}`);
guild.update({ welcomeChannelID: channel.id });
}
},
};
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
const Guild = require('../../models/guild');

/*module.exports = {
data: new SlashCommandBuilder()
.setName("set-welcome-channel")
.setDescription("Sets the welcome channel.")
.addChannelOption(option => option
.setName('channel')
.setDescription('The channel to set as the welcome channel')
),
*/
module.exports = {
data: new SlashCommandBuilder()
.setName("set-welcome-channel")
.setDescription("Sets the welcome channel.")
.addChannelOption(option => option
.setName('channel')
.setDescription('The channel to set as the welcome channel')
),

execute(interaction)
{
const { options, member } = interaction;

if (!member.permissions.has(PermissionFlagsBits.Administrator))
{
return interaction.reply('You do not have permission to use this command.');
}

const channel = options.getChannel('channel');
const [ guild, created ] = Guild.findOrCreate({ where: { id: interaction.guild.id } });

if (!channel)
{
return interaction.reply('Please specify a channel.');
}else
{
interaction.reply(`Welcome channel set to ${channel}`);
guild.update({ welcomeChannelID: channel.id });
}
},
};
and for some reason it doesnt load it the file system is like this:
root folder:
index.js
globals.js
deploy-commands.js
- Commands folder
- Administration
- Set-welcome-channel.js
- reset-database.js
- Fun
- ...
- ...

root folder:
index.js
globals.js
deploy-commands.js
- Commands folder
- Administration
- Set-welcome-channel.js
- reset-database.js
- Fun
- ...
- ...

πŸŒΊπŸ‡«πŸ‡· Shigu :3 🌺
Check for which files you get the error
NNKtv28
NNKtv28OPβ€’15mo ago
i dont get any error of any file, it just days Error deploying commands: TypeError: command.data.toJSON is not a function and then it procceeds to load 12 commands i already setup as many try catch as i can to see if i get any error but nope, nothing comes up
πŸŒΊπŸ‡«πŸ‡· Shigu :3 🌺
"I don't get any error of any file"
proceeds to show an error
NNKtv28
NNKtv28OPβ€’15mo ago
T_T i know but i mean the error doesnt say where it is
πŸŒΊπŸ‡«πŸ‡· Shigu :3 🌺
Log the file name at each iteration of the loop
NNKtv28
NNKtv28OPβ€’15mo ago
wdym? like make a console.log(file that its currently loading)?
πŸŒΊπŸ‡«πŸ‡· Shigu :3 🌺
Yeah Log the file path you're requiring And the ones with error are the ones you should look into
NNKtv28
NNKtv28OPβ€’15mo ago
Command info.js has data and execute property.
Command settings.js has data and execute property.
Command commands.js has data and execute property.
Command ping.js has data and execute property.
Command urban.js has data and execute property.
Command uptime.js has data and execute property.
Command time.js has data and execute property.
Command 8ball.js has data and execute property.
Command remove-items.js has data and execute property.
Command add-income-role.js has data and execute property.
Command give-item.js has data and execute property.
Command reset-database.js has data and execute property.
Command add-shop-item.js has data and execute property.
Error deploying commands:
TypeError: command.data.toJSON is not a function
Started refreshing 12 application (/) commands.
Successfully reloaded 12 application (/) commands.
Command info.js has data and execute property.
Command settings.js has data and execute property.
Command commands.js has data and execute property.
Command ping.js has data and execute property.
Command urban.js has data and execute property.
Command uptime.js has data and execute property.
Command time.js has data and execute property.
Command 8ball.js has data and execute property.
Command remove-items.js has data and execute property.
Command add-income-role.js has data and execute property.
Command give-item.js has data and execute property.
Command reset-database.js has data and execute property.
Command add-shop-item.js has data and execute property.
Error deploying commands:
TypeError: command.data.toJSON is not a function
Started refreshing 12 application (/) commands.
Successfully reloaded 12 application (/) commands.
i get this now, wich shows that the the set-welcome-channel.js and many others arent loading i use this function to load commands
if ('data' in command && 'execute' in command)
{
console.log(`Command ${file} has data and execute property.`);
commands.push(command.data.toJSON());
}else{
console.log(`Command ${file} does not have data or execute property.`);
}
if ('data' in command && 'execute' in command)
{
console.log(`Command ${file} has data and execute property.`);
commands.push(command.data.toJSON());
}else{
console.log(`Command ${file} does not have data or execute property.`);
}
and it does not give me anything saying the command doesnt have data or execute property
πŸŒΊπŸ‡«πŸ‡· Shigu :3 🌺
Could you use the try/catch within the loop (rather than putting the loop into it) so we can see which element throws the error?
NNKtv28
NNKtv28OPβ€’15mo ago
alr leme do that real quick new code:
// This file is for registering slash commands to every guild.
// You can run this file with `node deploy-commands.js` in your terminal.

const { REST, Routes } = require("discord.js");
const { clientId, token } = require("./config.json");
const fs = require("node:fs");
const { dir, error } = require("node:console");
const globals = require("./globals.js");

// subfolder handler
const commandFolders = fs.readdirSync("./commands");
const commands = [];

for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./commands/${folder}`).filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
try {
const command = require(`./commands/${folder}/${file}`);
if ('data' in command && 'execute' in command)
{
console.log(`Command ${file} has data and execute property.`);
commands.push(command.data.toJSON());
}else{
console.log(`Command ${file} does not have data or execute property.`);
}
} catch (error) {
console.error(`Error in the try in line 17: ${error}`);
}
}
}

const rest = new REST({ version: "10" }).setToken(token || process.env.BOT_Token);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

const data = await rest.put(Routes.applicationCommands(clientId), {
body: commands,
});

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
globals.sendWebhookError(error);
}
})();
// This file is for registering slash commands to every guild.
// You can run this file with `node deploy-commands.js` in your terminal.

const { REST, Routes } = require("discord.js");
const { clientId, token } = require("./config.json");
const fs = require("node:fs");
const { dir, error } = require("node:console");
const globals = require("./globals.js");

// subfolder handler
const commandFolders = fs.readdirSync("./commands");
const commands = [];

for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./commands/${folder}`).filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
try {
const command = require(`./commands/${folder}/${file}`);
if ('data' in command && 'execute' in command)
{
console.log(`Command ${file} has data and execute property.`);
commands.push(command.data.toJSON());
}else{
console.log(`Command ${file} does not have data or execute property.`);
}
} catch (error) {
console.error(`Error in the try in line 17: ${error}`);
}
}
}

const rest = new REST({ version: "10" }).setToken(token || process.env.BOT_Token);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

const data = await rest.put(Routes.applicationCommands(clientId), {
body: commands,
});

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
globals.sendWebhookError(error);
}
})();
output:
Command info.js has data and execute property.
Command settings.js has data and execute property.
Command commands.js has data and execute property.
Command ping.js has data and execute property.
Command urban.js has data and execute property.
Command uptime.js has data and execute property.
Command time.js has data and execute property.
Command 8ball.js has data and execute property.
Command remove-items.js has data and execute property.
Command add-income-role.js has data and execute property.
Command give-item.js has data and execute property.
Command reset-database.js has data and execute property.
Command add-shop-item.js has data and execute property.
Error in the try in line 17: TypeError: command.data.toJSON is not a function
Command give-money.js has data and execute property.
Command newshop.js has data and execute property.
Error in the try in line 17: Error: Invalid string format
Started refreshing 14 application (/) commands.
Successfully reloaded 14 application (/) commands.
Command info.js has data and execute property.
Command settings.js has data and execute property.
Command commands.js has data and execute property.
Command ping.js has data and execute property.
Command urban.js has data and execute property.
Command uptime.js has data and execute property.
Command time.js has data and execute property.
Command 8ball.js has data and execute property.
Command remove-items.js has data and execute property.
Command add-income-role.js has data and execute property.
Command give-item.js has data and execute property.
Command reset-database.js has data and execute property.
Command add-shop-item.js has data and execute property.
Error in the try in line 17: TypeError: command.data.toJSON is not a function
Command give-money.js has data and execute property.
Command newshop.js has data and execute property.
Error in the try in line 17: Error: Invalid string format
Started refreshing 14 application (/) commands.
Successfully reloaded 14 application (/) commands.
now it seems that its loading 2 more commands than before huh
πŸŒΊπŸ‡«πŸ‡· Shigu :3 🌺
What's in add-shop-item.js?
NNKtv28
NNKtv28OPβ€’15mo ago
a stupid attempt at trying to be able to add items to a shop database:
// commands/additem.js

const { CommandInteraction, MessageEmbed } = require('discord.js');
const ShopItem = require('../../schemas/shopItem');

module.exports = {
data: {
name: 'add-shop-item',
description: 'Add an item to the shop',
options: [
{
name: 'name',
type: 'STRING',
description: 'Name of the item',
required: true,
},
{
name: 'price',
type: 'INTEGER',
description: 'Price of the item',
required: true,
},
{
name: 'description',
type: 'STRING',
description: 'Description of the item',
required: true,
},
],
},
async execute(interaction) {
try {
const itemName = interaction.options.getString('name');
const itemPrice = interaction.options.getInteger('price');
const itemDescription = interaction.options.getString('description');

const newItem = new ShopItem({
itemName,
itemPrice,
itemDescription,
});

await newItem.save();

const embed = new MessageEmbed()
.setTitle('Item Added to Shop')
.setColor('#27ae60')
.setDescription(`Item: ${itemName}\nPrice: $${itemPrice}\nDescription: ${itemDescription}`);

await interaction.reply({ embeds: [embed] });
} catch (error) {
console.error(error);
await interaction.reply('An error occurred while adding the item to the shop.');
}
},
};
// commands/additem.js

const { CommandInteraction, MessageEmbed } = require('discord.js');
const ShopItem = require('../../schemas/shopItem');

module.exports = {
data: {
name: 'add-shop-item',
description: 'Add an item to the shop',
options: [
{
name: 'name',
type: 'STRING',
description: 'Name of the item',
required: true,
},
{
name: 'price',
type: 'INTEGER',
description: 'Price of the item',
required: true,
},
{
name: 'description',
type: 'STRING',
description: 'Description of the item',
required: true,
},
],
},
async execute(interaction) {
try {
const itemName = interaction.options.getString('name');
const itemPrice = interaction.options.getInteger('price');
const itemDescription = interaction.options.getString('description');

const newItem = new ShopItem({
itemName,
itemPrice,
itemDescription,
});

await newItem.save();

const embed = new MessageEmbed()
.setTitle('Item Added to Shop')
.setColor('#27ae60')
.setDescription(`Item: ${itemName}\nPrice: $${itemPrice}\nDescription: ${itemDescription}`);

await interaction.reply({ embeds: [embed] });
} catch (error) {
console.error(error);
await interaction.reply('An error occurred while adding the item to the shop.');
}
},
};
i was trying to do it with mongoDB database but since im an idiot i couldnt figure it out and moved to SQLite wich i already used in the past so i kinda know how this one works
πŸŒΊπŸ‡«πŸ‡· Shigu :3 🌺
The error is because you're not using the SlashCommandBuilder
NNKtv28
NNKtv28OPβ€’15mo ago
ohh yeah i just noticed ima fix some commands i get now
Command info.js was executed properly.
Command settings.js was executed properly.
Command commands.js was executed properly.
Command ping.js was executed properly.
Command urban.js was executed properly.
Command uptime.js was executed properly.
Command time.js was executed properly.
Command 8ball.js was executed properly.
Command add-income-role.js was executed properly.
Command reset-database.js was executed properly.
Error while loading setWelcomeChannel.js in the try in line 17: Error: Invalid string format
PS D:\Discord bots\Custom bot order>
Command info.js was executed properly.
Command settings.js was executed properly.
Command commands.js was executed properly.
Command ping.js was executed properly.
Command urban.js was executed properly.
Command uptime.js was executed properly.
Command time.js was executed properly.
Command 8ball.js was executed properly.
Command add-income-role.js was executed properly.
Command reset-database.js was executed properly.
Error while loading setWelcomeChannel.js in the try in line 17: Error: Invalid string format
PS D:\Discord bots\Custom bot order>
cuz i removed a bunch of useless commands
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
const Guild = require('../../models/guild');

module.exports = {
data: new SlashCommandBuilder()
.setName("setWelcomeChannel")
.setDescription("Sets the welcome channel.")
.addChannelOption(option => option
.setName('channel')
.setDescription('The channel to set as the welcome channel')
),

execute(interaction)
{
const { options, member } = interaction;

if (!member.permissions.has(PermissionFlagsBits.Administrator))
{
return interaction.reply('You do not have permission to use this command.');
}

const channel = options.getChannel('channel');
const [ guild, created ] = Guild.findOrCreate({ where: { id: interaction.guild.id } });

if (!channel)
{
return interaction.reply('Please specify a channel.');
}else
{
interaction.reply(`Welcome channel set to ${channel}`);
guild.update({ welcomeChannelID: channel.id });
}
},
};
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
const Guild = require('../../models/guild');

module.exports = {
data: new SlashCommandBuilder()
.setName("setWelcomeChannel")
.setDescription("Sets the welcome channel.")
.addChannelOption(option => option
.setName('channel')
.setDescription('The channel to set as the welcome channel')
),

execute(interaction)
{
const { options, member } = interaction;

if (!member.permissions.has(PermissionFlagsBits.Administrator))
{
return interaction.reply('You do not have permission to use this command.');
}

const channel = options.getChannel('channel');
const [ guild, created ] = Guild.findOrCreate({ where: { id: interaction.guild.id } });

if (!channel)
{
return interaction.reply('Please specify a channel.');
}else
{
interaction.reply(`Welcome channel set to ${channel}`);
guild.update({ welcomeChannelID: channel.id });
}
},
};
it says i got a invalid string format error while loading this file?
πŸŒΊπŸ‡«πŸ‡· Shigu :3 🌺
Command names have to be all lowercase
NNKtv28
NNKtv28OPβ€’15mo ago
oh alr thxx yess it works thxx ShiGu ur the best 😊
Want results from more Discord servers?
Add your server