首页 > 解决方案 > 值与 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"
}

标签: node.jsrolesdiscorddiscord.js

解决方案


根据角色和权限,您应该检查这样的权限:

if(message.member.roles.has(role.id)) {

}

您正在将一个字符串传递给该.find方法,引发的错误还告诉您应该将一个函数传递给该方法。但是该.has功能应该用于检查用户角色。


推荐阅读