Javascript 如何再次读取配置文件并更新Discord bot的前缀?

Javascript 如何再次读取配置文件并更新Discord bot的前缀?,javascript,node.js,json,discord.js,fs,Javascript,Node.js,Json,Discord.js,Fs,我在我的Discord机器人中编码前缀更改时遇到一些问题。我已经具备了所有基本功能: 前缀保存在配置文件中 bot可以写入文件并保存以供以后使用 但是,在重新启动bot之前,我似乎无法让bot在更改后使用新前缀。配置文件显示前缀已更改,但bot不响应它 所以,我的问题是,如何刷新内存以便重新加载配置,或者如何让bot再次读取配置并使用新前缀 谢谢 prefix.js: const fs = require('fs'); // node.js file system module config

我在我的Discord机器人中编码前缀更改时遇到一些问题。我已经具备了所有基本功能:

  • 前缀保存在配置文件中
  • bot可以写入文件并保存以供以后使用
但是,在重新启动bot之前,我似乎无法让bot在更改后使用新前缀。配置文件显示前缀已更改,但bot不响应它

所以,我的问题是,如何刷新内存以便重新加载配置,或者如何让bot再次读取配置并使用新前缀

谢谢

prefix.js

const fs = require('fs'); // node.js file system module
config = require('../config.json');
const Discord = require("discord.js");

module.exports = {
    name: 'prefix', // command keyword
    description: 'Changes the bot prefix', // info about command
    group: 'settings', // command group (not displayed in !help [command name])
    aliases: ['botprefix', 'newprefix'], // using these keywords also triggers command
    usage: '[new prefix]', // how command is supposed to be used
    cooldown: '1', // time command cannot be reused after it has been called
    args: true, // are arguments required
execute(message, args) {
  fs.exists("../config.json", function (error) {
    if (error) {
      console.log(error);
    }
  })
    ? fs
        .readFile("../config.json", function (error) {
          if (error) {
            console.log(error);
          }
        })
        .toString()
    : config.prefix;

  const newPrefix = args.shift().toString();
  newConfig = {
    token: config.token,
    prefix: newPrefix,
  };

  fs.writeFile("config.json", JSON.stringify(newConfig, null, 2), function (
    error
  ) {
    if (error) {
      console.log(error);
    }
  });
}
{
  "token": "token",
  "prefix": "!"
}

config.json

const fs = require('fs'); // node.js file system module
config = require('../config.json');
const Discord = require("discord.js");

module.exports = {
    name: 'prefix', // command keyword
    description: 'Changes the bot prefix', // info about command
    group: 'settings', // command group (not displayed in !help [command name])
    aliases: ['botprefix', 'newprefix'], // using these keywords also triggers command
    usage: '[new prefix]', // how command is supposed to be used
    cooldown: '1', // time command cannot be reused after it has been called
    args: true, // are arguments required
execute(message, args) {
  fs.exists("../config.json", function (error) {
    if (error) {
      console.log(error);
    }
  })
    ? fs
        .readFile("../config.json", function (error) {
          if (error) {
            console.log(error);
          }
        })
        .toString()
    : config.prefix;

  const newPrefix = args.shift().toString();
  newConfig = {
    token: config.token,
    prefix: newPrefix,
  };

  fs.writeFile("config.json", JSON.stringify(newConfig, null, 2), function (
    error
  ) {
    if (error) {
      console.log(error);
    }
  });
}
{
  "token": "token",
  "prefix": "!"
}

如果您想通过
require
访问数据,则不会在以后进行更新,因为该函数在项目开始时只运行一次,即使您需要它。这篇文章可以给你们更多的背景知识。基本上:始终使用
fs.readFile
,您很好

第一次如何加载配置?使用require还是使用fs?@zer0,Oops。当我提出这个问题时,我一定忘了复制和粘贴文件的那一部分。我对它进行了编辑以包含这些信息。我想我正在使用两种方法来加载配置。但不确定。我看不出你在哪里加载配置。是的,您可以使用
fs.readFile
prefix
命令中获取
config.json
的内容,但结果未使用。您可以删除缓存,如果需要,数据将是新的。不过我个人还是坚持使用fs.readFile。