From 6219dd817562ce4c742ffda27d2e2ffbe2708209 Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Sat, 7 Dec 2024 23:16:26 -0500 Subject: [PATCH] add command to claim daily tokens --- src/commands/gambling/balance.rs | 2 +- src/commands/gambling/daily.rs | 37 ++++++++++++++++++++++++++++++++ src/commands/gambling/mod.rs | 7 ++++++ src/commands/mod.rs | 2 ++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/commands/gambling/daily.rs diff --git a/src/commands/gambling/balance.rs b/src/commands/gambling/balance.rs index 80e8175..069b657 100644 --- a/src/commands/gambling/balance.rs +++ b/src/commands/gambling/balance.rs @@ -2,7 +2,7 @@ use crate::common::{Context, Error}; use poise::serenity_prelude as serenity; /// Tells you what your or someone else's balance is -#[poise::command(slash_command, prefix_command)] +#[poise::command(slash_command, prefix_command, aliases("bal"))] pub async fn balance(ctx: Context<'_>, user: Option) -> Result<(), Error> { let user = user.as_ref().unwrap_or(ctx.author()); let data = ctx.data(); diff --git a/src/commands/gambling/daily.rs b/src/commands/gambling/daily.rs new file mode 100644 index 0000000..bb20156 --- /dev/null +++ b/src/commands/gambling/daily.rs @@ -0,0 +1,37 @@ +use crate::{Context, Error}; + +use std::time::{Duration, Instant}; +use poise::serenity_prelude as serenity; + +/// Redeem 50 daily tokens. +#[poise::command(slash_command, prefix_command)] +pub async fn daily(ctx: Context<'_>) -> Result<(), Error> { + let data = ctx.data(); + let mut db = data.database.lock().await; + let db = db.as_mut(); + + let id = ctx.author().id; + + let mut dailies = data.dailies.lock().await; + + match dailies.get_mut(&id) { + Some(daily) => { + + if daily.elapsed() >= Duration::from_secs(24 * 60 * 60) { + *daily = Instant::now(); + super::add_balance(id, 50, db).await?; + ctx.reply("Added **50** credits to your account!").await?; + } else { + let until_next_daily = Duration::from_secs(10) - daily.elapsed(); + ctx.reply(format!("Your daily will be available in {:?}.", until_next_daily)).await?; + } + }, + None => { + dailies.insert(id.clone(), Instant::now()); + super::add_balance(id, 50, db).await?; + ctx.reply("Added **50** credits to your account!").await?; + } + } + + Ok(()) +} diff --git a/src/commands/gambling/mod.rs b/src/commands/gambling/mod.rs index 7387ad7..4642500 100644 --- a/src/commands/gambling/mod.rs +++ b/src/commands/gambling/mod.rs @@ -2,6 +2,7 @@ pub mod balance; pub mod give; pub mod wager; +pub mod daily; use crate::common::Error; use poise::serenity_prelude::UserId; @@ -28,4 +29,10 @@ pub async fn change_balance(id: UserId, balance: i32, db: &mut PgConnection) -> .execute(db).await?; Ok(()) +} + +pub async fn add_balance(id: UserId, amount: i32, db: &mut PgConnection) -> Result<(), Error> { + let balance = get_balance(id, db).await?; + change_balance(id, balance + amount, db).await?; + Ok(()) } \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 9e50758..a4b4e46 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -6,6 +6,7 @@ mod dox; mod yeehaw; mod gambling; mod eval; +mod self_roles; pub fn commands() -> Vec> { vec![ @@ -15,6 +16,7 @@ pub fn commands() -> Vec> { gambling::balance::balance(), gambling::give::give(), gambling::wager::wager(), + gambling::daily::daily(), eval::eval(), ] }