即使机器人离线或获得更新也能保存用户数据
Saving users data even if the bot go offline or it get an update
晚上好,
我正在开发一个 Discord 机器人,但我有一个逻辑问题。我添加了一些可自定义的变量,但如果机器人离线或获得更新,所有用户添加的数据都将被删除。我应该将它存储在外部文件中,还是数据库中,甚至使用 webhooks。谢谢。
这实际上取决于您要保存的 type/amount 数据。但是如果您要保存用户数据,您可能需要设置一个数据库。尝试查看 MongoDB 或 rethinkDb
我建议将播放器变量存储在 JSON 文件中。它们易于读写,并且几乎不需要额外的代码。
玩家数据示例 json 存储 xp 和角色变量的文件:
{
"407362137430687745": {
"xpp": 2001,
"xppr": 999182,
"currentRole": "rank 4"
},
"389981749448409100": {
"xpp": 112,
"xppr": 148,
"currentRole": "rank 2"
},
"405809293204520980": {
"xpp": 100,
"xppr": 128,
"currentRole": "rank 1"
}
}
它们可以自动列在 discord 用户 ID 下,这样您就可以使用 message.author.id
等将播放器的密钥保存到 json 中。
写入json文件的示例:
const fs = require('fs') //importing file save
var xpPath = 'file path to json here'
var xpRead = fs.readFileSync(xpPath);
var xpFile = JSON.parse(xpRead); //ready for use
var userId = message.author.id //user id here
if (!xpFile[userId]) { //this checks if data for the user has already been created
xpFile[userId] = {xpp: 0, xppr: 0, currentRole: ""}; //if not, create it
fs.writeFileSync(xpPath, JSON.stringify(xpFile, null, 2));
} else {
//as an example, I will give the owner of the id 50 xp and the role "Awesome Role"
var xppVar = Number(xpFile.xpp) + 50 //add 50 to their original xp
var xpprVar = Number(xpFile.xppr)
var roleToGive = "Awesome Role"
xpFile[userId] = {xpp: xppVar, xppr: xpprVar, currentRole: roleToGive};
fs.writeFileSync(xpPath, JSON.stringify(xpFile, null, 2));
console.log(`Changed that player's xp to ${xppVar} and gave him the role ${roleToGive}`)
}
在你的机器人中实现它会非常简单,因为你所要做的就是将 json 文件中的键和代码更改为你想要的,然后创建一个 json文件。
晚上好, 我正在开发一个 Discord 机器人,但我有一个逻辑问题。我添加了一些可自定义的变量,但如果机器人离线或获得更新,所有用户添加的数据都将被删除。我应该将它存储在外部文件中,还是数据库中,甚至使用 webhooks。谢谢。
这实际上取决于您要保存的 type/amount 数据。但是如果您要保存用户数据,您可能需要设置一个数据库。尝试查看 MongoDB 或 rethinkDb
我建议将播放器变量存储在 JSON 文件中。它们易于读写,并且几乎不需要额外的代码。
玩家数据示例 json 存储 xp 和角色变量的文件:
{
"407362137430687745": {
"xpp": 2001,
"xppr": 999182,
"currentRole": "rank 4"
},
"389981749448409100": {
"xpp": 112,
"xppr": 148,
"currentRole": "rank 2"
},
"405809293204520980": {
"xpp": 100,
"xppr": 128,
"currentRole": "rank 1"
}
}
它们可以自动列在 discord 用户 ID 下,这样您就可以使用 message.author.id
等将播放器的密钥保存到 json 中。
写入json文件的示例:
const fs = require('fs') //importing file save
var xpPath = 'file path to json here'
var xpRead = fs.readFileSync(xpPath);
var xpFile = JSON.parse(xpRead); //ready for use
var userId = message.author.id //user id here
if (!xpFile[userId]) { //this checks if data for the user has already been created
xpFile[userId] = {xpp: 0, xppr: 0, currentRole: ""}; //if not, create it
fs.writeFileSync(xpPath, JSON.stringify(xpFile, null, 2));
} else {
//as an example, I will give the owner of the id 50 xp and the role "Awesome Role"
var xppVar = Number(xpFile.xpp) + 50 //add 50 to their original xp
var xpprVar = Number(xpFile.xppr)
var roleToGive = "Awesome Role"
xpFile[userId] = {xpp: xppVar, xppr: xpprVar, currentRole: roleToGive};
fs.writeFileSync(xpPath, JSON.stringify(xpFile, null, 2));
console.log(`Changed that player's xp to ${xppVar} and gave him the role ${roleToGive}`)
}
在你的机器人中实现它会非常简单,因为你所要做的就是将 json 文件中的键和代码更改为你想要的,然后创建一个 json文件。