如何让 Discord.js 机器人为每个用户记录变量
How to get Discord.js bot to log variable for every user
我正在创建一个类似于 Pokecord 的 discord 机器人,我想为每个用户记录 1 个或多个变量。我正在使用 Discord.js、JavaScript 和 Node.js。
这可能吗?
这可能有效也可能无效,我没有时间或资源来测试但它的代码是从以前的项目中抢救出来的
var money = new Map();
const mems = client.guilds.get("serverid");
mems.members.forEach(member => () {
money.set(member.user.username, 1);
console.log(money.get(member.user.username));
});
嗯,这不是很容易,为此你必须创建一个数据库,我建议你
Mysql install that and then watch the guide another cool guide here
那么如果你安装并观看了指南
`
const mysql = require('mysql');
const connection = mysql.createConnection({
host : 'myhost',
port : '3306',
user : 'myuser'
password : 'mypassword',
database : 'mydatabase',
charset : 'utf8mb4'
});
这个连接然后你必须在数据库中创建一个值(金钱)并分配你想要的值
bot.on('!money add 1', message => {
//get the database and add a variable with the username and the value of money you want
});
要为每个用户存储特定值或变量,您有三个主要选项。数据库可能是您的最佳选择。
1。声明一个 Map
有关 JavaScript 中地图的更多信息,请参阅 here。
优点
- 简单易行
- 完全包含在您的代码中
缺点
- 如果代码停止
,所有值都将被重置
例子
初始化客户端时,您可以将其属性之一声明为 Map,这样您就可以在代码的其他地方轻松访问它:
client.money = new Map();
正在检索用户的余额:
const ID = message.author.id; // or set it as the mentioned user's ID, etc.
console.log(client.money.get(ID));
设置用户余额:
client.money.set(ID, 5);
添加到用户的余额中:
client.money.set(ID, client.money.get(ID) + 2);
2。 json 文件
优点
- 分开 file/organized
- 即使在代码停止后仍然存在 运行
缺点
- 稍微复杂一些
- 速度,与 DB
相比
例子
money.json
:
{ "userIDhere": 0 }
正在检索用户的余额:
const money = require('./money.json'); // path may vary
const ID = message.author.id; // or set it as the mentioned user's ID, etc.
console.log(money[ID]);
设置用户余额:
money[ID] = 3;
fs.writeFileSync('./money.json', JSON.stringify(money));
添加到用户的余额中:
money[ID] += 8;
fs.writeFileSync('./money.json', JSON.stringify(money));
3。数据库
优点
- 速度
- 即使在代码停止后仍然存在 运行
- 易于访问
缺点
- 更难的设置
您对数据库的选择由您决定,每个数据库的设置和用法各不相同。做一些最适合您的研究。
使用我的 SQL 安装在服务器上的数据,然后添加 phpmyadmin,您只需连接到您的数据库
我正在创建一个类似于 Pokecord 的 discord 机器人,我想为每个用户记录 1 个或多个变量。我正在使用 Discord.js、JavaScript 和 Node.js。 这可能吗?
这可能有效也可能无效,我没有时间或资源来测试但它的代码是从以前的项目中抢救出来的
var money = new Map();
const mems = client.guilds.get("serverid");
mems.members.forEach(member => () {
money.set(member.user.username, 1);
console.log(money.get(member.user.username));
});
嗯,这不是很容易,为此你必须创建一个数据库,我建议你 Mysql install that and then watch the guide another cool guide here 那么如果你安装并观看了指南 `
const mysql = require('mysql');
const connection = mysql.createConnection({
host : 'myhost',
port : '3306',
user : 'myuser'
password : 'mypassword',
database : 'mydatabase',
charset : 'utf8mb4'
});
这个连接然后你必须在数据库中创建一个值(金钱)并分配你想要的值
bot.on('!money add 1', message => {
//get the database and add a variable with the username and the value of money you want
});
要为每个用户存储特定值或变量,您有三个主要选项。数据库可能是您的最佳选择。
1。声明一个 Map
有关 JavaScript 中地图的更多信息,请参阅 here。
优点
- 简单易行
- 完全包含在您的代码中
缺点
- 如果代码停止
例子
初始化客户端时,您可以将其属性之一声明为 Map,这样您就可以在代码的其他地方轻松访问它:
client.money = new Map();
正在检索用户的余额:
const ID = message.author.id; // or set it as the mentioned user's ID, etc.
console.log(client.money.get(ID));
设置用户余额:
client.money.set(ID, 5);
添加到用户的余额中:
client.money.set(ID, client.money.get(ID) + 2);
2。 json 文件
优点
- 分开 file/organized
- 即使在代码停止后仍然存在 运行
缺点
- 稍微复杂一些
- 速度,与 DB
例子
money.json
:
{ "userIDhere": 0 }
正在检索用户的余额:
const money = require('./money.json'); // path may vary
const ID = message.author.id; // or set it as the mentioned user's ID, etc.
console.log(money[ID]);
设置用户余额:
money[ID] = 3;
fs.writeFileSync('./money.json', JSON.stringify(money));
添加到用户的余额中:
money[ID] += 8;
fs.writeFileSync('./money.json', JSON.stringify(money));
3。数据库
优点
- 速度
- 即使在代码停止后仍然存在 运行
- 易于访问
缺点
- 更难的设置
您对数据库的选择由您决定,每个数据库的设置和用法各不相同。做一些最适合您的研究。
使用我的 SQL 安装在服务器上的数据,然后添加 phpmyadmin,您只需连接到您的数据库