我的 discord.js xp 机器人中的 ER_PARSE_ERROR 错误

Posted

技术标签:

【中文标题】我的 discord.js xp 机器人中的 ER_PARSE_ERROR 错误【英文标题】:ER_PARSE_ERROR error in my discord.js xp bot 【发布时间】:2020-05-27 21:50:31 【问题描述】:

当我试图为 XP 创建一个 discord.js 机器人时,我刚刚收到一个名为“ER_PARSE_ERROR”的错误。 有人可以帮助我吗?这是我的代码:

    var con = mysql.createConnection(
  host: "127.0.0.1",
  user: "root",
  password: "",
  database: "sadb"
);

function generatedXp() 
  let min = 20;
  let max = 30;

  return Math.floor(Math.random() * (max - min + 1)) + min;


con.connect(err => 
  if(err) throw err;
  console.log("Connected to database!");
);

client.on("message", async message => 
  if(message.author.bot) return;
  if(message.channel.type === "dm") return;

  con.query(`SELECT $ FRM xp WHERE id = '` + message.author.id + `'`, (err, rows) => 
    if(err) throw err;

    let sql;

    if(rows.length < 1) 
      sql = `INSERT INT xp (id, xp) VALUES ('$message.author.id', $generatedXp())`;

     else
      let xp = rows[0].xp;
      sql = `UPDATE xp SET xp = $xp + generatedXp() WHERE id = '$message.author.id'`;
    

    con.query(sql, console.log);
  );

  if(/(?:http?:\/)?discord(?:app.com\/invite|.gg)/gi.test(message.content)) 
    message.delete();
    return;
  

  let messageArray = message.content.split(/\s+/g);
  let command = messageArray[0];
  let args = messageArray.slice(1);

  if(!command.startsWith(prefix)) return;

  let limit = client.ratelimits.get(message.author.id);
  let now = Date.now()
  let timeLimit = 2000;

  if(limit != null) 
    if(limit >= now - timeLimit) 
      message.delete()
      return message.channel.send("Vous venez de vous faire ratelimited. Essayez dans `" + (Math.abs((now - limit) - timeLimit) / 1000).toFixed(2) + "` seconds").then(m => m.delete(2000));

    else
      client.ratelimits.set(message.author.id, now);
    
  else
    client.ratelimits.set(message.author.id, now);
  

  let cmd = client.command.get(command.slice(prefix.length));
  if(cmd) cmd.run(client, message, args, con);
);

我之前遇到的错误:

ER_PARSE_ERROR: Syntax error 'xp WHERE id = '649196303137505298'' at lign 1
    at Query.Sequence._packetToError (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
    at Query.ErrorPacket (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\mysql\lib\protocol\sequences\Query.js:79:18)
    at Protocol._parsePacket (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\mysql\lib\protocol\Protocol.js:291:23)
    at Parser._parsePacket (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\mysql\lib\protocol\Parser.js:433:10)
    at Parser.write (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\mysql\lib\protocol\Parser.js:43:10)
    at Protocol.write (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\mysql\lib\protocol\Protocol.js:38:16)
    at Socket.<anonymous> (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\mysql\lib\Connection.js:88:28)
    at Socket.<anonymous> (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\mysql\lib\Connection.js:526:10)
    at Socket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    --------------------
    at Protocol._enqueue (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Connection.query (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\mysql\lib\Connection.js:198:25)
    at Client.<anonymous> (c:\Users\user\Desktop\BotDiscord\Discord Bot\index.js:45:7)
    at Client.emit (events.js:215:7)
    at MessageCreateHandler.handle (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (c:\Users\user\Desktop\BotDiscord\Discord Bot\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:210:5)

拜托,我真的需要帮助 ;-; ,我试图修复它,但我没有在代码中发现任何语法错误...

(对不起,我的英语真的很糟糕,我是法国人)

【问题讨论】:

试试con.query(`SELECT * FROM xp WHERE id = $message.author.id`, (err, rows) =&gt; if(err) throw err; 【参考方案1】:

使用placeholder values,像这样:

con.query('SELECT $ FRM xp WHERE id = ?', [message.author.id], (err, rows) => 
...
);

通过这样做,您的请求将被转义(实际上不需要带有 id,但您永远不会太小心,mysql 模块会在语法部分做得很好。

【讨论】:

谢谢,但我修复了这个错误,但我得到了另一个错误:/ ER_PARSE_ERROR at 'INT xp (id, xp) VALUES ('649196303137505298', 20)'

以上是关于我的 discord.js xp 机器人中的 ER_PARSE_ERROR 错误的主要内容,如果未能解决你的问题,请参考以下文章

Node.js、discord.js 和 MySQL - ER_NO_SUCH_TABLE:表 'NAME' 不存在

尝试为我的 discord.js 机器人创建排行榜命令,但除第一名之外的所有值都保持为 0

不和谐机器人 |使用 discord.js 和 MongoDB 进行排名(排行榜?)系统

Discord.js v12.0.1 频道未定义

Discord.js 机器人使用数组中的加权随机选择嵌入

使用 Discord.js 使用 Discord Bot 将语音频道中的所有人静音