我有一个名为“scores”的表,想要过滤 UserId 并按分数排序。我想为我的 Discord 机器人创建一个排名列表命令,但我不知道如何执行此操作。
我虽然是这样的,但它不起作用:
const sql = require("sqlite");
sql.open("score.sqlite");
exports.run = (client, message, args) => {
var index = 1;
var msg = "-- Top 5 list --\n";
sql.get("SELECT * FROM scores ORDER BY points DESC LIMIT 5").then(rows => {
for (index = 1; index < 6; index++) {
if (rows[index] !== null) {
msg += index + ". " + rows[index].userId + " - " + rows[index].points + "\n";
}
}
console.log(msg);
});
};
你们能帮帮我吗?
谢谢。
最佳答案
https://github.com/mapbox/node-sqlite3/wiki/API#databasegetsql-param--callback
The signature of the callback is function(err, row) {}. If the result set is empty, the second parameter is undefined, otherwise it is an object containing the values for the first row.
sql.get 仅返回单个行对象(而不是 rows 对象数组)。您需要使用 sql.each 或 sql.all。
sql.all https://github.com/mapbox/node-sqlite3/wiki/API#databaseallsql-param--callback
sql.each https://github.com/mapbox/node-sqlite3/wiki/API#databaseeachsql-param--callback-complete
编辑:看起来您正在尝试制作一个不和谐点系统机器人。这里有关于所有这些的非常好的指南。 https://anidiotsguide.gitbooks.io/discord-js-bot-guide/coding-guides/storing-data-in-an-sqlite-file.html
关于javascript - 为我的discord.js 机器人创建排名列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45435130/