Need help with subcommands

i cant figure how to get this subcommand right, its the info command and it has the user and the server subcommands
9 Replies
d.js toolkit
d.js toolkit17mo 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!
NNKtv28
NNKtv28OP17mo ago
error:
/home/container/index.js:52
client.commands.set(command.data.name, command);
^
TypeError: Cannot read properties of undefined (reading 'name')
at Object.<anonymous> (/home/container/index.js:52:38)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
at node:internal/main/run_main_module:23:47
Node.js v19.0.0
/home/container/index.js:52
client.commands.set(command.data.name, command);
^
TypeError: Cannot read properties of undefined (reading 'name')
at Object.<anonymous> (/home/container/index.js:52:38)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
at node:internal/main/run_main_module:23:47
Node.js v19.0.0
grass
grass17mo ago
one of your commands doesnt have a data property
NNKtv28
NNKtv28OP17mo ago
Gist
info.js
GitHub Gist: instantly share code, notes, and snippets.
NNKtv28
NNKtv28OP17mo ago
its probably this one
grass
grass17mo ago
remove the try...catch
NNKtv28
NNKtv28OP17mo ago
done, still get this error:
/home/container/index.js:52
client.commands.set(command.data.name, command);
^
TypeError: Cannot read properties of undefined (reading 'name')
at Object.<anonymous> (/home/container/index.js:52:38)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
at node:internal/main/run_main_module:23:47
Node.js v19.0.0
/home/container/index.js:52
client.commands.set(command.data.name, command);
^
TypeError: Cannot read properties of undefined (reading 'name')
at Object.<anonymous> (/home/container/index.js:52:38)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
at node:internal/main/run_main_module:23:47
Node.js v19.0.0
NNKtv28
NNKtv28OP17mo ago
Gist
index.js
GitHub Gist: instantly share code, notes, and snippets.
NNKtv28
NNKtv28OP17mo ago
? why then what should i do? do i leave the command loader as it was in the index.js? and in the info.js i do it .addSubcommand to each one? i dont do anything to the commands in interactionCreate.js
const { Events } = require("discord.js");
const { incr } = require("../globals.js");

module.exports = {
name: Events.InteractionCreate,
once: false,
async execute(interaction) {
if (!interaction.isCommand()) return;

const command = interaction.client.commands.get(interaction.commandName);

// If the command doesn't exist, log it and return.
if (!command) {
console.error(`No command matching ${interaction.commandName} was found. Make sure the file exists.`);
return;
}

if ((await redis.get(`${interaction.user.id}_hasStarted`)) !== "1") {
await redis.set(`${interaction.user.id}_coins`, "0");
await redis.set(`${interaction.user.id}_hp`, "100");
await redis.set(`${interaction.user.id}_max_hp`, "100");
await redis.set(`${interaction.user.id}_armor`, "0");
await redis.set(`${interaction.user.id}_damage`, "5");
await redis.set(`${interaction.user.id}_xp`, "0");
await redis.set(`${interaction.user.id}_xp_needed`, "100");
await redis.set(`${interaction.user.id}_level_xp`, "100");
await redis.set(`${interaction.user.id}_next_level`, 2);
await redis.set(`${interaction.user.id}_level`, "1");
await redis.set(`${interaction.user.id}_hasStarted`, "1");
await redis.set(`${interaction.user.id}_color`, "#FFE302");
await redis.set(`${interaction.user.id}_xp_alerts`, "1");
await redis.set(`${interaction.user.id}_commandsUsed`, "1");

await command.execute(interaction);
return;
}
const { Events } = require("discord.js");
const { incr } = require("../globals.js");

module.exports = {
name: Events.InteractionCreate,
once: false,
async execute(interaction) {
if (!interaction.isCommand()) return;

const command = interaction.client.commands.get(interaction.commandName);

// If the command doesn't exist, log it and return.
if (!command) {
console.error(`No command matching ${interaction.commandName} was found. Make sure the file exists.`);
return;
}

if ((await redis.get(`${interaction.user.id}_hasStarted`)) !== "1") {
await redis.set(`${interaction.user.id}_coins`, "0");
await redis.set(`${interaction.user.id}_hp`, "100");
await redis.set(`${interaction.user.id}_max_hp`, "100");
await redis.set(`${interaction.user.id}_armor`, "0");
await redis.set(`${interaction.user.id}_damage`, "5");
await redis.set(`${interaction.user.id}_xp`, "0");
await redis.set(`${interaction.user.id}_xp_needed`, "100");
await redis.set(`${interaction.user.id}_level_xp`, "100");
await redis.set(`${interaction.user.id}_next_level`, 2);
await redis.set(`${interaction.user.id}_level`, "1");
await redis.set(`${interaction.user.id}_hasStarted`, "1");
await redis.set(`${interaction.user.id}_color`, "#FFE302");
await redis.set(`${interaction.user.id}_xp_alerts`, "1");
await redis.set(`${interaction.user.id}_commandsUsed`, "1");

await command.execute(interaction);
return;
}
// Achievement for April Fools. (1st-3rd April)
// Remember that JavaScript counts months from 0.

if ((await redis.get(`${interaction.user.id}_april_achievement`)) !== null && (await redis.get(`${interaction.user.id}_april_achievement`)) != true) {
const today = new Date();
const start = new Date(Date.UTC(today.getUTCFullYear(), 3, 1)); // April 1st, UTC
const end = new Date(Date.UTC(today.getUTCFullYear(), 3, 3)); // April 3rd, UTC
if (today >= start && today <= end) {
redis.set(`${interaction.user.id}_april_achievement`, true);
redis.incrby(`${interaction.user.id}_coins`, 500);
}
}

try {
await incr(`${interaction.user.id}`, "commandsUsed", 1);
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
// The user will see this if an error occurs. Can be good for reporting bugs.
}
},
};
// Achievement for April Fools. (1st-3rd April)
// Remember that JavaScript counts months from 0.

if ((await redis.get(`${interaction.user.id}_april_achievement`)) !== null && (await redis.get(`${interaction.user.id}_april_achievement`)) != true) {
const today = new Date();
const start = new Date(Date.UTC(today.getUTCFullYear(), 3, 1)); // April 1st, UTC
const end = new Date(Date.UTC(today.getUTCFullYear(), 3, 3)); // April 3rd, UTC
if (today >= start && today <= end) {
redis.set(`${interaction.user.id}_april_achievement`, true);
redis.incrby(`${interaction.user.id}_coins`, 500);
}
}

try {
await incr(`${interaction.user.id}`, "commandsUsed", 1);
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
// The user will see this if an error occurs. Can be good for reporting bugs.
}
},
};
thats the interactionCreate.js i did it and now this is what i should have right?:
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { get, incr } = require("../../globals.js");

