首页 > 解决方案 > JavaScript 和 Json 问题

问题描述

[更新] 我现在选择使用数据库并为每个命令创建一个新集合,然后它将收集所有使用该命令的用户。

有谁知道我如何在我的 Json 文件中获取多个数组,我在不和谐中有 3 个斜杠命令,我试图从中收集数据,但是当我使用命令它只是更新 Json 文件中的当前数组时,我想要么获得每个命令的所有用途。这样我就可以将 Json 文件导入到 Mongodb。

我是 JavaScript 新手并使用 Mongodb,所以我还在学习,所以我非常感谢我能在下面获得的所有帮助。我将提供我的代码示例,以便您查看我是否做错了什么。

我想要实现的示例

{
  "id": "281185483717869569",
  "command": "architect",
  "name": "UKzs",
  "description": [
    {
      "value": "569",
      "name": "x"
    },
    {
      "value": "663",
      "name": "y"
    },
{ 
  "id": "281185483717869569",
  "command": "duke",
  "name": "UKzs",
  "description": [
    {
      "value": "123",
      "name": "x"
    },
    {
      "value": "123",
      "name": "y"
    }
  ]
}
  ]
}

JavaScipt 文件

Client.ws.on("INTERACTION_CREATE", async (interaction, msg) => {
  const command = interaction.data.name.toLowerCase();
  const args = interaction.data.options;

  console.log(args);

  const title = {
    id: interaction.member.user.id,
    command: command,
    name: interaction.member.user.username,
    description: args,
  };

  fs.writeFile("title.json", JSON.stringify(title, null, 2), (err) => {
    if (err) {
      console.log(err);
    } else {
      console.log("File has been written");
    }
  });

  if (command == "duke") {
    const description = args.map((opt) => {
      return opt.value;
    });
    const embed = new Discord.MessageEmbed()
      .setTitle(`Would like the duke title!`)
      .setColor("RANDOM")
      .setDescription(`These are my Coordinates \n ${description}`)
      .setTimestamp()
      .setAuthor(interaction.member.user.username);

    Client.api.interactions(interaction.id, interaction.token).callback.post({
      data: {
        type: 4,
        data: await createAPIMessage(interaction, embed),
      },
    });
  }
  if (command == "architect") {
    const description = args.map((opt) => {
      return opt.value;
    });
    const embed = new Discord.MessageEmbed()
      .setTitle(`Would like the architect title!`)
      .setColor("RANDOM")
      .setDescription(`These are my Coordinates \n ${description}`)
      .setTimestamp()
      .setAuthor(interaction.member.user.username);

    Client.api.interactions(interaction.id, interaction.token).callback.post({
      data: {
        type: 4,
        data: await createAPIMessage(interaction, embed),
      },
    });
  }
  if (command == "scientist") {
    const description = args.map((opt) => {
      return opt.value;
    });
    const embed = new Discord.MessageEmbed()
      .setTitle(`Would like the scientist title!`)
      .setColor("RANDOM")
      .setDescription(`These are my Coordinates \n ${description}`)
      .setTimestamp()
      .setAuthor(interaction.member.user.username);

    Client.api.interactions(interaction.id, interaction.token).callback.post({
      data: {
        type: 4,
        data: await createAPIMessage(interaction, embed),
      },
    });
  }
});

json文件

[
  {
    "id": "281185483717869569",
    "command": "architect",
    "name": "UKzs",
    "description": [
      {
        "value": "123",
        "name": "x"
      },
      {
        "value": "123",
        "name": "y"
      }
    ]
  }
]

如您所见,该命令只有一个输入,如果我使用 /duke 之类的另一个命令然后在 discord 上输入我的数据,那么它只会更新上面的 Json 文件。

如果有人能指出我如何将其保存到数据库的正确方向,那就更好了,我将不胜感激,我不是 100% 确定该怎么做,但我认为它与模式有关。

标签: javascriptjsondiscord

解决方案


我很抱歉回答一个问题。但是“...如何在我的 Json 文件中获取多个数组...”是否意味着“如何在 JSON 中嵌套数组?” 您想知道的是如何在另一个 JSON 数组中定义一个 JSON 数组?

如果这就是所寻求的,那就是:

[
  {
    "id": "281185483717869569",
    "command": "architect",
    "name": "UKzs",
    "array-within-array": [1,2,3, ["one", "two", "three"]],
    "description": [
      {
        "value": "123",
        "name": "x"
      },
      {
        "value": "123",
        "name": "y"
      }
    ]
  }
]

推荐阅读