I'm using the tutorial from discordjs guide (https://discordjs.guide/creating-your-bot/slash-commands.html) and for some reason the slash commands file (my code is basically a copy of their github repo https://github.com/discordjs/guide/tree/main/code-samples/creating-your-bot/command-deployment) execute before index.js. this is an issue because because I define my mongodb client stuff in index.js and call on mongodb in the slash commands files. since the slash commands are running first, lots of the stuff that it's calling is undefined. so tldr why are my slash commands files running before index.js?
#what's calling the slash command files?
14 messages · Page 1 of 1 (latest)
• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.
Can you send your slash commands file? It would be easier to tell what's going on if we could see the specific code
const { EmbedBuilder } = require('discord.js');
mongoclient = global.mongoclient;
const mongoUsers = mongoclient.db("RankedPDT").collection("users");
module.exports = {
data: new SlashCommandBuilder()
.setName('create-profile')
.setDescription('Create your Profile')
.addStringOption(option => option.setName('name').setDescription('Name for profile').setRequired(true)),
async execute(interaction) {
const name = interaction.options.getString('name');
await mongoUsers.insertOne({name: name})
return interaction.reply({ content: "Your profile has been successfully created!", ephemeral: false });
},
};
Line 4 is the one causing the errors
mongoclient is undefined
I defined it in index.js
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { MongoClient } = require('mongodb')
const env = require('dotenv').config()
const URI = process.env.URI
const TOKEN = process.env.TOKEN;
const mongoclient = new MongoClient(URI, { useUnifiedTopology: true });
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
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);
client.commands.set(command.data.name, command);
}
mongoclient.connect(async function (err, mongoclient) {
console.log("Successfully connected to MongoDB!")
client.once(Events.ClientReady, () => {
console.log('Ready!');
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
});
client.login(TOKEN);
})
});
the issue is that the slash commands file is running first so it's calling on undefined objects
and I have no idea why it's executing first/what's calling on that file because I'm just using the code from the tutorial
I could be wrong but I don’t think you can access mongoclient from your command like that. ohh missed the global. Maybe the client doesn’t make it into the global space?
Log stuff to the console to get an idea of what’s executing when. If everything lines up I’d log global to see if it’s what you expect.
Log stuff to the console to get an idea of what’s executing when.
the slash commands file is executing first, which is why it isn't working
You should generally avoid using global in the first place
Personally, I recommend creating and exporting mongoclient in its own file; then, you can import that file into both your index.js (where you login) and your command file (where you access it)