Gilles Heinesch
Gilles Heinesch

Reputation: 2990

Create a ranklist for my discord.js Bot

I have a table named "scores" and want to filter the UserId and sort by their points. I want to create a ranklist command for my Discord Bot, but I don't know how to do this.

I though it is like this but it doesnt work:

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

Can you guys help me please?

Thanks.

Upvotes: 0

Views: 1591

Answers (1)

Astrydax
Astrydax

Reputation: 465

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 only returns a single row object (not a rows array of row objects). You need to use either sql.each or 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

EDIT: it looks like you're trying to make a discord point system bot. There is a really good guide on all of this here. https://anidiotsguide.gitbooks.io/discord-js-bot-guide/coding-guides/storing-data-in-an-sqlite-file.html

Upvotes: 1

Related Questions