#Problems importing commands

19 messages · Page 1 of 1 (latest)

delicate kiln
#

Hello. I put my commands in a separate folder and file and imported them in the main bot file ((an example of one of the commands I have)):

import { SlashCommandBuilder } from '@discordjs/builders';

//create command with slash command builder
const orderCommand = new SlashCommandBuilder()
.setName('order')
.setDescription('order your favorite meals!')
.addStringOption((option) => 
    option
        .setName('food')
        .setDescription("select your favorite food")
        .setRequired(true)
        .setChoices(
            {
                name: 'Cake',
                value: 'cake',
            },
            {
                name: 'Hamburger',
                value: 'hamburger',
            },
            {
                name: 'Pizza',
                value: 'pizza',
            },
        )
)
.addStringOption((option) =>
    option
        .setName('drink')
        .setDescription('select your favorite drink')
        .setRequired(true)
        .setChoices(
            {
                name: "Water",
                value: "water",
            },
            {
                name: "Coca-cola",
                value: "coca-cola",
            },
            {
                name: "Sprite",
                value: "sprite",
            },
        )
);

export default orderCommand.toJSON()

but when running it gives this error:

import { orderCommand } from './commands/ords.js';
         ^^^^^^^^^^^^
SyntaxError: The requested module './commands/ords.js' does not provide an export named 'orderCommand'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:533:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

can you help me to fix this?

twin spokeBOT
#

• 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.

delicate kiln
# delicate kiln Hello. I put my commands in a separate folder and file and imported them in the ...

Here is the main bot's code:

import { Client, GatewayIntentBits, Routes } from 'discord.js';
import { config } from 'dotenv';
import { REST } from '@discordjs/rest';
import { orderCommand } from './commands/ords.js';
import { rolesCommand } from "./commands/roles.js";

config()

//read data from .env file
const TOKEN = process.env.TOKEN;
const CLIENT_ID = process.env.CLIENT_ID;
const GUILD_ID = process.env.GUILD_ID;


const client = new Client({intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
]})

//for slash command
const rest = new REST({version: 10, }).setToken(TOKEN);


//we use ((on)) function to register and listen to event
//in this case we work with ready event
client.on('ready', () => {console.log(`${client.user.username} online...`)});

client.on('interactionCreate', (interaction) => {
    if  (interaction.isChatInputCommand()) {
        const food = interaction.options.get('food').value;
        const drink = interaction.options.get('drink').value;

        //reply wen command send
        interaction.reply({
            content: `you ordered ${food} and ${drink}`,
        });
    }
})

async function main() {
    //our command strucure
    const commands = [orderCommand, rolesCommand];

    try {
        console.log('Started refreshing application (/) commands.');
        await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), {
            body: commands,
        });

        //start bot
        client.login(TOKEN);
    } catch (err) {
        console.log(err)
    }
}

main();
woven vigil
#

Error tells you the exact issue. You didn't export orderCommand from ords.js

delicate kiln
woven vigil
#

That's the default export. You didn't export orderCommand on its own. Either export the actual variable or use the default import in your index file

woven vigil
#

I am aware

delicate kiln
woven vigil
#

If you want to use the default import, don't try destructure the import

#
- import { orderCommand } from './commands/ords.js';
+ import orderCommand from './commands/ords.js';
delicate kiln
woven vigil
#

Do you know how destructuring properties works?

#

The same applies to imports. If you want to access specific properties of an import, you destructure them. If not, you just import the default export

delicate kiln
twin spokeBOT
#

mdn Destructuring assignment
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

woven vigil
#

you can visit that if you wish to learn more about how destructuring variables works