node.js - discord.js better-sqlite3 消息返回未定义
问题描述
Soooo 我正在尝试创建一个经济系统,但是,每当我输入消息“eco bal ${args}”(其中 args 是表的名称)时,我会得到“未定义”而不是“$0” “这应该是我需要的平衡。这是代码。
client.on("message", message => {
if(message.author.bot) return;
const args = message.content.slice(config.prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
if (command === "testbal") {
const table = sql.prepare(`SELECT count(*) FROM sqlite_master WHERE type='table' AND name = '${args}';`).get();
if (!table['count(*)']) {
// If the table isn't there, create it and setup the database correctly.
sql.prepare(`CREATE TABLE IF NOT EXISTS ${args} (id TEXT PRIMARY KEY, user TEXT, guild TEXT, bal INTEGER);`).run();
// Ensure that the "id" row is always unique and indexed.
sql.prepare(`CREATE UNIQUE INDEX idx_${args}_id ON ${args} (id);`).run();
sql.pragma("synchronous = 1");
sql.pragma("journal_mode = wal");
}
// And then we have two prepared statements to get and set the score data.
client.getScore = sql.prepare(`SELECT * FROM ${args} WHERE user = ? AND guild = ?`);
client.setScore = sql.prepare(`INSERT OR REPLACE INTO ${args} (id, user, guild, bal) VALUES (@id, @user, @guild, @bal);`);
}
});
//Actual code of the thing
client.on("message", message => {
const args = message.content.slice(config.prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
if (command == "bal") {
if (message.author.bot) return;
let score;
if (message.guild) {
score = client.getScore.get(message.author.id, message.content.args);
if (!score) {
score = { id: `${message.guild.id}-${message.author.id}`, user: message.author.id, guild: message.guild.id, args, bal: 0}
}
score.bal++;
}
if (message.content.indexOf(config.prefix) !==0) return;
const data = sql.prepare(`SELECT bal FROM ${args}`);
message.channel.send(`You have ${data.bal}`)
}
});
当我运行命令“eco bal”时,我得到“你有未定义”
解决方案
这有什么区别:
const table = sql.prepare(`SELECT count(*) FROM sqlite_master WHERE type='table' AND name = '${args}';`).get();
和这个:
const data = sql.prepare(`SELECT bal FROM ${args}`);
首先,table
准备并执行 ( get
) sql。第二,data
只准备sql(即创建一个Statement
对象)。
缺少的方法(“获取”,如果你愿意的话)是“你有未定义”结果的直接原因。如果另一个数据元素会给出所需的结果,请务必尝试一下!
这是该对象的API 文档。Statement
推荐阅读
- python - 将默认值添加到有序字典列表
- python - 如何在线性代数方程的数组中写入 (5-x)**2?
- angular - Angular HTTP拦截器错误未刷新视图
- python-3.x - Python(Pyppeteer):[WinError 10054] 远程主机强制关闭现有连接
- ios - 从 CoreData 获取的函数存在问题(使用 .fetchOffset 和 .fetchLimit)
- azure-devops - Azure Devops YAML 管道 - 如何重复任务
- machine-learning - 学习率太大,这如何影响使用批量梯度下降的逻辑回归的损失函数
- json - “int”类型不是“double”类型的子类型——Dart/Flutter 错误
- spring-boot - 如何有效地测试mapstruct spring mapper?
- javascript - 工作箱后台同步 - 访问重放的 API 响应