Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法创建一个可以在多个文件(Node.JS)上使用的变量_Node.js_Discord_Discord.js - Fatal编程技术网

有没有办法创建一个可以在多个文件(Node.JS)上使用的变量

有没有办法创建一个可以在多个文件(Node.JS)上使用的变量,node.js,discord,discord.js,Node.js,Discord,Discord.js,我在一个discord机器人上工作,我有一个重新启动的var,默认为false,如果设置为true,它应该在我的日志中打印另一条消息, 现在有没有办法创建一个全局变量 我有一个bot.js,我在其中调用导出的模块(在本例中的另一个文件中是core.js restart,在restart中,我想将restarted设置为true,然后在bot.on(“断开”)将变量添加到module.exports(在bot.js中),这样就可以了 然后使用访问它(在core.js中) 多亏了djfdev的评论,

我在一个discord机器人上工作,我有一个重新启动的var,默认为false,如果设置为true,它应该在我的日志中打印另一条消息, 现在有没有办法创建一个全局变量


我有一个bot.js,我在其中调用导出的模块(在本例中的另一个文件中是core.js restart,在restart中,我想将restarted设置为true,然后在bot.on(“断开”)将变量添加到module.exports(在bot.js中),这样就可以了

然后使用访问它(在core.js中)


多亏了djfdev的评论, 我正在导出一个函数,该函数更改restard的值 我正在我的core.js中导入这个值

//bot.js
module.exports= {
    changerestarted: function() {
            restarted = true
    }
}

//core.js
const main = require("../../bot.js")
const auth = require("../../auth/auth.json")
module.exports = { 
    restart: function (message, bot) {
        if (message.author.id == 141218912934166528 || message.author.id == 533665091468656650) {
            console.log(message.author.tag + ' restarted The bot')
            message.reply('You restarted the bot, wait a few seconds')
            bot.channels.get("593824605144088586").send(message.author.tag + ' restarted the bot')
            bot.channels.get("593824605144088586").send('---------------------------------------------------')
            main.changerestarted()
            bot.channels.get("593824605144088586").send('Restarting...')
                .then(msg => bot.destroy())
                .then(() => bot.login(auth.token));
        }
        else {
            message.reply('I´m sorry,:no_entry_sign: you don´t have the permssion to run this command :no_entry_sign:')
            console.log(message.author.tag + ' tried to use -restart')
        }
}


试过了,遗憾的是core.js=()bot.js不起作用(刚刚看到编辑,但是我可以编辑导出的变量吗?),因为我需要在bot.js中检查restarted的值并在core中更改它,jsa您可能会重新考虑您的设计。例如,将restarted的值保留为bot类的属性,并定义一个可以更改其值的方法。然后,您可以将该类或方法导出到core.jsa然后从那里更改。请阅读并添加部分代码,以澄清您的问题,并使他人易于回答。问题越清楚,下次回答的效果越好
const bot = require("./bot");

restarted = bot.restarted;
//bot.js
module.exports= {
    changerestarted: function() {
            restarted = true
    }
}

//core.js
const main = require("../../bot.js")
const auth = require("../../auth/auth.json")
module.exports = { 
    restart: function (message, bot) {
        if (message.author.id == 141218912934166528 || message.author.id == 533665091468656650) {
            console.log(message.author.tag + ' restarted The bot')
            message.reply('You restarted the bot, wait a few seconds')
            bot.channels.get("593824605144088586").send(message.author.tag + ' restarted the bot')
            bot.channels.get("593824605144088586").send('---------------------------------------------------')
            main.changerestarted()
            bot.channels.get("593824605144088586").send('Restarting...')
                .then(msg => bot.destroy())
                .then(() => bot.login(auth.token));
        }
        else {
            message.reply('I´m sorry,:no_entry_sign: you don´t have the permssion to run this command :no_entry_sign:')
            console.log(message.author.tag + ' tried to use -restart')
        }
}