-
Notifications
You must be signed in to change notification settings - Fork 2
feat(redis): Comprehensive Redis integration (#177) #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
422f176
6859442
67075f7
a42bfab
713cd4d
a82a08e
1e92e97
c6f09b6
193aaa8
a8d573e
4c68ec5
7f7e798
d0ace9a
6efff59
6aac00f
196c84e
fc6c67d
648527e
3181c26
1f0eae5
60862ce
5c0cbd5
e98ae3a
600b870
e0c1bf4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; | |
| import { getPool } from '../db.js'; | ||
| import { error as logError } from '../logger.js'; | ||
| import { getConfig } from '../modules/config.js'; | ||
| import { getLeaderboardCached } from '../utils/reputationCache.js'; | ||
| import { safeEditReply } from '../utils/safeSend.js'; | ||
|
|
||
| export const data = new SlashCommandBuilder() | ||
|
|
@@ -30,14 +31,17 @@ export async function execute(interaction) { | |
|
|
||
| try { | ||
| const pool = getPool(); | ||
| const { rows } = await pool.query( | ||
| `SELECT user_id, xp, level | ||
| FROM reputation | ||
| WHERE guild_id = $1 | ||
| ORDER BY xp DESC | ||
| LIMIT 10`, | ||
| [interaction.guildId], | ||
| ); | ||
| const rows = await getLeaderboardCached(interaction.guildId, async () => { | ||
| const result = await pool.query( | ||
| `SELECT user_id, xp, level | ||
| FROM reputation | ||
| WHERE guild_id = $1 | ||
| ORDER BY xp DESC | ||
| LIMIT 10`, | ||
| [interaction.guildId], | ||
| ); | ||
| return result.rows; | ||
| }); | ||
|
Comment on lines
+34
to
+44
|
||
|
|
||
| if (rows.length === 0) { | ||
| await safeEditReply(interaction, { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,11 @@ import { error as logError } from '../logger.js'; | |
| import { getConfig } from '../modules/config.js'; | ||
| import { buildProgressBar, computeLevel } from '../modules/reputation.js'; | ||
| import { REPUTATION_DEFAULTS } from '../modules/reputationDefaults.js'; | ||
| import { | ||
| getRankCached, | ||
| getReputationCached, | ||
| setReputationCache, | ||
| } from '../utils/reputationCache.js'; | ||
| import { safeEditReply } from '../utils/safeSend.js'; | ||
|
|
||
| export const data = new SlashCommandBuilder() | ||
|
|
@@ -43,15 +48,23 @@ export async function execute(interaction) { | |
| const repCfg = { ...REPUTATION_DEFAULTS, ...cfg.reputation }; | ||
| const thresholds = repCfg.levelThresholds; | ||
|
|
||
| // Fetch reputation row | ||
| const { rows } = await pool.query( | ||
| 'SELECT xp, level, messages_count FROM reputation WHERE guild_id = $1 AND user_id = $2', | ||
| [interaction.guildId, target.id], | ||
| ); | ||
| // Fetch reputation row (cached) | ||
| const cachedRep = await getReputationCached(interaction.guildId, target.id); | ||
| let repRow = cachedRep; | ||
| if (!repRow) { | ||
| const { rows } = await pool.query( | ||
| 'SELECT xp, level, messages_count FROM reputation WHERE guild_id = $1 AND user_id = $2', | ||
| [interaction.guildId, target.id], | ||
| ); | ||
| repRow = rows[0] ?? null; | ||
| if (repRow) { | ||
| await setReputationCache(interaction.guildId, target.id, repRow); | ||
| } | ||
| } | ||
|
Comment on lines
+51
to
+63
|
||
|
|
||
| const xp = rows[0]?.xp ?? 0; | ||
| const xp = repRow?.xp ?? 0; | ||
| const level = computeLevel(xp, thresholds); | ||
| const messagesCount = rows[0]?.messages_count ?? 0; | ||
| const messagesCount = repRow?.messages_count ?? 0; | ||
|
|
||
| // XP within current level and needed for next | ||
| const currentThreshold = level > 0 ? thresholds[level - 1] : 0; | ||
|
|
@@ -62,14 +75,16 @@ export async function execute(interaction) { | |
| const progressBar = | ||
| nextThreshold !== null ? buildProgressBar(xpInLevel, xpNeeded) : `${'▓'.repeat(10)} MAX`; | ||
|
|
||
| // Rank position in guild | ||
| const rankRow = await pool.query( | ||
| `SELECT COUNT(*) + 1 AS rank | ||
| FROM reputation | ||
| WHERE guild_id = $1 AND xp > $2`, | ||
| [interaction.guildId, xp], | ||
| ); | ||
| const rank = Number(rankRow.rows[0]?.rank ?? 1); | ||
| // Rank position in guild (cached) | ||
| const rank = await getRankCached(interaction.guildId, target.id, async () => { | ||
| const rankRow = await pool.query( | ||
| `SELECT COUNT(*) + 1 AS rank | ||
| FROM reputation | ||
| WHERE guild_id = $1 AND xp > $2`, | ||
| [interaction.guildId, xp], | ||
| ); | ||
| return { rank: Number(rankRow.rows[0]?.rank ?? 1) }; | ||
| }).then((r) => r?.rank ?? 1); | ||
|
Comment on lines
+78
to
+87
|
||
|
|
||
| const levelLabel = `Level ${level}`; | ||
| const xpLabel = nextThreshold !== null ? `${xp} / ${nextThreshold} XP` : `${xp} XP (Max Level)`; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.