Javascript 如何在没有数组的情况下附加到JSON?

Javascript 如何在没有数组的情况下附加到JSON?,javascript,node.js,json,discord,discord.js,Javascript,Node.js,Json,Discord,Discord.js,我正在用Discord.JS编写一个Discord机器人,我对它相当陌生。(今年夏天开始) 我为我和我的朋友们准备了一个无线电机器人,我让它加入频道,阅读JSON并播放音乐 我想让它做的是,我可以通过一个命令添加到stations.json文件中,然后从那里更新所有内容 这是stations.json: { "b101": { "command":"b101", &

我正在用Discord.JS编写一个Discord机器人,我对它相当陌生。(今年夏天开始)

我为我和我的朋友们准备了一个无线电机器人,我让它加入频道,阅读JSON并播放音乐

我想让它做的是,我可以通过一个命令添加到
stations.json
文件中,然后从那里更新所有内容

这是stations.json:

{
    "b101":
        {
            "command":"b101",
            "link":"https://playerservices.streamtheworld.com/api/livestream-redirect/WBEBFMAAC.aac",
            "name":"Philly's B101"
        },
}
这是每个电台的格式 下面是
play.js

module.exports = {
    name: "music!play",
    description: "Find a music stream and play it",
    execute(msg, args, client) {
        function validURL(str) {
        var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
            '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
            '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
            '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
            '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
            '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
        return !!pattern.test(str);
        }
        
        if (args.length === 0)
        return msg.reply('supply a station please');

        if (msg.member.voice.channel) {
            let stationsJson = require('./stations.json');
            const isStation = stationsJson.hasOwnProperty(args[0])
            if (isStation) {
                const station = args[0]
                msg.member.voice.channel.join()
                .then(connection => {
                    const link = stationsJson[station].link
                    const title = stationsJson[station].name
                    msg.reply(`Connected! Playing ${title}`)
                    connection.play(link);
                });
            } else {
              // this is just manual link input here
            }
        } else {
        msg.reply('You are not in a voice channel!');
      }
    }
}
最后,这里是
add.js
。用于添加桩号的命令

const fs = require('fs')
var command = args[0]
var link = args[1]
var name = args.slice(2,20).join(" ")

fs.readFile('./commands/stations.json', function (err, data) {
  if (err) throw err

  var json = JSON.parse(data)
  json.stations.push(
    {
      "${command}": {
        "command": command,
        "link": link,
        "name": name
      }
   });

json = JSON.stringify(json);
fs.writeFile('./commands/stations.json', json, 'utf8', function (err) {
  if (err) throw err
});

这就是我到目前为止所尝试的,但是我必须把它放在一个数组中,当我尝试从
play.js
播放时,它不起作用


谢谢你的帮助

我看到您的
add.js
文件中有一个错误

json文件中没有
json.stations
。如果您有以下内容,它将可用:

// stations.json
{
  stations: [{
    "b101": {
      "command":"b101",
      "link":"https://playerservices.streamtheworld.com/api/livestreamredirect/WBEBFMAAC.aac",
      "name":"Philly's B101"
    }
  }]
}
现在
json.stations
是可能的,它是和数组

如果要向对象添加内容,应使用

json[command] = {
  `${command}`: {
    "command": command,
    "link": link,
    "name": name
  }
}

在add函数中,当您将“${command}”推送到用引号编写的数组中时,如果希望将变量追加到字符串中,则将“${command}”替换为“${command}”`无论如何,这在您的情况下不是必需的,因为您的字符串只包含命令名,所以您可以在发布问题时只编写变量名,请尝试添加更干净的代码。删除代码中所有不必要的部分,以便希望提供帮助的每个人都可以专注于重要部分。例如,您不需要在.json文件中添加所有内容。您可以删除所有验证代码等,因为它们与您的问题无关。好的,谢谢!我不太会问这些问题。我现在就解决它。我如何更新我的
play.js
文件以从JSON文件播放?@zangw在问题35389060中说:“require是同步的,只读取文件一次,下面的调用会从缓存返回结果。”也许你可以尝试用文件系统调用替换require。好的,现在是我的午餐时间,我将尝试所有这些修复方法。谢谢