Skip to content

Commit

Permalink
add manual leaderboard showing command
Browse files Browse the repository at this point in the history
  • Loading branch information
ana-rchy committed Jun 2, 2024
1 parent ab63110 commit 3a8f9a8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
67 changes: 65 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::global::*;
use poise::{serenity_prelude::CreateAttachment, reply::CreateReply};
use poise::serenity_prelude::{ChannelId, Colour, CreateAttachment, CreateEmbed, CreateMessage, UserId};
use poise::reply::CreateReply;
use tokio::fs::File;
use log::debug;

type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, SharedData, Error>;
Expand All @@ -21,7 +23,6 @@ pub async fn fact_check(ctx: Context<'_>) -> Result<(), Error> {
Ok(())
}


async fn get_fact_check_image(shared_data: &SharedData) -> (File, String) {
let root_folder = &shared_data.root_path;

Expand All @@ -37,3 +38,65 @@ async fn get_fact_check_image(shared_data: &SharedData) -> (File, String) {

(File::open(image).await.unwrap(), image.to_string())
}


#[poise::command(prefix_command)]
pub async fn get_leaderboard(ctx: Context<'_>, param: Option<String>) -> Result<(), Error> {
let http = ctx.http();
let leaderboard = ctx.data().evening_leaderboard.lock().await;

let leaderboard_top_10: String = 'top_10: {
if leaderboard.len() == 0 {
break 'top_10 "noone yet :(".to_string();
}

let mut sorted: Vec<(&u64, &u16)> = leaderboard.iter().collect();
sorted.sort_by(|a, b| b.1.cmp(a.1));
sorted.shrink_to(10);

let mut top_10 = String::new();
let mut position = 1;
for (id, count) in sorted {
let user_id = UserId::new(id.clone());
let user = user_id.to_user(http).await.expect("couldnt get user from id for leaderboard");
let username = user.global_name.unwrap();

top_10.push_str(&format!("{}. {}: {}\n", position, username, count));

position += 1;
}
top_10.remove(top_10.len() - 1);

top_10
};

debug!("leaderboard retrieved by command:\n{leaderboard_top_10}");

let embed = CreateEmbed::new()
.colour(Colour::from_rgb(255, 0, 124))
.title("good evening leaderboard")
.description(leaderboard_top_10);



if let Some(param) = param {
if param == "general" {
const GENERAL_CHANNEL_ID: u64 = 1215048710074011692;
let channel = ChannelId::new(GENERAL_CHANNEL_ID);

let message = CreateMessage::new()
.add_embed(embed);

channel.send_message(http, message).await?;
}
} else {
let message = CreateReply {
embeds: vec![embed],
..Default::default()
};

ctx.send(message).await?;
}

Ok(())
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub async fn poise_setup(shared_data: &SharedData) -> serenity::Client {
})
})
.options(poise::FrameworkOptions {
commands: vec![commands::fact_check()],
commands: vec![commands::fact_check(), commands::get_leaderboard()],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("!".into()),
..Default::default()
Expand Down

0 comments on commit 3a8f9a8

Please sign in to comment.