84 lines
2.3 KiB
Rust
84 lines
2.3 KiB
Rust
use crate::discord::Context;
|
|
use crate::error::Error;
|
|
use crate::user::{User, UserError};
|
|
use j_db::database::Database;
|
|
use log::info;
|
|
use poise::serenity_prelude::model::id::UserId;
|
|
use poise::serenity_prelude::prelude::Mentionable;
|
|
use rand::{rng, Rng};
|
|
|
|
#[poise::command(prefix_command, category = "FrenCoin", aliases("audit"))]
|
|
pub async fn balance(
|
|
ctx: Context<'_>,
|
|
#[description = "User to get balance of"] user: Option<UserId>,
|
|
) -> Result<(), Error> {
|
|
let user = user.unwrap_or(ctx.author().id);
|
|
|
|
let wallet = User::get_user(&ctx.data().db, user)?;
|
|
|
|
ctx.reply(format!(
|
|
"{}'s current balance is {} fren coins!",
|
|
user.mention(),
|
|
wallet.coin_count
|
|
))
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[poise::command(prefix_command, category = "FrenCoin", aliases("audit"))]
|
|
pub async fn gift(
|
|
ctx: Context<'_>,
|
|
#[description = "User to gift coin to"] target: UserId,
|
|
#[description = "Amount of coins to give"] amount: u32,
|
|
) -> Result<(), Error> {
|
|
if let Err(e) = User::transfer_funds(&ctx.data().db, ctx.author().id, target, amount) {
|
|
if let Error::UserError(err) = e {
|
|
match err {
|
|
UserError::NotEnoughFunds => {
|
|
ctx.reply(
|
|
"Sorry pal, I can't give credit. Come back when you're a bit mmmm richer.",
|
|
)
|
|
.await?;
|
|
}
|
|
UserError::InvalidTarget => {
|
|
ctx.reply(
|
|
"Sorry pal, I can't make a friend up for you. Come back when you got friends.",
|
|
)
|
|
.await?;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
} else {
|
|
ctx.reply(format!(
|
|
"You have gifted {} {} fren coins!",
|
|
target.mention(),
|
|
amount
|
|
))
|
|
.await?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn give_coin(
|
|
db: &Database,
|
|
user: UserId,
|
|
percent: f64,
|
|
number_of_coins: i64,
|
|
) -> Result<bool, Error> {
|
|
let should_get_coin = {
|
|
let mut rng = rng();
|
|
|
|
rng.random_bool(percent)
|
|
};
|
|
|
|
if should_get_coin {
|
|
info!("Randomly giving {} {} fren coins", user, number_of_coins);
|
|
User::give_funds(db, user, number_of_coins as u32)?;
|
|
}
|
|
|
|
Ok(should_get_coin)
|
|
}
|