【问题标题】:JavaScript and Json issueJavaScript 和 Json 问题
【发布时间】:2021-05-02 21:18:23
【问题描述】:

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

有谁知道如何在我的 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% 确定该怎么做,但我认为这与模式有关.

【问题讨论】:

    标签: javascript json discord


    【解决方案1】:

    很抱歉回答了一个问题。但是“...如何在我的 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"
          }
        ]
      }
    ]
    

    【讨论】:

    • 您好,感谢您的回复。我很难解释它,因为我知道的不多。所以我对 discord 的 3 个命令是:/duke | /建筑师 | /scientist 正如您从我的 Json 文件中看到的那样,它存储了命令“architect”,但是如果我随后使用 /duke 或 /scientist,那么它只会将文件从架构师更新为公爵或科学家。每次使用该命令时,我都想尝试获取所有数据。
    • 所以我应该说字符串而不是获取数组
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 2011-04-12
    • 1970-01-01
    • 2019-05-11
    • 2015-06-16
    • 2015-06-02
    • 1970-01-01
    相关资源
    最近更新 更多