module.exports = {
data: new SlashCommandBuilder()
.setName('info')
.setDescription('Get info about a user or a server!')
.addSubcommand(subcommand => subcommand
.setName('user')
.setDescription('Info about a user')
.addUserOption(option => option.setName('target').setDescription('The user')))
.addSubcommand(subcommand => subcommand
.setName('server')
.setDescription('Info about the server')),
async execute(interaction) {
if (interaction.options.getSubcommand() === "user") {
const targetUser = interaction.options.getUser('target') || interaction.user;

const userEmbed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(`User Info - ${targetUser.tag}`)
.addFields(
{
name: 'Username',
value: targetUser.username
},
{
name: "User ID",
value: targetUser.id
},
{
name: "Avatar",
iconURL: targetUser.displayAvatarURL({ dynamic: true })
}
)
.setColor(await get(`${interaction.user.id}_color`))
.setTimestamp();

await interaction.reply({ embeds: [userEmbed] });
} else if (interaction.options.getSubcommand() === "server") {

const serverEmbed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Server Info')
.addFields(
{
name: "Server Name",
value: interaction.guild.name
},
{
name: "Server ID",
value: interaction.guild.id
},
{
name: "Total Members",
value: interaction.guild.memberCount
},

const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { get, incr } = require("../../globals.js");

module.exports = {
data: new SlashCommandBuilder()
.setName('info')
.setDescription('Get info about a user or a server!')
.addSubcommand(subcommand => subcommand
.setName('user')
.setDescription('Info about a user')
.addUserOption(option => option.setName('target').setDescription('The user')))
.addSubcommand(subcommand => subcommand
.setName('server')
.setDescription('Info about the server')),
async execute(interaction) {
if (interaction.options.getSubcommand() === "user") {
const targetUser = interaction.options.getUser('target') || interaction.user;

const userEmbed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(`User Info - ${targetUser.tag}`)
.addFields(
{
name: 'Username',
value: targetUser.username
},
{
name: "User ID",
value: targetUser.id
},
{
name: "Avatar",
iconURL: targetUser.displayAvatarURL({ dynamic: true })
}
)
.setColor(await get(`${interaction.user.id}_color`))
.setTimestamp();

await interaction.reply({ embeds: [userEmbed] });
} else if (interaction.options.getSubcommand() === "server") {

const serverEmbed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Server Info')
.addFields(
{
name: "Server Name",
value: interaction.guild.name
},
{
name: "Server ID",
value: interaction.guild.id
},
{
name: "Total Members",
value: interaction.guild.memberCount
},

i also removed the thing u told me to remove in index.js and now each time i try and use any command i get No command matching inventory was found. Make sure the file exists. this block
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}`);
client.commands.set(command.data.name, command);
if(!command){
client.commands.set(command.data.name, subCommand);
}
console.log("\u001b[1;36mLoaded command " + `'${command.data.name}'` + " from /" + folder + "/" + file + "\u001b[0m");
}
}
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}`);
client.commands.set(command.data.name, command);
if(!command){
client.commands.set(command.data.name, subCommand);
}
console.log("\u001b[1;36mLoaded command " + `'${command.data.name}'` + " from /" + folder + "/" + file + "\u001b[0m");
}
}
by itself it already has a subcommand checker, but each time i start the bot i get:
Loaded command 'newshop' from /Administration/newshop.js
/home/container/index.js:18
client.commands.set(command.data.name, command);
^
TypeError: Cannot read properties of undefined (reading 'name')
at Object.<anonymous> (/home/container/index.js:18:38)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
at node:internal/main/run_main_module:23:47
Node.js v19.0.0
Loaded command 'newshop' from /Administration/newshop.js
/home/container/index.js:18
client.commands.set(command.data.name, command);
^
TypeError: Cannot read properties of undefined (reading 'name')
at Object.<anonymous> (/home/container/index.js:18:38)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
at node:internal/main/run_main_module:23:47
Node.js v19.0.0
thats with the code block added int he index.js now that i reverted all the changes, the bot doesnt work anyway wtf im going to fk put a whole through the screen but i reversed my changes and it still doesnt work wtf i undid everything yes is there anything i can implement so it tells me exactly where it gives the error? remote-items.js
const { SlashCommandBuilder } = require("discord.js");
const { set, get, decr } = require("../../globals.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("remove-item")
.setDescription("Removes an item from a specified user.")
.addStringOption((option) => option.setName("item").setDescription("The item you want to remove.").setRequired(true))
.addUserOption((option) => option.setName("user").setDescription("The user from whom you want to remove the item.").setRequired(true))
.addStringOption((option) => option.setName("all").setDescription("Remove all instances of this item from the user's inventory?").setRequired(true)),

async execute(interaction) {
const item = interaction.options.getString("item").toLowerCase();
const all = interaction.options.getString("all").toLowerCase();
const targetUser = interaction.options.getUser("user");
const user = interaction.user;
const member = interaction.guild.members.cache.get(user.id);

try {
// Check if the member has a role with ADMINISTRATOR permission
if (!member.roles.cache.some((role) => role.permissions.has("ADMINISTRATOR"))) {
return interaction.reply({
content: "You cannot use this command, only users with ADMINISTRATOR role can use this command.",
ephemeral: true,
});
}
const { SlashCommandBuilder } = require("discord.js");
const { set, get, decr } = require("../../globals.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("remove-item")
.setDescription("Removes an item from a specified user.")
.addStringOption((option) => option.setName("item").setDescription("The item you want to remove.").setRequired(true))
.addUserOption((option) => option.setName("user").setDescription("The user from whom you want to remove the item.").setRequired(true))
.addStringOption((option) => option.setName("all").setDescription("Remove all instances of this item from the user's inventory?").setRequired(true)),

async execute(interaction) {
const item = interaction.options.getString("item").toLowerCase();
const all = interaction.options.getString("all").toLowerCase();
const targetUser = interaction.options.getUser("user");
const user = interaction.user;
const member = interaction.guild.members.cache.get(user.id);

try {
// Check if the member has a role with ADMINISTRATOR permission
if (!member.roles.cache.some((role) => role.permissions.has("ADMINISTRATOR"))) {
return interaction.reply({
content: "You cannot use this command, only users with ADMINISTRATOR role can use this command.",
ephemeral: true,
});
}
// Remove all instances of the item from the user's inventory
if (all === "yes") {
await set(`${targetUser.id}`, `${item}`, 0);
return interaction.reply({
content: `Successfully removed all instances of ${item} from ${targetUser}`,
ephemeral: true,
});
}

const amount = interaction.options.getInteger("amount");

// Check if the provided amount is valid
if (!Number.isInteger(amount) || amount <= 0) {
return interaction.reply({
content: "The amount must be a positive integer.",
ephemeral: true,
});
}

// Remove the specified amount of the item from the user's inventory
const success = await decr(`${targetUser.id}`, `${item}`, amount);

if (success) {
return interaction.reply({
content: `Successfully removed ${amount} ${item}(s) from ${targetUser}`,
ephemeral: true,
});
} else {
return interaction.reply({
content: `Failed to remove ${amount} ${item}(s) from ${targetUser}`,
ephemeral: true,
});
}
// Remove all instances of the item from the user's inventory
if (all === "yes") {
await set(`${targetUser.id}`, `${item}`, 0);
return interaction.reply({
content: `Successfully removed all instances of ${item} from ${targetUser}`,
ephemeral: true,
});
}

const amount = interaction.options.getInteger("amount");

// Check if the provided amount is valid
if (!Number.isInteger(amount) || amount <= 0) {
return interaction.reply({
content: "The amount must be a positive integer.",
ephemeral: true,
});
}

// Remove the specified amount of the item from the user's inventory
const success = await decr(`${targetUser.id}`, `${item}`, amount);

if (success) {
return interaction.reply({
content: `Successfully removed ${amount} ${item}(s) from ${targetUser}`,
ephemeral: true,
});
} else {
return interaction.reply({
content: `Failed to remove ${amount} ${item}(s) from ${targetUser}`,
ephemeral: true,
});
}
} catch (error) {
console.log(error);
return interaction.reply({
content: "An error occurred while processing the command.",
ephemeral: true,
});
}
},
};
} catch (error) {
console.log(error);
return interaction.reply({
content: "An error occurred while processing the command.",
ephemeral: true,
});
}
},
};
found the command okay noww it works
Want results from more Discord servers?
Add your server