【发布时间】:2021-01-26 12:24:30
【问题描述】:
我正在编写一个不和谐的机器人,并且我正在尝试获取 MySQL,以便我可以保留有关玩家的数据。当我尝试将 MySQL 连接到 discord.js 和 node.js 时,它说:
ER_PARSE_ERROR:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '' 附近使用正确的语法
我不知道这意味着什么,所以如果有人知道如何解决这个问题,谢谢。
这是我的代码:
const Discord = require('discord.js');
const mysql = require('mysql');
const client = new Discord.Client();
const prefix = "p ";
client.once('ready', () => {
console.log('pog is online! Pogchamp')
})
var con = mysql.createConnection({
host: "127.0.0.1",
socketPath: "/Applications/MAMP/tmp/mysql/mysql.sock",
user: "root",
password: "1234",
database: "PogBot"
});
con.connect(function(err) {
if (err)
{
console.log("ERROR: " + err.message);
return;
}
console.log("Connected!");
});
/*shop*/
const shop = new Discord.MessageEmbed()
.setColor('#00FF00')
.setTitle('Shop')
.setDescription('Pog Shop')
.addFields(
{ name: '\u200B', value: '\u200B' },
{ name: 'How to buy', value: 'use "pogs buy (itemName)"to buy an item' },
{ name: 'Laptop :computer:', value: '1000 Coins', inline: true },
{ name: 'Phone :mobile_phone:', value: '400 Coins', inline: true },
{ name: 'Mouse :mouse_three_button:', value: '100 Coins', inline: true },
)
client.on('message', message =>{
if(message.author.bot)
return;
if(message.content.startsWith(prefix + "id"))
message.author.send('your id: ' + message.author.id);
if(message.content.startsWith(prefix + 'me')){
sql = "SELECT * FROM users WHERE user_id = " + message.author.id;
con.query(sql, function (err, result)
{
try
{
if (err)
throw new exception(err.message);
if (result.length > 1)
throw new exception("select user_id=" + message.author.id + " got (result.length) records");
if (result.length == 0)
{
console.log("Not user");
return;
}
message.channel.send("Tag: " + result[0].tag + "\nCoins: " + result[0].pog_coins);
}
catch( exception )
{
// WRITE TO FILE -> ... exception.message
console.log("Something went wrong. Error #" + exception.line);
}
});
}
if(message.content.startsWith(prefix + "create")){
sql = "SELECT * FROM users WHERE user_id = " + message.author.id;
con.query(sql, function (err, result)
{
if (err)
console.log(err.message);
if (result.length > 1)
{
message.channel.send("Account already created:");
message.channel.send("Tag: " + result[0].tag + "\nCoins: " + result[0].pog_coins);
return;
}
sql = "INSERT INTO users(user_id, tag, pog_coins)VALUES(" + message.author.id + ", " + message.author.tag + ", 0)";
con.query(sql, function (err , result)
{
if (err)
console.log(err.message);
message.channel.send("Account Created!");
});
});
}
if(message.content.startsWith(prefix + "shop"))
message.channel.send(shop);
/*if(message.content.startsWith('pog'))
message.channel.send('champ')
if(message.content.startsWith('champ'))
message.channel.send('pog')*/
})
【问题讨论】:
-
您的
message.author.tag中可能包含非字母数字字符。
标签: mysql node.js discord.js