add command to claim daily tokens

This commit is contained in:
2024-12-07 23:16:26 -05:00
parent 3bc51c858f
commit 6219dd8175
4 changed files with 47 additions and 1 deletions

View File

@@ -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<serenity::User>) -> Result<(), Error> {
let user = user.as_ref().unwrap_or(ctx.author());
let data = ctx.data();

View File

@@ -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(())
}

View File

@@ -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;
@@ -29,3 +30,9 @@ pub async fn change_balance(id: UserId, balance: i32, db: &mut PgConnection) ->
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(())
}

View File

@@ -6,6 +6,7 @@ mod dox;
mod yeehaw;
mod gambling;
mod eval;
mod self_roles;
pub fn commands() -> Vec<Command<Data, Error>> {
vec![
@@ -15,6 +16,7 @@ pub fn commands() -> Vec<Command<Data, Error>> {
gambling::balance::balance(),
gambling::give::give(),
gambling::wager::wager(),
gambling::daily::daily(),
eval::eval(),
]
}