【问题标题】:Doing an addition from a JSON value which is a number and replace it by the result从作为数字的 JSON 值进行加法并将其替换为结果
【发布时间】:2021-08-04 16:44:38
【问题描述】:

我需要处理一个 JSON 值,它是一个数字。

我试过了,但没有任何结果:

let picture = require('./pictures.json');
        
let embed = new Discord.MessageEmbed()
.setColor('#fdafb5')
.setDescription(picture['pic01'].description)
.addField("❤ Likes:", picture['pic01'].likes)
.setImage(picture['pic01'].image)
.setFooter("Source: " + picture['pic01'].source)
.setTimestamp()

let authorCommandMessage = message.author.id;

message.channel.send(embed).then(message => {
    message.react('❤');

    message.awaitReactions((reaction, user) => user.id === authorCommandMessage && (reaction.emoji.name === '❤'), { max: 1, time: 30000 })
        .then(collected => {
            if(collected.first().emoji.name === '❤'){
                picture['pic01'].likes + 1;
            }
        });
    });
}

(如果需要,这里是 JSON:)

{
    "pic01":
        {
            "likes":0,
        }
}

【问题讨论】:

    标签: javascript json discord.js


    【解决方案1】:

    您需要将计算的值分配回 json。

    改变

    picture['pic01'].likes + 1;
    

    picture['pic01'].likes = picture['pic01'].likes + 1;
    

    甚至更好,

    picture['pic01'].likes += 1;
    

    【讨论】:

    • 我已经尝试过picture['pic01'].likes += 1;,但它也没有给出任何结果,picture['pic01'].likes = picture['pic01'].likes + 1; 也是如此。
    • 您要寻找的结果是什么?你的问题真的不清楚,我不明白你的意图。
    • 我只希望当用户对❤做出反应时,它会在 Pictures.json 中为 pic01 之类的内容添加 1
    • 您是否要更新.addField("❤ Likes:", picture['pic01'].likes) 中的字段?更新 json 值后,您需要手动编辑嵌入
    • 不,我不是要更新字段中的值,而是要更新 JSON 文件中的值。
    【解决方案2】:

    Simon Cheng 的答案是正确的,需要赋值。

    // Assign +1 value to the current property.
    picture['pic01'].likes += 1;
    
    // This will do the same as above, but less elegantly.
    picture['pic01'].likes = picture['pic01'].likes + 1;
    

    这是一个简单的JSFiddle,展示了它的工作原理。

    我注意到您在一个可能的异步例程中执行此任务。请记住,更改likes 属性的副作用只会在此例程完成它需要做的事情后才会发生,并且您必须在完成后读取对象。

    而且,可以肯定的是,如果您尝试更改 JSON 文件本身,则需要添加读/写机制。目前,您正在读取 JSON 文件并在运行时中将其创建为 JS 对象的副本 - 因此,对此副本所做的更改不会影响 JSON 文件。

    【讨论】:

      【解决方案3】:

      我找到了解决问题的方法(感谢 mdeamf 告诉我需要编写文件,以及 Simon Cheng 告诉我如何进行添加)。

      let picture = require('./pictures.json');
      
      let pictures = ['pic01'];
      let random = Math.floor(Math.random() * pictures.length);
              
      let embed = new Discord.MessageEmbed()
      .setColor('#fdafb5')
      
      .setDescription(picture[pictures[random]].description)
      .addField("❤ Likes:", picture[pictures[random]].likes)
      .setImage(picture[pictures[random]].image)
      .setFooter("Source: " + picture[pictures[random]].source)
      .setTimestamp()
      
      let authorCommandMessage = message.author.id;
      
      message.channel.send(embed).then(message => {
          message.react('❤');
      
          message.awaitReactions((reaction, user) => user.id === authorCommandMessage && (reaction.emoji.name === '❤'), { max: 1, time: 30000 })
              .then(collected => {
                  if(collected.first().emoji.name === '❤'){
                      picture[pictures[random]].likes = picture[pictures[random]].likes += 1;
                              
                      fs.writeFileSync('pictures.json', JSON.stringify(picture, null, 2));
              }
          });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-03
        • 2019-01-20
        • 1970-01-01
        • 2015-03-07
        • 2020-08-25
        • 1970-01-01
        • 2022-09-23
        • 1970-01-01
        相关资源
        最近更新 更多