Slash Commands

I'm having trouble restricting the slash commands so that they only appear on the servers where the bot is installed. They even appear in users' DMs. Can you please help me? Below is my command registration code:
import 'dotenv/config';

import { REST, Routes, ApplicationCommandOptionType } from "discord.js";

const commandsGlobal = [
{
name: 'invite',
description: 'Receba um link de convite para adicionar o bot a outros servidores',
},
{
name: "wows-usuario",
description: "Exibe informações do usuário",
options: [{
name: "nome",
description: "Digite o nome do usuário",
type: ApplicationCommandOptionType.String,
required: "true"
}]
}
];

const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);

(async () => {
try {
console.log("Registrando comandos globais...");

// Registro dos comandos globais
await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID),
{ body: commandsGlobal } // Aqui está a variável `commandsGlobal`
);

console.log("Comandos globais registrados com sucesso!");
} catch (error) {
console.log(`Há erro(s): ${error}`);
}
})();
import 'dotenv/config';

import { REST, Routes, ApplicationCommandOptionType } from "discord.js";

const commandsGlobal = [
{
name: 'invite',
description: 'Receba um link de convite para adicionar o bot a outros servidores',
},
{
name: "wows-usuario",
description: "Exibe informações do usuário",
options: [{
name: "nome",
description: "Digite o nome do usuário",
type: ApplicationCommandOptionType.String,
required: "true"
}]
}
];

const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);

(async () => {
try {
console.log("Registrando comandos globais...");

// Registro dos comandos globais
await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID),
{ body: commandsGlobal } // Aqui está a variável `commandsGlobal`
);

console.log("Comandos globais registrados com sucesso!");
} catch (error) {
console.log(`Há erro(s): ${error}`);
}
})();
14 Replies
d.js toolkit
d.js toolkit2w 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
souji
souji2w ago
specify the context in the command payload
d.js docs
d.js docs2w ago
:discord: Application Commands - Create Global Application Command POST /applications/\application.id/commands Create a new global command. Returns 201 if a command with the same name does not already exist, or a 200 if it does (in which case the previous command will be overwritten). Both responses include an application command object. read more
souji
souji2w ago
contexts
JAMPERR7
JAMPERR7OP2w ago
Thank you. It worked correctly.
souji
souji2w ago
np, glad it worked
JAMPERR7
JAMPERR7OP2w ago
Another question please. Do you know why the command below does not work when the user executes the slash command in DM?
case "invite":
//const inviteLink = `https://discord.com/oauth2/authorize?client_id=${client.user.id}&scope=bot&permissions=8`;
const inviteLink = `https://discord.com/application-directory/1170039088791965706`;
await interaction.reply(`> Aqui está o link para adicionar o bot ao seu servidor: ${inviteLink}`);
break;
case "invite":
//const inviteLink = `https://discord.com/oauth2/authorize?client_id=${client.user.id}&scope=bot&permissions=8`;
const inviteLink = `https://discord.com/application-directory/1170039088791965706`;
await interaction.reply(`> Aqui está o link para adicionar o bot ao seu servidor: ${inviteLink}`);
break;
souji
souji2w ago
"does not work" surfaces how? does it reach that code?
JAMPERR7
JAMPERR7OP2w ago
When it runs in a server channel it works, but when it runs through DM it doesn't. I'm using a translator and it might be a little difficult to understand.
souji
souji2w ago
does it reach that portion of code? (try putting a log there) if not, there might be some part of your command handler like "in cached guild" guards that prevents execution
JAMPERR7
JAMPERR7OP2w ago
Now I understand your tip. I think it's correct. I will put the beginning of the code.
if (!interaction.isChatInputCommand()) return;

switch(interaction.commandName) {
case "invite":
//const inviteLink = `https://discord.com/oauth2/authorize?client_id=${client.user.id}&scope=bot&permissions=8`;
const inviteLink = `https://discord.com/application-directory/1170039088791965706`;
await interaction.reply(`> Here is the link to add the bot to your server: ${inviteLink}`);
break;
if (!interaction.isChatInputCommand()) return;

switch(interaction.commandName) {
case "invite":
//const inviteLink = `https://discord.com/oauth2/authorize?client_id=${client.user.id}&scope=bot&permissions=8`;
const inviteLink = `https://discord.com/application-directory/1170039088791965706`;
await interaction.reply(`> Here is the link to add the bot to your server: ${inviteLink}`);
break;
souji
souji2w ago
it should be a chat input command still, but it won't be in the context of a guild/cached guild if that's something you expect/enforce in code
d.js docs
d.js docs2w ago
:method: CommandInteraction#inCachedGuild() [email protected] Indicates whether this interaction is received from a cached guild.
JAMPERR7
JAMPERR7OP2w ago
Thanks!

Did you find this page helpful?