FrenBot/src/discord/fren_coin.rs
Joey Hines c680f788c1
WOOO 1.1
* Fixed help command by implementing my own
* General cleanup
* Added an error handler
2025-03-23 13:28:18 -06:00

86 lines
2.4 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};
/// Get your balance or someone else's
#[poise::command(prefix_command, category = "Fren Coin", aliases("audit"))]
pub async fn balance(
ctx: Context<'_>,
#[description = "User to get balance of"] user: Option<poise::serenity_prelude::User>,
) -> Result<(), Error> {
let user = user.map(|u| u.id).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(())
}
/// Gift someone the gift that keeps on giving, Fren Coins!
#[poise::command(prefix_command, category = "Fren Coin", aliases("audit"))]
pub async fn gift(
ctx: Context<'_>,
#[description = "User to gift coin to"] target: poise::serenity_prelude::User,
#[description = "Amount of coins to give"] amount: u32,
) -> Result<(), Error> {
if let Err(e) = User::transfer_funds(&ctx.data().db, ctx.author().id, target.id, 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)
}