command deploys faster than i can fetch info from db

My goofy ahh code:
const { SlashCommandBuilder, EmbedBuilder, AttachmentBuilder } = require('discord.js');
const { MongoClient } = require("mongodb");

const CLIENT = new MongoClient(process.env.MONGO_URI);
const DB = CLIENT.db("resources");
const COLLECTION = DB.collection("item_resources");

let choicesArray = [];
let choicesArray1 = [];

async function fetchChoices() {
try {
await CLIENT.connect();

// Find documents with 'id' field equal to 'emfReader' and project only the 'id' field
const id = COLLECTION.find({}, { projection: { id: 1, name: 1, _id: 0 } });

// Convert the cursor to an array and extract 'id' values
const documentsArray = await id.toArray();
choicesArray = documentsArray.map(document => ({ name: document.name, value: document.id }));

} catch (err) {
console.error(err);
}
}

fetchChoices().catch(console.dir);

fetchChoices().then(() => {
choicesArray1 = choicesArray;

}).catch(console.dir);

const data = new SlashCommandBuilder()
.setName('item1')
.setNameLocalizations({
"en-US": "item1",
"pl": "przedmiot1"
})
.setDescription('Get information about any item')
.setDescriptionLocalizations({
"en-US": "Get information about any item",
"pl": "Wyświetl informacje o wybranym przedmiocie"
})
.addStringOption(option =>
option.setName('item_name')
.setDescription('Name of the item you want to check info about')
.setDescriptionLocalizations({
"en-US": "Name of the item you want to check info about",
"pl": "Podaj nazwę przedmiotu"
})
.addChoices(
...choicesArray1
)
.setRequired(true));

const execute = async (interaction) => {

}

module.exports = {
data,
execute
};
const { SlashCommandBuilder, EmbedBuilder, AttachmentBuilder } = require('discord.js');
const { MongoClient } = require("mongodb");

const CLIENT = new MongoClient(process.env.MONGO_URI);
const DB = CLIENT.db("resources");
const COLLECTION = DB.collection("item_resources");

let choicesArray = [];
let choicesArray1 = [];

async function fetchChoices() {
try {
await CLIENT.connect();

// Find documents with 'id' field equal to 'emfReader' and project only the 'id' field
const id = COLLECTION.find({}, { projection: { id: 1, name: 1, _id: 0 } });

// Convert the cursor to an array and extract 'id' values
const documentsArray = await id.toArray();
choicesArray = documentsArray.map(document => ({ name: document.name, value: document.id }));

} catch (err) {
console.error(err);
}
}

fetchChoices().catch(console.dir);

fetchChoices().then(() => {
choicesArray1 = choicesArray;

}).catch(console.dir);

const data = new SlashCommandBuilder()
.setName('item1')
.setNameLocalizations({
"en-US": "item1",
"pl": "przedmiot1"
})
.setDescription('Get information about any item')
.setDescriptionLocalizations({
"en-US": "Get information about any item",
"pl": "Wyświetl informacje o wybranym przedmiocie"
})
.addStringOption(option =>
option.setName('item_name')
.setDescription('Name of the item you want to check info about')
.setDescriptionLocalizations({
"en-US": "Name of the item you want to check info about",
"pl": "Podaj nazwę przedmiotu"
})
.addChoices(
...choicesArray1
)
.setRequired(true));

const execute = async (interaction) => {

}

module.exports = {
data,
execute
};
4 Replies
Not Kuma anymore
Not Kuma anymoreOP11mo ago
My deploy command js file:
const { REST, Routes } = require('discord.js');
const { guildId } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');
require('dotenv').config();

const commands = [];
// Grab all the command folders from the commands directory you created earlier
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
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.`);
}
}
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken(process.env.TOKEN);

// and deploy your commands!
(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, guildId), // Add a missing colon after "guildId"
{ 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);
}
})();
const { REST, Routes } = require('discord.js');
const { guildId } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');
require('dotenv').config();

const commands = [];
// Grab all the command folders from the commands directory you created earlier
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
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.`);
}
}
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken(process.env.TOKEN);

// and deploy your commands!
(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, guildId), // Add a missing colon after "guildId"
{ 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);
}
})();
d.js toolkit
d.js toolkit11mo 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!
Not Kuma anymore
Not Kuma anymoreOP11mo ago
when im trying to make data and exec async too then it returns me that missing "data" or "execute" from deploy & i've deleted my exec code cuz of symbol limit
monbrey
monbrey11mo ago
Well yeah, module exports arent async They're immediate when you load the file If you want dynamic choices you should be using autocomplete
Want results from more Discord servers?
Add your server