如何在Discord.js上的另一个文件中导出变量

发布于 2025-01-21 22:53:24 字数 1255 浏览 0 评论 0原文

我试图在discord.js中制作shifumi游戏来训练我的技能。

因此,我创建了创建游戏并设计我们的伴侣的命令。

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('
              

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2025-01-28 22:53:24
// File A
const userMain = interaction.user;
module.exports = { varToExport: userMain };

// File B
const userMain = require('./fileA').varToExport;
// File A
const userMain = interaction.user;
module.exports = { varToExport: userMain };

// File B
const userMain = require('./fileA').varToExport;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文