如何为每个用户存储一个号码
How to store a number for each user
我有一个命令,我想成为每个用户的 "counter":我已经尝试将新的 属性 直接添加到用户对象,但这没有用。这是我当前的代码:
if (command == "f") {
if (!message.author.counter) message.author.counter = 0
message.author.counter++
message.channel.send(message.author + " paid respects.\nC'est le F numéro " + message.author.counter + " de " + message.author + ".")
}
您不能直接将其存储到 User 对象中,但您可以创建一个不同的 object/map 来存储它。
您首先需要在代码中的某处初始化一个空对象:
var counters = {};
然后,每次使用该命令时,您都会存储一个与用户 ID 关联的数字:
if (command == "f") {
if (!counters[message.author.id]) counters[message.author.id] = 0;
counters[message.author.id]++;
message.channel.send(message.author + " paid respects.\nC'est le F numéro " + counters[message.author.id] + " de " + message.author + ".");
}
请注意,每次机器人启动时都会重新构建对象:如果您想长期保留它,您需要将其存储在数据库或 JSON 文件中。
如何保存和加载 JSON 文件?
要将现有文件加载到名为 counters
的变量中,您可以执行以下操作:
// You need to require fs (there's no need to install it though)
const fs = require('fs');
// You can set the name of the file you want to save the counters in
const filePath = './counters.json';
var counters;
try {
// If the file already exists, you can load that into counters
let file = require(filePath);
counters = file;
} catch {
// If you can't load the file it means you should create one
counters = {};
fs.writeFileSync(path, '{}');
}
每次你想增加一个计数器时,你可以像我之前写的那样做:你将 counters
对象中的数字 属性 增加一个。
为了保存您需要使用的文件:
fs.writeFileSync(filePath, JSON.stringify(counters));
由您决定何时 运行 它:您可以定期 运行 它,或者,如果您想确保内容立即得到保存,您可以将它添加到您的命令:
if (command == "f") {
if (!counters[message.author.id]) counters[message.author.id] = 0;
counters[message.author.id]++;
message.channel.send(message.author + " paid respects.\nC'est le F numéro " + counters[message.author.id] + " de " + message.author + ".");
fs.writeFileSync(filePath, JSON.stringify(counters));
}
我有一个命令,我想成为每个用户的 "counter":我已经尝试将新的 属性 直接添加到用户对象,但这没有用。这是我当前的代码:
if (command == "f") {
if (!message.author.counter) message.author.counter = 0
message.author.counter++
message.channel.send(message.author + " paid respects.\nC'est le F numéro " + message.author.counter + " de " + message.author + ".")
}
您不能直接将其存储到 User 对象中,但您可以创建一个不同的 object/map 来存储它。
您首先需要在代码中的某处初始化一个空对象:
var counters = {};
然后,每次使用该命令时,您都会存储一个与用户 ID 关联的数字:
if (command == "f") {
if (!counters[message.author.id]) counters[message.author.id] = 0;
counters[message.author.id]++;
message.channel.send(message.author + " paid respects.\nC'est le F numéro " + counters[message.author.id] + " de " + message.author + ".");
}
请注意,每次机器人启动时都会重新构建对象:如果您想长期保留它,您需要将其存储在数据库或 JSON 文件中。
如何保存和加载 JSON 文件?
要将现有文件加载到名为 counters
的变量中,您可以执行以下操作:
// You need to require fs (there's no need to install it though)
const fs = require('fs');
// You can set the name of the file you want to save the counters in
const filePath = './counters.json';
var counters;
try {
// If the file already exists, you can load that into counters
let file = require(filePath);
counters = file;
} catch {
// If you can't load the file it means you should create one
counters = {};
fs.writeFileSync(path, '{}');
}
每次你想增加一个计数器时,你可以像我之前写的那样做:你将 counters
对象中的数字 属性 增加一个。
为了保存您需要使用的文件:
fs.writeFileSync(filePath, JSON.stringify(counters));
由您决定何时 运行 它:您可以定期 运行 它,或者,如果您想确保内容立即得到保存,您可以将它添加到您的命令:
if (command == "f") {
if (!counters[message.author.id]) counters[message.author.id] = 0;
counters[message.author.id]++;
message.channel.send(message.author + " paid respects.\nC'est le F numéro " + counters[message.author.id] + " de " + message.author + ".");
fs.writeFileSync(filePath, JSON.stringify(counters));
}