How to export variable in another file on discord.js

1.1k views Asked by At

Im trying to make a shifumi game in discord.js to train my skills.

So I created my command to create a game and design our mate.

const { MessageEmbed, MessageActionRow, MessageButton, Permissions} = require('discord.js');
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('shifumi')
        .setDescription('Play to shifumi with another user')
        .addUserOption(option =>
            option.setName('player')
                .setDescription('The player you want to play with')
                .setRequired(true)),

    async execute(interaction) {
        exports.userMain = interaction.user;
        exports.userPlayer = interaction.options.getUser('player');

        const shifumiEmbed = new MessageEmbed()
            .setColor('#00B9FF')
            .setTitle("Shifumi")
            .setDescription('Indiquez la finalisation de votre exercice en cliquant sur le bouton ci-dessous.');

        const rockButton = new MessageActionRow()
            .addComponents(
                new MessageButton()
                    .setCustomId('rock-button')
                    .setLabel('Pierre\ \ ')
                    .setStyle('PRIMARY')
                    .setEmoji(''),
            );

        const leafButton = new MessageActionRow()
            .addComponents(
                new MessageButton()
                    .setCustomId('leaf-button')
                    .setLabel('Feuille\ \ ')
                    .setStyle('PRIMARY')
                    .setEmoji(''),
            );

        const scissorsButton = new MessageActionRow()
            .addComponents(
                new MessageButton()
                    .setCustomId('scissors-button')
                    .setLabel('Ciseaux')
                    .setStyle('PRIMARY')
                    .setEmoji('✂️'),
            );
            
        interaction.channel.send({ embeds: [shifumiEmbed], components: [rockButton, leafButton, scissorsButton]});
        return interaction.reply({ content: `Game started !`, ephemeral: true });
    },
};

And I need to use these variables :

userMain userPlayer

Inside my rock-button.js file :

const { PermissionOverwrites, Permissions, Collection} = require("discord.js");
const shifumi = require("../../commands/shifumi.js");

module.exports = {
    data: {
        name: `rock-button`
    },
    async execute (interaction) {
        // import userMain and userPlayer from the shifumi.js file
        const { userMain, userPlayer } = shifumi;
        console.log(userMain);

        await interaction.reply({ content:'Information envoyée !', ephemeral: true});
    }  
}

I tried to use :

global.userMain = interaction.user;
global.userPlayer = interaction.options.getUser('player');

The result when I try to log the variable are undefined.

and ```js exports.userMain exports.userPlayer


But seems doesn't work to :/

I think we cant export a module when we already are in module but I can be wrong.

If somebody can help me I would appreciate :)

Ty
1

There are 1 answers

1
Pooyan On BEST ANSWER
// File A
const userMain = interaction.user;
module.exports = { varToExport: userMain };

// File B
const userMain = require('./fileA').varToExport;