我正在嘗試通過 Sequelize 從 Sqlite 資料庫中提取用戶和捕獲量,并在訊息回復中顯示按捕獲量排名的排行榜。
我的 Sequelize Sqlite 資料庫看起來像這樣
const Tags = sequelize.define('tags', {
user: {
type: Sequelize.STRING,
unique: true,
},
catches: {
type: Sequelize.INTEGER,
defaultValue: 0,
allowNull: false,
},
我試圖將資料庫中的前 10 個值推入一個陣列并使用 discord.js 指南建議的排行榜方法,但它回傳一條空訊息
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"),
),
);
uj5u.com熱心網友回復:
你忘了等待findAll結果,因為它是異步的:
let board = await Tags.findAll({ limit: 10, attributes: ['user', 'catches']});
uj5u.com熱心網友回復:
我通過 SQLite 而不是 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] });
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/514005.html
上一篇:插入年齡<0時觸發更新年齡
