I am trying to pull users and catches from a Sqlite database through Sequelize and display in a message reply a leaderboard ranked by the amount of catches.
My Sequelize Sqlite database looks like this
const Tags = sequelize.define('tags', {
user: {
type: Sequelize.STRING,
unique: true,
},
catches: {
type: Sequelize.INTEGER,
defaultValue: 0,
allowNull: false,
},
I attempted to push the top 10 values in the database into an array and use the leaderboard method suggested by the discord.js guide but it returns an empty message
if (commandName === 'leaderboard') {
let board = Tags.findAll({ limit: 10, attributes: ['user', 'catches']});
lead = []
lead.push(board)
console.log(lead)
await interaction.reply(
codeBlock(
lead.sort((a, b) => b.balance - a.balance)
.filter(user => client.users.cache.has(user.user_id))
.map((user, position) => `(${position + 1}) ${(user.tag)}: ${user.balance}`)
.join("\n"),
),
);
I found a solution to my issue by requesting from the database via SQLite and not Sequelize
const SQLite = require('better-sqlite3')
const sql = SQLite('./database.sqlite')
...
board = []
const top10 = await sql.prepare("SELECT * FROM tags ORDER BY catches DESC LIMIT 10;").all();
top10.map(({ user, catches }) => {
board.push(`${user} ${catches}`)
});
board = board.toString();
board = board.replace(",", "\n")
const embed = new EmbedBuilder()
.setTitle("Leaderboard")
.setColor(0x0099FF)
.addFields({ name: '------------------', value: board});
return interaction.reply({ embeds: [embed] });