【问题标题】:Values being improperly communicated with bot and mysql database值与 bot 和 mysql 数据库的通信不正确
【发布时间】:2019-03-25 08:54:52
【问题描述】:

所以我正在制定一个命令,让您可以花费积分来提升排名。这些点存储在一个名为“点”的表中,在一个数据库中与之相同。每行包含用户的不和谐雪花 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.js roles discord discord.js


    【解决方案1】:

    根据Roles and Permissions,您应该像这样检查权限:

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

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

    【讨论】:

    • 谢谢我能够通过它,所以它成功地识别了角色以及用户是否拥有它。现在它只是不与 mysql 数据库通信。顶部的 if 语句中的代码仍然相同,但会引发此错误pastebin.com/Ew7K3Zu2。与数据库交互的其他命令没有这个问题。
    • 那个错误意味着你还没有定义你的连接con,如果有的话在哪里定义?
    • 上面定义为const mysql = require("mysql"); var con = mysql.createConnection({ host: "localhost", user: "root", password: "1234", database: "points" });,但运行几次后它没有响应并显示查询为空的错误。
    • 所以现在修复了找不到con的错误?
    • con定义成功,能找到,现在只是吐槽查询为空的错误。
    【解决方案2】:

    为了将来参考,在使用.find() 时,你应该使用这个:

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

    例如,当您在服务器中查找角色时,请使用:

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

    当然你可以在函数中传递任何东西,这只是一个例子。

    【讨论】:

      猜你喜欢
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 2012-04-14
      • 2023-03-06
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多