1

I am trying to import consts from the second file into the first file so the embed can use those imports to send the message

File one:

module.exports = {
  data: {
    name: "GT1",
  },
  async execute(interaction, client, message) {

    //const {a1, a2, a3} = require('./src/components/modals/GeneralTicket');
    import {a1, a2, a3} from './src/components/modals/GeneralTicket.js'

   // const a1 = interaction.fields.getTextInputValue("a1");
   // const a2 = interaction.fields.getTextInputValue("a2");
   // const a3 = interaction.fields.getTextInputValue("a3");

    const embed = new EmbedBuilder()
      .setColor(0x0099ff)
      .setTitle("General Support Ticket ")
      .setTimestamp()
      .addFields(
        { name: "IGN:", value: `${a1}` },
        { name: "What is your Ticket related to:", value: `${a2}` },
        { name: "Brief summary:", value: `${a3}` }
      );

      createdChannel.send({
      embeds: [embed],
      ephemeral: true,
    });
    
  },
};

File two:

module.exports = {
    data: {
        name: `GeneralTicket`
    },
    async execute(interaction, client, message) {

        client.on('interactionCreate', (modalSubmit) => {
            if (!modalSubmit.isModalSubmit()) return;
            
            
            const a1 = interaction.fields.getTextInputValue('a1');
            const a2 = interaction.fields.getTextInputValue('a2');
            const a3 = interaction.fields.getTextInputValue('a3');

            const embed = new EmbedBuilder()
            .setColor(0x0099FF)
            .setTitle('General Support Ticket ')
            .setTimestamp()
            .addFields(
                { name: 'IGN:', value: `${a1}` },
                { name: 'What is your Ticket related to:', value: `${a2}`},
                { name: 'Brief summary:', value: `${a3}`});
             
                const row = new ActionRowBuilder()
                .addComponents(
            new ButtonBuilder()
                .setCustomId('GT1')
                .setLabel(`Submit`)
                .setStyle(ButtonStyle.Success)
                .setDisabled(false),
            );
                
            modalSubmit.reply({ embeds: [embed] , ephemeral: true, components: [row], content: "To submit your ticket click `Submit` to cancel click `Dismiss Message`." });
        
            
        });
    },
};

My error with import: SyntaxError: Cannot use import statement outside a module

I have tried both of these methods and still could not get it to work

import {a1, a2, a3} from './src/components/modals/GeneralTicket.js'
const { a1, a2, a3 } = require("./src/components/modals/GeneralTicket");

1 Answer 1

3

That's not how imports work in Javascript. What you're doing is importing variables from a function that hasn't even been called yet. Imports need to be at the top of the file, and the const x = require('x') syntax is correct. You can't do what you want the way you're doing it, but I'm sure there's another way. Unfortunately without knowledge of what you want and more code, I can't help.

Sign up to request clarification or add additional context in comments.

2 Comments

When this is done though it is still not able to use the consts from file 2 in file 1 unless I am missing something
You're not exporting the variables, you're exporting a function that has those variables inside. Aside from them being out of scope, they're not even initialized when you import in the function. You would need to restructure your code, which without an idea of what you're doing and more code, I couldn't help you with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.