Sabry
DIdiscord.js - Imagine ❄
•Created by Sabry on 11/23/2024 in #djs-questions
Discord slash command
it shows the command in every servers, so the user app worked, but not in DMs, am I missing something?
4 replies
DIdiscord.js - Imagine ❄
•Created by Sabry on 11/23/2024 in #djs-questions
Discord slash command
I installed the user app
const { Client, GatewayIntentBits, REST, Routes } = require('discord.js');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
dotenv.config();
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const commands = [
{
name: 'hello',
description: 'Replies with Hello, World!',
},
];
const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);
async function connectDatabase() {
try {
await mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('Connected to MongoDB');
} catch (error) {
console.error('Error connecting to MongoDB:', error);
}
}
client.once('ready', async () => {
try {
await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID),
{ body: commands }
);
console.log('Slash commands registered successfully');
} catch (error) {
console.error(error);
}
console.log('Bot is ready');
await connectDatabase();
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;
if (interaction.commandName === 'hello') {
await interaction.reply('Hello, World!');
}
});
client.login(process.env.TOKEN);
const { Client, GatewayIntentBits, REST, Routes } = require('discord.js');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
dotenv.config();
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const commands = [
{
name: 'hello',
description: 'Replies with Hello, World!',
},
];
const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);
async function connectDatabase() {
try {
await mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('Connected to MongoDB');
} catch (error) {
console.error('Error connecting to MongoDB:', error);
}
}
client.once('ready', async () => {
try {
await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID),
{ body: commands }
);
console.log('Slash commands registered successfully');
} catch (error) {
console.error(error);
}
console.log('Bot is ready');
await connectDatabase();
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;
if (interaction.commandName === 'hello') {
await interaction.reply('Hello, World!');
}
});
client.login(process.env.TOKEN);
4 replies