代码:
const botconfig = require("./botconfig.json");
const tokenfile = require("./token.json");
const Discord = require("discord.js");
const bot = new Discord.Client({disableEveryone: true});
const sql = require("sqlite");
sql.open("./coin.sqlite");
bot.commands = new Discord.Collection();
fs.readdir("./commands/", (err, files) => {
if(err) console.log(err);
let jsfile = files.filter(f => f.split(".").pop() === "js");
if(jsfile.length <= 0){
console.log("Не удалось найти команды!.");
return;
}
jsfile.forEach((f, i) =>{
let props = require(`./commands/${f}`);
console.log(`${f} загружен!`);
bot.commands.set(props.help.name, props);
});
});
bot.on("message", async message => {
if (message.author.bot) return;
if (message.channel.type === "dm") return;
let prefix = botconfig.prefix;
if(!message.content.startsWith(prefix)) return;
let messageArray = message.content.split(" ");
let cmd = messageArray[0];
let args = messageArray.slice(1);
let commandfile = bot.commands.get(cmd.slice(prefix.length));
if (commandfile) commandfile.run(bot, message, args);
});
sql.get(`SELECT * FROM coins WHERE userId = [message.author.id]`).then(row => {
if (!row) { // Can't find the row.
sql.run("INSERT INTO coins (userId, coins) VALUES (?, ?)", [message.author.id, 0]);
} else { // Can find the row.
let curAmt = Math.floor(Math.random() * 5) +0.3 (row.coins + curAmt);
if (curAmt > row.coins) {
row.coins = curAmt;
sql.run(`UPDATE coins SET coins = ${row.coins + curAmt}, WHERE userId = [message.author.id]`);
}
sql.run(`UPDATE coins SET coins = ${row.coins + curAmt} WHERE userId = [message.author.id]`);
}
}).catch(() => {
console.error; // Log those errors.
sql.run("CREATE TABLE IF NOT EXISTS coins (userId TEXT, coins INTEGER)").then(() => {
sql.run("INSERT INTO scores (userId, coins) VALUES (?, ?)", [message.author.id, 0]);
});
});
bot.login(tokenfile.token);
我得到那些错误:
错误:
(node:14328) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'run' of null
at Promise (C:\Users\Илья\Desktop\JyxoBot\Jyxo\node_modules\sqlite\main.js:219:19)
at new Promise (<anonymous>)
at Database.run (C:\Users\Илья\Desktop\JyxoBot\Jyxo\node_modules\sqlite\main.js:218:12)
at sql.get.then.catch (C:\Users\Илья\Desktop\JyxoBot\Jyxo\index.js:67:7)
at <anonymous>
at runMicrotasksCallback (internal/process/next_tick.js:121:5)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
at Function.Module.runMain (module.js:695:11)
at startup (bootstrap_node.js:191:16)
(node:14328) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
此错误是由于在没有catch块的情况下抛出异步函数而引起的,或者是由于拒绝了.catch()未处理的诺言而引起的。 (拒绝ID:3) (节点:14328)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。
答案 0 :(得分:0)
更改
const sql = require("sqlite");
sql.open("./coin.sqlite");
收件人
const sql = require("sqlite");
const db = sql.open("./coin.sqlite");
然后在脚本的其余部分中使用db
代替sql
。 db.get
,db.run
等