首页 > 解决方案 > discord js 机器人硬币系统| sqlite 错误

问题描述

代码:

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:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。

标签: javascriptnode.jssqlitediscord

解决方案


改变

const sql = require("sqlite");
sql.open("./coin.sqlite");

const sql = require("sqlite");
const db = sql.open("./coin.sqlite");

然后在脚本的其余部分中使用db而不是。等_sqldb.getdb.run


推荐阅读