discord.js - 如何将 JSON 文件中的数字递减到 0?
问题描述
我有一个 JSON 文件,它为特定工作人员设置了静音,我想将其递减到 0,我该怎么做?
我也想要一个代码来减少所有工作人员的静音
client.on('message', message => {
if(!staffstats[message.author.id]) staffstats[message.author.id] = {
mutes: 0,
bans: 0,
warns: 0,
tickets: 0,
appeals: 0,
vips: 0,
WarnedTimes: 0
}
if(message.content === prefix + "mutes-reset"){
user = message.mentions.users.first();
staffstats[user.id].mutes--;
}
})
解决方案
你很亲近!您可以这样做staffstats[user.id].mutes = staffstats[user.id].mutes - 1;
,但是,您确实要求直到 0,因此在更改值之前进行简单检查就足够了:
if (!staffstats[user.id].mutes <= 0) //if mutes value is NOT lower or equal to 0, do:
staffstats[user.id].mutes = staffstats[user.id].mutes - 1; //reduces current value of mutes by 1
要decrement all staff members mutes
,您需要知道员工是谁,以及他们的 ID。假设您知道这一点,例如,您可以遍历用户 ID 数组。
如果您仅将所有员工的所有值存储在对象 ( {}
) 中,那么您可以Object.keys(staffstats);
为所有键(这些是用户 ID)执行操作,因为它方便地存储在一个可以循环的数组中。
var staffId = ['12345', '23456', '34567']; //this is just an example array
staffId.forEach(id => { //loop through array of staffId, storing value in id variable
//same method as above
if (!staffstats[id].mutes <= 0)
staffstats[id].mutes = staffstats[id].mutes - 1;
};
推荐阅读
- testing - Webdriver.io 中的“BrowserObject”类型上不存在属性“config”
- javascript - 自定义 Reactjs Checkbox 组件需要动态颜色
- php - PHP:json_decode 返回 NULL(Paytm 校验和生成)
- python - 将 python 正则表达式模式转换为 lua
- java - Java 特殊符号日志记录后的“JVM 控制台输出中的无效多字节序列:无效或不完整的多字节或宽字符”
- azure - 在 azure devops 管道中执行 gulp 脚本
- javascript - 实现函数并返回新的列表js
- c++ - 如何解决:else 语句未被调用 c++
- for-loop - 为什么我的 `for-select` 语句在我关闭频道后仍不断收到来自我的频道的信息?
- python - Mongo GridFS 发现投影问题