discord-kurante/src/commands/op/pity.rs

76 lines
2.1 KiB
Rust

use super::queries::OpCommandQueries;
use crate::{util::debug_say, DbConnPool};
use log::warn;
use serenity::framework::standard::{macros::command, Args, CommandResult};
use serenity::model::{channel::Message, id::UserId};
use serenity::prelude::Context;
use std::str::FromStr;
#[command]
#[sub_commands(reset)]
pub(super) async fn pity(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let db_pool = ctx.data.clone();
let db_pool = db_pool.read().await;
let db_pool = db_pool
.get::<DbConnPool>()
.expect("No db pool in context?!");
let mut other_user = false;
let user_id = match args.current().map(|id| UserId::from_str(id.trim())) {
Some(Ok(user_id)) => {
other_user = true;
user_id
}
_ => msg.author.id,
}
.as_u64()
.to_string();
match db_pool.get_roll_count(user_id).await {
Ok(count) => {
debug_say(
msg,
ctx,
format!(
"{}'s Current roll: {}.\n6\u{2605} chance: {}%",
msg.author,
count,
2 + (count as u16).saturating_sub(50) * 2
),
)
.await?;
}
Err(sqlx::Error::RowNotFound) => {
if !other_user {
reset(ctx, msg, Args::new("", &[])).await?;
} else {
debug_say(
msg,
ctx,
"We don't know where you're currently at. Use ~pity reset first!",
)
.await?;
}
}
Err(_) => {
warn!("Unable to communicate with database");
}
}
Ok(())
}
#[command]
async fn reset(ctx: &Context, msg: &Message) -> CommandResult {
let db_pool = ctx.data.clone();
let db_pool = db_pool.read().await;
let db_pool = db_pool
.get::<DbConnPool>()
.expect("No db pool in context?!");
db_pool
.reset_roll_count(msg.author.id.as_u64().to_string())
.await?;
pity(ctx, msg, Args::new("", &[])).await?;
Ok(())
}