Some issue with the code that doesn't allow the creation of slash command error code 400

discord.js slash command
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
],
});

const BOT_TOKEN = 'YOUR_BOT_TOKEN';

const commands = [
{
name: 'ping',
description: 'Ping pong!',
options: [
{
name: 'secret',
description: 'Secret code',
type: 'STRING',
required: true,
},
],
},
];

client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);

// Register slash commands globally
const application = await client.application.fetch();
await application.commands.set(commands);
});

client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;

const { commandName, options } = interaction;

if (commandName === 'ping') {
const secretOption = options.getString('secret');

if (secretOption === 'secretcode') {
await interaction.reply('Pong!');
} else {
await interaction.reply('Bong!');
}
}
});

client.login(BOT_TOKEN);
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
],
});

const BOT_TOKEN = 'YOUR_BOT_TOKEN';

const commands = [
{
name: 'ping',
description: 'Ping pong!',
options: [
{
name: 'secret',
description: 'Secret code',
type: 'STRING',
required: true,
},
],
},
];

client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);

// Register slash commands globally
const application = await client.application.fetch();
await application.commands.set(commands);
});

client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;

const { commandName, options } = interaction;

if (commandName === 'ping') {
const secretOption = options.getString('secret');

if (secretOption === 'secretcode') {
await interaction.reply('Pong!');
} else {
await interaction.reply('Bong!');
}
}
});

client.login(BOT_TOKEN);
29 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!
treble/luna
treble/luna17mo ago
Show your error Also, you need to specify the type with the correct enum which is ApplicationCommandOptionType
gamer50082
gamer50082OP17mo ago
? sadly i work on mobile so will a screenshot do?
treble/luna
treble/luna17mo ago
Your 'type' property needs to be an enum Not a string Also dont deploy your commands on every start, use a deploy script
gamer50082
gamer50082OP17mo ago
so number 3 rate limit issues?
d.js docs
d.js docs17mo ago
guide Creating Your Bot: Registering slash commands read more
treble/luna
treble/luna17mo ago
It might get your ratelimited yes I dont know, hence why i say use the enum
gamer50082
gamer50082OP17mo ago
wait so when deployed does it automatically work with other server if deployed once like global
treble/luna
treble/luna17mo ago
If you use the global route, yes
gamer50082
gamer50082OP17mo ago
ah i see do u need logs or no?
treble/luna
treble/luna17mo ago
Not atm because the 400 is likely due to your incorrect type, if it persists, then yes
gamer50082
gamer50082OP17mo ago
yes the error return the type is 0
treble/luna
treble/luna17mo ago
did you use the enum
gamer50082
gamer50082OP17mo ago
yes bruh it works oh wrong script i used the index.js instead of bot.js lol node . becomes to comfortable nope error
treble/luna
treble/luna17mo ago
you should just have to run node deploy-commands.js Show it
gamer50082
gamer50082OP17mo ago
that would require me to take out the deployment code
treble/luna
treble/luna17mo ago
thats what the script is indeed made for to not run at every start
gamer50082
gamer50082OP17mo ago
treble/luna
treble/luna17mo ago
show your updated code And i dont see you using the enum, rather a string
gamer50082
gamer50082OP17mo ago
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
],
});

const BOT_TOKEN = 'Aworkingtoken';

const commands = [
{
name: 'ping',
description: 'Ping pong!',
options: [
{
name: 'secret',
description: 'Secret code',
type: '3',
required: true,
},
],
},
];

client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);

// Register slash commands globally
const application = await client.application.fetch();
await application.commands.set(commands);
});

client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;

const { commandName, options } = interaction;

if (commandName === 'ping') {
const secretOption = options.getString('secret');

if (secretOption === 'secretcode') {
await interaction.reply('Pong!');
} else {
await interaction.reply('Bong!');
}
}
});

client.login(BOT_TOKEN);
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessages,
],
});

const BOT_TOKEN = 'Aworkingtoken';

const commands = [
{
name: 'ping',
description: 'Ping pong!',
options: [
{
name: 'secret',
description: 'Secret code',
type: '3',
required: true,
},
],
},
];

client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);

// Register slash commands globally
const application = await client.application.fetch();
await application.commands.set(commands);
});

client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;

const { commandName, options } = interaction;

if (commandName === 'ping') {
const secretOption = options.getString('secret');

if (secretOption === 'secretcode') {
await interaction.reply('Pong!');
} else {
await interaction.reply('Bong!');
}
}
});

client.login(BOT_TOKEN);
treble/luna
treble/luna17mo ago
yeah use the enum or actual numbers
gamer50082
gamer50082OP17mo ago
i did didn't i?
treble/luna
treble/luna17mo ago
No, thats a string
gamer50082
gamer50082OP17mo ago
oh i see that's wh
treble/luna
treble/luna17mo ago
Just use ApplicationCommandOptionType.String, or just 3 without '', if you can remember the types from memory
gamer50082
gamer50082OP17mo ago
it works 0-0 i read the docs but i didn't know '' was the cause welp it's time for me to come clean i used chatgpt ok it's solved well if the code 99% works it ok
treble/luna
treble/luna17mo ago
that does explain your issue Never ever use chatgpt for djs It uses data from 2021
gamer50082
gamer50082OP17mo ago
i am surprised the flag didn't cause any issues cause it has been
treble/luna
treble/luna17mo ago
Instead learn actual js instead of asking chatgpt
Want results from more Discord servers?
Add your server