I'm currently building a Discord app that allows me to view a user's status (online, DND, offline) and what songs they are listening to. This works perfectly in my own server:
However, when I switch to DMs, none of my commands (activity, ping, user) show up:
My project directory is structured as follows:
commands/
└── utility/
├── ping.js
├── check_activity.js
├── user.js
server/
├── api.js
├── models/
└── activity.js
deploy-commands.js
index.js
The commands work fine in a server but do not function in DMs, and I can't figure out why.
index.js (excluding MongoDB-related parts)
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits, MessageFlags } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
],
});
client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.warn(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
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);
const errorMessage = 'There was an error while executing this command!';
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: errorMessage, flags: MessageFlags.Ephemeral });
} else {
await interaction.reply({ content: errorMessage, flags: MessageFlags.Ephemeral });
}
}
});
client.login(token);
I have tried:
- Searching online for similar issues, but I couldn't find anything useful.
- Checking if I need different permissions or intents for DMs.
Is this happening because my bot relies on "guild"-related functions, which do not work in DMs? If so, what should I change to support both servers and DMs?
I'm new to JavaScript and Discord app development, so any help would be greatly appreciated! Thanks!
also my deploy-commands.js:
const { REST, Routes } = require('discord.js');
const { clientId, guildId, token } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');
const commands = [];
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
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.`);
}
}
}
const rest = new REST().setToken(token);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(
Routes.applicationCommands(clientId),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
GatewayIntentBits.DirectMessagesand partial:Partials.Channel