Add in fren coin commands
This commit is contained in:
parent
095d6e82a0
commit
0a84a0c07c
@ -1,94 +1,48 @@
|
|||||||
use crate::config::GlobalData;
|
use crate::discord::Context;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::user::{User, UserError};
|
use crate::user::{User, UserError};
|
||||||
use crate::{command, group};
|
use j_db::database::Database;
|
||||||
use rand::{thread_rng, Rng};
|
use log::info;
|
||||||
use serenity::all::parse_user_mention;
|
use poise::serenity_prelude::model::id::UserId;
|
||||||
use serenity::client::Context;
|
use poise::serenity_prelude::prelude::Mentionable;
|
||||||
use serenity::framework::standard::{Args, CommandResult};
|
use rand::{rng, Rng};
|
||||||
use serenity::model::channel::Message;
|
|
||||||
use serenity::model::id::UserId;
|
|
||||||
use serenity::prelude::Mentionable;
|
|
||||||
|
|
||||||
#[group]
|
#[poise::command(prefix_command, category = "FrenCoin", aliases("audit"))]
|
||||||
#[commands(balance, gift)]
|
pub async fn balance(
|
||||||
pub struct FrenCoin;
|
ctx: Context<'_>,
|
||||||
|
#[description = "User to get balance of"] user: Option<UserId>,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let user = user.unwrap_or(ctx.author().id);
|
||||||
|
|
||||||
#[command]
|
let wallet = User::get_user(&ctx.data().db, user)?;
|
||||||
#[description("Get your current balance")]
|
|
||||||
#[aliases("audit")]
|
|
||||||
async fn balance(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
|
||||||
let mut data = ctx.data.write().await;
|
|
||||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
|
||||||
|
|
||||||
let user = if args.is_empty() {
|
ctx.reply(format!(
|
||||||
msg.author.id
|
"{}'s current balance is {} fren coins!",
|
||||||
} else {
|
user.mention(),
|
||||||
let mention = args.parse::<String>().unwrap();
|
wallet.coin_count
|
||||||
parse_user_mention(&mention).unwrap()
|
))
|
||||||
};
|
|
||||||
|
|
||||||
let wallet = User::get_user(&global_data.db, user)?;
|
|
||||||
|
|
||||||
msg.reply(
|
|
||||||
&ctx.http,
|
|
||||||
format!(
|
|
||||||
"{}'s current balance is {} fren coins!",
|
|
||||||
user.mention(),
|
|
||||||
wallet.coin_count
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[command]
|
#[poise::command(prefix_command, category = "FrenCoin", aliases("audit"))]
|
||||||
#[only_in(guilds)]
|
pub async fn gift(
|
||||||
#[description("Gift your frens coins!")]
|
ctx: Context<'_>,
|
||||||
#[usage("@fren 25")]
|
#[description = "User to gift coin to"] target: UserId,
|
||||||
async fn gift(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
#[description = "Amount of coins to give"] amount: u32,
|
||||||
let target = args.parse::<String>().unwrap();
|
) -> Result<(), Error> {
|
||||||
|
if let Err(e) = User::transfer_funds(&ctx.data().db, ctx.author().id, target, amount) {
|
||||||
let target = match parse_user_mention(&target) {
|
|
||||||
None => {
|
|
||||||
msg.reply(
|
|
||||||
&ctx.http,
|
|
||||||
"Gonna be real honest with you gamer, no clue who that is",
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
Some(t) => t,
|
|
||||||
};
|
|
||||||
|
|
||||||
args.advance();
|
|
||||||
|
|
||||||
let amount = match args.parse::<u32>() {
|
|
||||||
Ok(amount) => amount,
|
|
||||||
Err(err) => {
|
|
||||||
msg.reply(&ctx.http, format!("Invalid coin amount: {}", err))
|
|
||||||
.await?;
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut data = ctx.data.write().await;
|
|
||||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
|
||||||
|
|
||||||
if let Err(e) = User::transfer_funds(&global_data.db, msg.author.id, target, amount) {
|
|
||||||
if let Error::UserError(err) = e {
|
if let Error::UserError(err) = e {
|
||||||
match err {
|
match err {
|
||||||
UserError::NotEnoughFunds => {
|
UserError::NotEnoughFunds => {
|
||||||
msg.reply(
|
ctx.reply(
|
||||||
&ctx.http,
|
|
||||||
"Sorry pal, I can't give credit. Come back when you're a bit mmmm richer.",
|
"Sorry pal, I can't give credit. Come back when you're a bit mmmm richer.",
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
UserError::InvalidTarget => {
|
UserError::InvalidTarget => {
|
||||||
msg.reply(
|
ctx.reply(
|
||||||
&ctx.http,
|
|
||||||
"Sorry pal, I can't make a friend up for you. Come back when you got friends.",
|
"Sorry pal, I can't make a friend up for you. Come back when you got friends.",
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
@ -97,14 +51,11 @@ async fn gift(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
msg.reply(
|
ctx.reply(format!(
|
||||||
&ctx.http,
|
"You have gifted {} {} fren coins!",
|
||||||
format!(
|
target.mention(),
|
||||||
"You have gifted {} {} fren coins!",
|
amount
|
||||||
target.mention(),
|
))
|
||||||
amount
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,22 +63,20 @@ async fn gift(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn give_coin(
|
pub async fn give_coin(
|
||||||
ctx: &Context,
|
db: &Database,
|
||||||
user: UserId,
|
user: UserId,
|
||||||
percent: f64,
|
percent: f64,
|
||||||
number_of_coins: i64,
|
number_of_coins: i64,
|
||||||
) -> Result<bool, Error> {
|
) -> Result<bool, Error> {
|
||||||
let should_get_coin = {
|
let should_get_coin = {
|
||||||
let mut thread_rng = thread_rng();
|
let mut rng = rng();
|
||||||
|
|
||||||
thread_rng.gen_bool(percent)
|
rng.random_bool(percent)
|
||||||
};
|
};
|
||||||
|
|
||||||
if should_get_coin {
|
if should_get_coin {
|
||||||
let mut data = ctx.data.write().await;
|
info!("Randomly giving {} {} fren coins", user, number_of_coins);
|
||||||
let global_data = data.get_mut::<GlobalData>().unwrap();
|
User::give_funds(db, user, number_of_coins as u32)?;
|
||||||
|
|
||||||
User::give_funds(&global_data.db, user, number_of_coins as u32)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(should_get_coin)
|
Ok(should_get_coin)
|
||||||
|
|||||||
@ -4,6 +4,7 @@ mod birthday;
|
|||||||
mod celeryman;
|
mod celeryman;
|
||||||
mod color;
|
mod color;
|
||||||
mod emoji_race;
|
mod emoji_race;
|
||||||
|
mod fren_coin;
|
||||||
|
|
||||||
use crate::config::GlobalData;
|
use crate::config::GlobalData;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
@ -11,9 +12,12 @@ use log::info;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use crate::discord::fren_coin::give_coin;
|
||||||
use crate::models::task::Task;
|
use crate::models::task::Task;
|
||||||
use poise::serenity_prelude::Message;
|
use poise::serenity_prelude::{Message, ReactionType};
|
||||||
use poise::{find_command, serenity_prelude as serenity, FrameworkOptions};
|
use poise::{find_command, serenity_prelude as serenity, FrameworkOptions};
|
||||||
|
use rand::prelude::IteratorRandom;
|
||||||
|
use rand::rng;
|
||||||
|
|
||||||
pub type Context<'a> = poise::Context<'a, Arc<GlobalData>, Error>;
|
pub type Context<'a> = poise::Context<'a, Arc<GlobalData>, Error>;
|
||||||
|
|
||||||
@ -52,12 +56,54 @@ async fn event_handler(
|
|||||||
handle_unrecognised_commands(ctx, new_message, data).await?;
|
handle_unrecognised_commands(ctx, new_message, data).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handle_message(ctx, data, new_message).await?;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn handle_message(
|
||||||
|
ctx: &serenity::Context,
|
||||||
|
data: &Arc<GlobalData>,
|
||||||
|
new_message: &Message,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
if new_message.content.to_lowercase().contains("good bot") {
|
||||||
|
let recv_coin = give_coin(&data.db, new_message.author.id, 0.50, 25).await?;
|
||||||
|
|
||||||
|
if recv_coin {
|
||||||
|
let emojis = &new_message.guild_id.unwrap().emojis(&ctx.http).await?;
|
||||||
|
|
||||||
|
let emoji = {
|
||||||
|
let mut rng = rng();
|
||||||
|
emojis.iter().choose(&mut rng)
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(emoji) = emoji {
|
||||||
|
new_message
|
||||||
|
.react(
|
||||||
|
&ctx.http,
|
||||||
|
ReactionType::Custom {
|
||||||
|
animated: emoji.animated,
|
||||||
|
id: emoji.id,
|
||||||
|
name: Some(emoji.name.clone()),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if new_message.content.to_lowercase().contains("bad bot") {
|
||||||
|
new_message.react(&ctx.http, '😭').await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
give_coin(&data.db, new_message.author.id, 0.05, 10).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn handle_unrecognised_commands(
|
async fn handle_unrecognised_commands(
|
||||||
ctx: &serenity::Context,
|
ctx: &serenity::Context,
|
||||||
message: &Message,
|
message: &Message,
|
||||||
@ -125,6 +171,8 @@ pub async fn run_bot(global_data: GlobalData) {
|
|||||||
emoji_race::bet(),
|
emoji_race::bet(),
|
||||||
emoji_race::race(),
|
emoji_race::race(),
|
||||||
emoji_race::start_race(),
|
emoji_race::start_race(),
|
||||||
|
fren_coin::balance(),
|
||||||
|
fren_coin::gift(),
|
||||||
],
|
],
|
||||||
event_handler: |ctx, event, framework, data| {
|
event_handler: |ctx, event, framework, data| {
|
||||||
Box::pin(event_handler(ctx, event, framework, data))
|
Box::pin(event_handler(ctx, event, framework, data))
|
||||||
|
|||||||
@ -76,7 +76,7 @@ impl Task {
|
|||||||
time,
|
time,
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
println!("Adding {:?} task to run at {}", task_type, time);
|
info!("Adding {:?} task to run at {}", task_type, time);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user