javascript - 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 进程。
解决方案
改变
const sql = require("sqlite");
sql.open("./coin.sqlite");
到
const sql = require("sqlite");
const db = sql.open("./coin.sqlite");
然后在脚本的其余部分中使用db
而不是。等_sql
db.get
db.run
推荐阅读
- java - 使用另一个哈希映射 Java 中给出的过滤条件过滤哈希映射列表
- css - 动画延迟前出现的文字
- macos - SwiftUI TextField 光标在 MacOS 中的大小和位置错误
- java - Java getResourceAsStream 正在加载所需文件的先前版本,但不是最近创建的文件
- python - 从字典生成形状
- vba - 工作簿功能
- python - 您如何修补模拟从正在导入到您正在测试的模块的类的导入?
- pandas - 如何通过从另一个数据框中获取它们的索引来获取来自两个数据框的列之间的差异?
- google-maps - 如何关闭 Flutter Google 地图插件中的“找到我”FAB?
- hibernate - Spring boot 2 多租户配置不使用事务