basic leaderboard, %b command

This commit is contained in:
2024-12-09 15:02:37 -05:00
parent 0889297f2a
commit 3b7f452301
3 changed files with 32 additions and 1 deletions

View File

@@ -3,7 +3,7 @@ use crate::common::{self, Context, Error};
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
/// Tells you what your or someone else's balance is /// Tells you what your or someone else's balance is
#[poise::command(slash_command, prefix_command, aliases("bal"))] #[poise::command(slash_command, prefix_command, aliases("bal", "b"))]
pub async fn balance(ctx: Context<'_>, user: Option<serenity::User>) -> Result<(), Error> { pub async fn balance(ctx: Context<'_>, user: Option<serenity::User>) -> Result<(), Error> {
let user = user.as_ref().unwrap_or(ctx.author()); let user = user.as_ref().unwrap_or(ctx.author());
let db = &ctx.data().database; let db = &ctx.data().database;

View File

@@ -0,0 +1,30 @@
use crate::common::{Context, Error};
use poise::serenity_prelude::UserId;
use sqlx::Row;
/// Display a leaderboard of the top 10 wealthiest players
#[poise::command(slash_command, prefix_command)]
pub async fn leaderboard(ctx: Context<'_>) -> Result<(), Error> {
let db = &ctx.data().database;
let rows = sqlx::query(
r#"
SELECT id, balance FROM bank
ORDER BY balance DESC
LIMIT 10
"#
).fetch_all(db).await?;
let users: Vec<(_, i32)> = rows.iter().map(|row| (UserId::new(row.get::<i64, _>(0) as u64), row.get(1))).collect();
let mut output = String::new();
for (id, balance) in users {
let user = id.to_user(ctx).await?;
output += &format!("{} - {}\n", user.display_name(), balance);
}
ctx.reply(format!("```\n{output}```")).await?;
Ok(())
}

View File

@@ -17,6 +17,7 @@ pub fn commands() -> Vec<Command<Data, Error>> {
gambling::give::give(), gambling::give::give(),
gambling::wager::wager(), gambling::wager::wager(),
gambling::daily::daily(), gambling::daily::daily(),
gambling::leaderboard::leaderboard(),
eval::eval(), eval::eval(),
self_roles::role(), self_roles::role(),
] ]