node.js - 值与 bot 和 mysql 数据库的通信不正确
问题描述
所以我正在研究一个允许你花费积分来提升排名的命令。这些点存储在一个名为“点”的表中,在一个数据库中与之相同。每行包含用户的不和谐雪花 ID,以及他们有多少分。
运行时的期望例如 !rankup :检查您的级别(现在每个人都是 0,然后从数据库中减去 100 分(如果有的话)并为您分配级别 1 的新角色。
结果:没有减去积分,我收到了这个错误:
(node:10784) DeprecationWarning: Collection#find: pass a function instead
(node:10784) UnhandledPromiseRejectionWarning: Error: Value must be specified.
at Map.find (C:\github\discordBot\node_modules\discord.js\src\util\Collection.js:499:45)
at Object.module.exports.run (C:\github\discordBot\cmds\rankup.js:12:29)
at Client.bot.on (C:\github\discordBot\index.js:77:17)
at Client.emit (events.js:182:13)
at MessageCreateHandler.handle (C:\github\discordBot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (C:\github\discordBot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (C:\github\discordBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (C:\github\discordBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
at WebSocket.onMessage (C:\github\discordBot\node_modules\ws\lib\event-target.js:120:16)
at WebSocket.emit (events.js:182:13)
(node:10784) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10784) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
null OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 0,
serverStatus: 34,
warningCount: 0,
message: '(Rows matched: 1 Changed: 1 Warnings: 0',
protocol41: true,
changedRows: 1 } undefined
下面是整个命令的代码:
const Discord = module.require("discord.js");
module.exports.run = async (bot, message, args) => {
let toPromote = message.author;
if(message.member.roles.find("Level 0")){ //level 0
con.query(`SELECT * FROM points WHERE id = '${toPromote.id}'`, (err, rows) =>{ //get points from database
if(err) throw err;
let sql;
if(rows.length > 1) { //Has points
//previous points found
let points = rows[0].points;
if(points > 100){ //has more than 100 points
sql = `UPDATE points SET points = ${points - 100} WHERE id = '${toPromote.id}'`; //subtract 100 points
toPromote.removeRole(level0); //change their roles
toPromote.addRole(level1); //change their roles
}
}
con.query(sql, console.log);
});
}else if(message.member.roles.find("Level 2")){ //level 1
}else if(message.member.roles.find("Level 3")){ //level 2
}else if(message.member.roles.find("Level 4")){ //level 3
}else if(message.member.roles.find("Level 5")){ //level 4
}else{ //no level role, assign them one(make it if it doesn't exist)
if(!level0) {
try{
role = await message.guild.createRole({
name: "Level 0",
color: "#000000",
permission: []
});
} catch(e) {
console.log(e.stack);
}
}
toPromote.addRole(level0);
}
};
module.exports.help = {
name: "rankup"
}
解决方案
根据角色和权限,您应该检查这样的权限:
if(message.member.roles.has(role.id)) {
}
您正在将一个字符串传递给该.find
方法,引发的错误还告诉您应该将一个函数传递给该方法。但是该.has
功能应该用于检查用户角色。
推荐阅读
- javascript - 将第一行存储为对象[名称],获取对应的行/列值
- google-vr - VR 查看网页和 iOS
- c++ - 如何检查方法是否返回错误或值?
- java - 在 PageFactory (Selenium+Java) 中获取 NPE NullPointerException
- typescript - Typescript - 如何禁止两个解析为相同类型的类型别名互换使用?
- javascript - Javascript动画倒计时
- cplex - 如何设置使用完整版 CPLEX 的密钥?
- django - Django 中的 def __str__(self) 中的条件
- python-3.x - 如果关联 ID 的一部分匹配,则来自多行的平均值
- ddl - Snowflake SQL 编译错误:无法将列从类型 TIMESTAMP_LTZ(9) 更改为 TIMESTAMP_NTZ(9)