How to add support for private slash commands

Not a big Discord user so lots of common stuff I don't know. But I've made a bot for use just in one channel with a couple of friends. I have slash commands working and I've experimented enough to send and receive DMs. But now I want to add support slash commands in DMs. I can't seem to find the next step via Google, the official docs, Stack Overflow, searching this discord, or asking AI chatbots. Do I have to modify some stuff on my bot's dev page? Pass some more settings in new Client() in my bot code? Or both?
8 Replies
d.js toolkit
d.js toolkit5mo 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
monbrey
monbrey5mo ago
Can you show how you're deploying your slash commands.
hippietrail
hippietrail5mo ago
i modified the deploy-commands.js from a Coding Train video to add support for multiple commands per module:
import { REST, Routes } from 'discord.js';
import { config } from 'dotenv';
import fs from 'node:fs';

config();

const commands = [];
const commandFiles = fs.readdirSync('./commands').filter((file) => file.endsWith('.js'));

for (const file of commandFiles) {
const command = await import(`./commands/${file}`); // Using dynamic import
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());

if ('data2' in command && 'execute2' in command) {
commands.push(command.data2.toJSON());

if ('data3' in command && 'execute3' in command) {
commands.push(command.data3.toJSON());

if ('data4' in command && 'execute4' in command) {
commands.push(command.data4.toJSON());

if ('data5' in command && 'execute5' in command) {
commands.push(command.data5.toJSON());
}
}
}
}
} else {
console.log(`[WARNING] The command ${file} is missing a required "data" or "execute" property.`);
}
}

const rest = new REST().setToken(process.env.TOKEN);

(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(process.env.CLIENTID, process.env.SERVERID), {
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);
}
})();
import { REST, Routes } from 'discord.js';
import { config } from 'dotenv';
import fs from 'node:fs';

config();

const commands = [];
const commandFiles = fs.readdirSync('./commands').filter((file) => file.endsWith('.js'));

for (const file of commandFiles) {
const command = await import(`./commands/${file}`); // Using dynamic import
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());

if ('data2' in command && 'execute2' in command) {
commands.push(command.data2.toJSON());

if ('data3' in command && 'execute3' in command) {
commands.push(command.data3.toJSON());

if ('data4' in command && 'execute4' in command) {
commands.push(command.data4.toJSON());

if ('data5' in command && 'execute5' in command) {
commands.push(command.data5.toJSON());
}
}
}
}
} else {
console.log(`[WARNING] The command ${file} is missing a required "data" or "execute" property.`);
}
}

const rest = new REST().setToken(process.env.TOKEN);

(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(process.env.CLIENTID, process.env.SERVERID), {
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 only dug in and grokked enough to do my mods
hippietrail
hippietrail5mo ago
The Coding Train
YouTube
I made a Discord Bot that says Choo Choo
Learn to create a Discord bot using Discord.js in Node.js. This video walks through setting up a node project, creating a Discord application, and writing code for authentication, slash commands, and bot interaction. Code: https://thecodingtrain.com/tracks/discord-bots/discord/coding-a-bot 🚀 Watch this video ad-free on Nebula https://nebula.tv/...
GitHub
Discord-Bot-Examples/01-discordjs at main · CodingTrain/Discord-Bot...
Bot Examples for Fall 2023. Contribute to CodingTrain/Discord-Bot-Examples development by creating an account on GitHub.
monbrey
monbrey5mo ago
Yeah so, the line where you have Routes.applicationGuildCommands You'd want to change that to Routes.applicationCommands(client_id) Or your process.env.CLIENTID anyway That will deploy them globally, into DMs You'd also then want to remove them from the guild, so they aren't duplicated
d.js docs
d.js docs5mo ago
guide Creating Your Bot: Registering slash commands read more
monbrey
monbrey5mo ago
Our guide covers it
hippietrail
hippietrail5mo ago
ah ok i just found that independently and it's working. this text made me wonder if it is a bit heavy handed
Guild-based deployment of commands is best suited for development and testing in your own personal server. Once you're satisfied that it's ready, deploy the command globally to publish it to all guilds that your bot is in.
but i guess not as it's basically a private bot for my friends and one server so thanks everyone!
Want results from more Discord servers?
Add your server
More Posts
Selecting the same Slash Command option multiple timesHello. Currently learning the basics of Discord.js, I was wondering if it supported selecting the saDiscord Error Code 10015 when using a collector to handle button eventsHello! I am working on a bot Im on the development team with and I dont know how to fix this issue. How would I send an interaction user a message DM?Currently trying to send a user a DM, I've had a quick scope at the docs and Noticed `createDM`, NoDiscord TTS botHi there I am trying to create a discord bot where it would join a vc that I am in and when I use !tHow to retrieve original command interaction options from select menu interaction?Hi people, I am trying to figure out if it's possible to retrieve CommandInteraction options from a When I interact, whether creating ephemeral messages or editing (editReply) it doesn't workWhen I interact with a dropdown or button, whether to edit or create a new ephemera message, it doesSend audio from one channel to another one in another guildHi, I'm currently making a discord bot about game events utilities, and I had an idea. Basically I wHow do i set a custom status, not a "Playing" one.I noticed that bots can now have a custom status which isn't the good old playing one, how do i do iFormat Embed DescriptionI'm having trouble formatting an embed leaderboard with the embed builder. I have the following forHost a Bot on Raspberry PiHey I need help to host my bot on a raspberry pi, can someone help me ? (my bot run on last version