值与bot和mysql数据库通信不正确

时间:2018-10-20 20:13:26

标签: node.js roles discord discord.js

因此,我正在开发一个命令,该命令使您可以花费积分进行排名。这些点被存储在一个称为“点”的表中,并与数据库相同。每行包含用户的不和谐雪花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"
}

2 个答案:

答案 0 :(得分:0)

根据Roles and Permissions,您应该检查以下权限:

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

}

您正在将字符串传递到.find方法中,引发的错误还告诉您,您应该改为将函数传递到该方法中。但是应该使用.has函数来检查用户角色。

答案 1 :(得分:0)

为将来参考,在使用.find()时,您应该使用以下方法:

<collection>.find(c => c.<property> == 'thing');

例如,当您在服务器中寻找角色时,请使用以下命令:

guild.roles.find(r => r.name == 'roleName');

当然,您可以在函数中传递任何内容,这只是一个示例。