jordan
DIAdiscord.js - Imagine an app
•Created by jordan on 12/23/2023 in #djs-questions
Subcommands show up as individual commands
Hey! I'm having some trouble with commands and subcommands. I've already implemented the code for /count create and /count set, but whenever the bot is deployed, the slash commands being shown are just /create and /set. Here is the commands code.
Not too sure why it's not picking up its a subcommand, but if anyone has had a similar issue, any help is appreciated!
const { SlashCommandBuilder } = require("discord.js");
const Count = require("../../lib/database/models/count.model.js");
const { connectToDatabase } = require("../../lib/database/index.js");
module.exports = {
data: new SlashCommandBuilder()
.setName("count")
.setDescription("Count Commands")
.addSubcommand((subcommand) =>
subcommand
.setName("create")
.setDescription("Creates a new count")
.addStringOption((option) =>
option.setName("name").setDescription("Count Name").setRequired(true)
)
)
.addSubcommand((subcommand) =>
subcommand
.setName("set")
.setDescription("Set the current count for the sever")
.addStringOption((option) =>
option.setName("id").setDescription("Count ID").setRequired(true)
)
),
async execute(interaction) {
const command = interaction.options.getSubcommand();
switch (command) {
case "create":
try {
await connectToDatabase();
const newCount = await Count.create({
name: interaction.options.getString("name"),
value: 0,
creator: interaction.member.id,
createdAt: Date.now(),
});
await interaction.reply(
`Sucessfully created count with name ${interaction.options.getString(
"name"
)} and ID ${newCount._id}!`
);
console.log(
`New Count Created in DB ${interaction.options.getString("name")}`
);
console.log(JSON.parse(JSON.stringify(newCount)));
} catch (error) {
console.log(error);
await interaction.reply(
`Error creating count in database. Contact @jordan for help.`
);
}
break;
case "set":
try {
await connectToDatabase();
await interaction.reply(
`Setting count to ${interaction.options.getString("ID")}`
);
console.log(
`Sever Count Set to ${interaction.options.getString("ID")} for ${
interaction.guild.name
}`
);
} catch (error) {
console.log(error);
await interaction.reply(
`Error creating count in database. Contact @jordan for help.`
);
}
break;
default:
await interaction.reply(`Invalid Subcommand!`);
break;
}
},
};
const { SlashCommandBuilder } = require("discord.js");
const Count = require("../../lib/database/models/count.model.js");
const { connectToDatabase } = require("../../lib/database/index.js");
module.exports = {
data: new SlashCommandBuilder()
.setName("count")
.setDescription("Count Commands")
.addSubcommand((subcommand) =>
subcommand
.setName("create")
.setDescription("Creates a new count")
.addStringOption((option) =>
option.setName("name").setDescription("Count Name").setRequired(true)
)
)
.addSubcommand((subcommand) =>
subcommand
.setName("set")
.setDescription("Set the current count for the sever")
.addStringOption((option) =>
option.setName("id").setDescription("Count ID").setRequired(true)
)
),
async execute(interaction) {
const command = interaction.options.getSubcommand();
switch (command) {
case "create":
try {
await connectToDatabase();
const newCount = await Count.create({
name: interaction.options.getString("name"),
value: 0,
creator: interaction.member.id,
createdAt: Date.now(),
});
await interaction.reply(
`Sucessfully created count with name ${interaction.options.getString(
"name"
)} and ID ${newCount._id}!`
);
console.log(
`New Count Created in DB ${interaction.options.getString("name")}`
);
console.log(JSON.parse(JSON.stringify(newCount)));
} catch (error) {
console.log(error);
await interaction.reply(
`Error creating count in database. Contact @jordan for help.`
);
}
break;
case "set":
try {
await connectToDatabase();
await interaction.reply(
`Setting count to ${interaction.options.getString("ID")}`
);
console.log(
`Sever Count Set to ${interaction.options.getString("ID")} for ${
interaction.guild.name
}`
);
} catch (error) {
console.log(error);
await interaction.reply(
`Error creating count in database. Contact @jordan for help.`
);
}
break;
default:
await interaction.reply(`Invalid Subcommand!`);
break;
}
},
};
4 replies