slash commands not working

My slash commands arent working. Give me a moment to get the code!
8 Replies
d.js toolkit
d.js toolkit4mo 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!
! MYA
! MYAOP4mo ago
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!'),
async execute(interaction) {
await interaction.reply('Pong!');
},
};
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!'),
async execute(interaction) {
await interaction.reply('Pong!');
},
};
thats warn js
const fs = require("node:fs")
const path = require("node:path")
const {Client , REST, Routes,Collection , Events , IntentsBitField , EmbedBuilder, ButtonBuilder, ActionRowBuilder, ButtonStyle, GatewayIntentBits} = require("discord.js")

const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
})


const roles = [
{
id: "1260676704712785991",
label: "Verify!"

},
];



client.on("messageCreate", (m) => {



if(m.content === "!createVerifyEmbed") {

m.delete()

const verificationEmbed = new EmbedBuilder()
.setTitle("VERIFICATION EMBED")
.addFields(
{ name: "instructions" , value: "PRESS ON THE GREEN BUTTON BELOW TO VERIFY"}
)
.setColor("Green")


const row = new ActionRowBuilder();

roles.forEach((role) => {
row.components.push(
new ButtonBuilder().setCustomId(role.id).setLabel(role.label).setStyle(ButtonStyle.Success)
)
});


m.channel.send({
embeds: [verificationEmbed],
components: [row]
})

}



});

client.on("interactionCreate", async (interaction) => {
if (!interaction.isButton()) return;
await interaction.deferReply({ ephemeral: true });

const role = interaction.guild.roles.cache.get(interaction.customId)
if (!role) {
interaction.editReply({
content: "Role not found. Contact the server owner!"
});
return;
};


const hasRole = interaction.member.roles.cache.has(role.id)

if (hasRole) {
await interaction.editReply({
content: "You are already verified!"
})
}

if (!hasRole) {

await interaction.member.roles.add(role)

await interaction.editReply({
content: "Verified!"
})
}
})




// STARTUP CODE

client.on("ready", () => {
console.log("Bot online!")
});


client.login("MTI3MDg3OTQyNzY5NzUAAAA.GNUso4.IDcLPO17yT5sCKjzHjLW1KHTTPvxHPejfGqRJc")
const fs = require("node:fs")
const path = require("node:path")
const {Client , REST, Routes,Collection , Events , IntentsBitField , EmbedBuilder, ButtonBuilder, ActionRowBuilder, ButtonStyle, GatewayIntentBits} = require("discord.js")

const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
})


const roles = [
{
id: "1260676704712785991",
label: "Verify!"

},
];



client.on("messageCreate", (m) => {



if(m.content === "!createVerifyEmbed") {

m.delete()

const verificationEmbed = new EmbedBuilder()
.setTitle("VERIFICATION EMBED")
.addFields(
{ name: "instructions" , value: "PRESS ON THE GREEN BUTTON BELOW TO VERIFY"}
)
.setColor("Green")


const row = new ActionRowBuilder();

roles.forEach((role) => {
row.components.push(
new ButtonBuilder().setCustomId(role.id).setLabel(role.label).setStyle(ButtonStyle.Success)
)
});


m.channel.send({
embeds: [verificationEmbed],
components: [row]
})

}



});

client.on("interactionCreate", async (interaction) => {
if (!interaction.isButton()) return;
await interaction.deferReply({ ephemeral: true });

const role = interaction.guild.roles.cache.get(interaction.customId)
if (!role) {
interaction.editReply({
content: "Role not found. Contact the server owner!"
});
return;
};


const hasRole = interaction.member.roles.cache.has(role.id)

if (hasRole) {
await interaction.editReply({
content: "You are already verified!"
})
}

if (!hasRole) {

await interaction.member.roles.add(role)

await interaction.editReply({
content: "Verified!"
})
}
})




// STARTUP CODE

client.on("ready", () => {
console.log("Bot online!")
});


client.login("MTI3MDg3OTQyNzY5NzUAAAA.GNUso4.IDcLPO17yT5sCKjzHjLW1KHTTPvxHPejfGqRJc")
thats index.js and heres register commands
const { REST, Routes } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');

const commands = [];
// Grab all the command folders from the commands directory you created earlier
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken("MTI3MDg3OTQyNzY5NAAAAAso4.IDcLPO17yT5sCKjzHjLW1KHTTPvxHPejfGqRJc");

// and deploy your commands!
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands("1270879427697573948", "1260674094110605412"),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
const { REST, Routes } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');

const commands = [];
// Grab all the command folders from the commands directory you created earlier
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken("MTI3MDg3OTQyNzY5NAAAAAso4.IDcLPO17yT5sCKjzHjLW1KHTTPvxHPejfGqRJc");

// and deploy your commands!
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);

// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands("1270879427697573948", "1260674094110605412"),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
I Have 0 clue whatsoever Im extremely new to this slash command stuff
treble/luna
treble/luna4mo ago
You return if your interaction isnt a button So yes, your slash command will never execute Also you just leaked your token
! MYA
! MYAOP4mo ago
nah I changed a few letters also I dont understand the interactionCreate event what do I put in there so it recognises the slash command
d.js docs
d.js docs4mo ago
:method: BaseInteraction#isChatInputCommand() @14.15.3 Indicates whether this interaction is a ChatInputCommandInteraction.
! MYA
! MYAOP4mo ago
Im js really confused on this whole event when I removed the if (!interaction.isButton()) return; Why doesnt it to what I told it to do in async execute? now I got no clue why this isnt working
client.on("interactionCreate", async (interaction) => {


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

if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}


try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}

await interaction.deferReply({ ephemeral: true });

const role = interaction.guild.roles.cache.get(interaction.customId)
if (!role) {
interaction.editReply({
content: "Role not found. Contact the server owner!"
});
return;
};


const hasRole = interaction.member.roles.cache.has(role.id)

if (hasRole) {
await interaction.editReply({
content: "You are already verified!"
})
}

if (!hasRole) {

await interaction.member.roles.add(role)

await interaction.editReply({
content: "Verified!"
})
}
})
client.on("interactionCreate", async (interaction) => {


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

if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}


try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}

await interaction.deferReply({ ephemeral: true });

const role = interaction.guild.roles.cache.get(interaction.customId)
if (!role) {
interaction.editReply({
content: "Role not found. Contact the server owner!"
});
return;
};


const hasRole = interaction.member.roles.cache.has(role.id)

if (hasRole) {
await interaction.editReply({
content: "You are already verified!"
})
}

if (!hasRole) {

await interaction.member.roles.add(role)

await interaction.editReply({
content: "Verified!"
})
}
})
treble/luna
treble/luna4mo ago
Where are you setting your commands in client.commands?
! MYA
! MYAOP4mo ago
um uh no where? Yeah bro im cooked I gotta re write this whole thing :SkullCry_DV:
Want results from more Discord servers?
Add your server