【问题标题】:Create a ranklist for my discord.js Bot为我的 discord.js 机器人创建一个排名列表
【发布时间】:2018-01-08 03:52:51
【问题描述】:

我有一个名为“scores”的表,想要过滤 UserId 并按他们的分数排序。我想为我的 Discord Bot 创建一个排名列表命令,但我不知道该怎么做。

我虽然是这样但它不起作用:

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);
    });
};

你们能帮帮我吗?

谢谢。

【问题讨论】:

  • 请详细说明“它不起作用”
  • 没有错误。而在控制台中也没什么。
  • 您似乎没有发现错误。尝试将错误处理程序添加到您的 .get 和/或看看您是否至少获得了您的查询。
  • 我收到以下错误“TypeError: Cannot read property 'userId' of undefined” 但我不明白为什么。
  • 看看你在rows 中得到了什么。 undefinednull 在 JavaScript 中是不同的东西,只有在 rows 数组中有明确的 null 值时,您的 null-guard if 语句才有效。您可以简单地检查if(rows[index]) 以保证有一个真实的值。

标签: javascript sqlite discord.js


【解决方案1】:

https://github.com/mapbox/node-sqlite3/wiki/API#databasegetsql-param--callback

回调的签名是function(err, row) {}。如果结果 set 为空,第二个参数未定义,否则为 包含第一行值的对象。

sql.get 仅返回单个行对象(不是 rows 对象的 row 对象数组)。您需要使用 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

【讨论】:

    猜你喜欢
    • 2021-11-19
    • 2021-05-16
    • 2020-07-02
    • 2021-05-29
    • 2020-12-11
    • 2023-04-10
    • 2020-06-30
    • 2020-11-07
    • 1970-01-01
    相关资源
    最近更新 更多