Added more ways to get fren coin + other fixes

+ Clippy + fmt
This commit is contained in:
Joey Hines 2022-12-19 19:07:17 -07:00
parent 8b01177557
commit b14c1e8278
Signed by: joeyahines
GPG Key ID: 995E531F7A569DDB
6 changed files with 100 additions and 34 deletions

View File

@ -1,4 +1,5 @@
use crate::error::Error;
use crate::error::Error::NoAlbumFound;
use crate::imgur;
use crate::imgur::Image;
use config::{Config, File};
@ -111,11 +112,11 @@ impl BotState {
})
}
pub fn get_image(&self, album_name: &str, tags: Vec<&str>) -> Option<Image> {
pub fn get_image(&self, album_name: &str, tags: Vec<&str>) -> Result<Option<Image>, Error> {
let mut rng = rand::thread_rng();
let album = match self.albums.get(album_name) {
None => return None,
None => return Err(NoAlbumFound),
Some(a) => a,
};
@ -139,7 +140,7 @@ impl BotState {
.collect()
};
album.choose(&mut rng).cloned()
Ok(album.choose(&mut rng).cloned())
}
}

View File

@ -131,11 +131,21 @@ pub async fn parse_album(
let data = ctx.data.read().await;
let global_data = data.get::<GlobalData>().unwrap();
Ok(match global_data.bot_state.get_image(album_name, tags) {
Some(image) => {
msg.reply(&ctx.http, &image.link).await?;
true
let img = match global_data.bot_state.get_image(album_name, tags) {
Ok(img) => img,
Err(err) => {
return match err {
Error::NoAlbumFound => Ok(false),
_ => Err(err),
}
None => false,
})
}
};
if let Some(img) = img {
msg.reply(&ctx.http, img.link).await?;
} else {
msg.reply(&ctx.http, "No image found :(").await?;
}
Ok(true)
}

View File

@ -1,5 +1,7 @@
use crate::config::{BotConfig, GlobalData, Wallet};
use crate::error::Error;
use crate::{command, group};
use rand::{thread_rng, Rng};
use serenity::client::Context;
use serenity::framework::standard::{Args, CommandResult};
use serenity::model::channel::Message;
@ -102,3 +104,34 @@ async fn gift(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
global_data.cfg.save(&global_data.args.cfg_path).await?;
Ok(())
}
pub async fn give_coin(
ctx: &Context,
user: UserId,
percent: f64,
numer_of_coins: i64,
) -> Result<bool, Error> {
let should_get_coin = {
let mut thread_rng = thread_rng();
thread_rng.gen_bool(percent)
};
if should_get_coin {
let mut data = ctx.data.write().await;
let global_data = data.get_mut::<GlobalData>().unwrap();
{
let wallet = get_user_wallet(&mut global_data.cfg, user);
wallet.coin_count += numer_of_coins;
}
global_data
.cfg
.save(&global_data.args.cfg_path)
.await
.unwrap();
}
Ok(should_get_coin)
}

View File

@ -56,7 +56,7 @@ impl RandomCtx {
let mut random_image: HashMap<String, String> = HashMap::new();
for album in &global_data.cfg.albums {
let image = global_data.bot_state.get_image(&album.name, Vec::new());
let image = global_data.bot_state.get_image(&album.name, Vec::new())?;
if let Some(image) = image {
random_image.insert(album.name.clone(), image.link);

View File

@ -6,16 +6,17 @@ pub mod fren_coin;
pub mod joke;
pub mod story;
use crate::discord::fren_coin::get_user_wallet;
use crate::discord::fren_coin::give_coin;
use crate::discord::joke::random;
use crate::{help, hook, GlobalData};
use rand::{thread_rng, Rng};
use rand::prelude::IteratorRandom;
use rand::thread_rng;
use serenity::async_trait;
use serenity::client::Context;
use serenity::framework::standard::{
help_commands, Args, CommandGroup, CommandResult, HelpOptions,
};
use serenity::model::channel::Message;
use serenity::model::channel::{Message, ReactionType};
use serenity::model::id::UserId;
use serenity::model::prelude::{GuildId, Ready};
use serenity::prelude::EventHandler;
@ -50,6 +51,10 @@ impl EventHandler for Handler {
return;
}
if new_message.guild_id.is_none() {
return;
}
if new_message.content.eq_ignore_ascii_case("yes")
|| new_message.content.eq_ignore_ascii_case("mhmm")
{
@ -63,6 +68,39 @@ impl EventHandler for Handler {
}
}
}
if new_message.content.to_lowercase().contains("good bot") {
let recv_coin = give_coin(&ctx, new_message.author.id, 0.50, 25)
.await
.unwrap();
if recv_coin {
let emojis = new_message.guild(&ctx.cache).unwrap().emojis;
let emoji = {
let mut rng = thread_rng();
emojis.iter().choose(&mut rng)
};
if let Some(emoji) = emoji {
new_message
.react(
&ctx.http,
ReactionType::Custom {
animated: emoji.1.animated,
id: emoji.1.id,
name: Some(emoji.1.name.clone()),
},
)
.await
.unwrap();
}
}
}
give_coin(&ctx, new_message.author.id, 0.05, 5)
.await
.unwrap();
}
async fn ready(&self, _: Context, ready: Ready) {
@ -80,27 +118,7 @@ pub async fn after(
match command_result {
Ok(()) => {
println!("Processed command '{}'", command_name);
let give_coins = {
let mut rng = thread_rng();
rng.gen_bool(0.10)
};
if give_coins {
let mut data = ctx.data.write().await;
let global_data = data.get_mut::<GlobalData>().unwrap();
{
let wallet = get_user_wallet(&mut global_data.cfg, msg.author.id);
wallet.coin_count += 5;
}
global_data
.cfg
.save(&global_data.args.cfg_path)
.await
.unwrap();
}
give_coin(ctx, msg.author.id, 0.10, 10).await.unwrap();
}
Err(why) => {
println!("Command '{}' returned error {:?}", command_name, why);
@ -142,6 +160,8 @@ pub async fn unrecognised_command_hook(
Err(e) => println!("Error processing random command: {}", e),
}
}
give_coin(ctx, msg.author.id, 0.5, 10).await.unwrap();
}
#[help]

View File

@ -10,6 +10,7 @@ pub enum Error {
ImgurError(ImgurError),
SerenityError(serenity::Error),
TeraError(tera::Error),
NoAlbumFound,
}
impl StdError for Error {}
@ -39,6 +40,7 @@ impl Display for Error {
Error::ImgurError(e) => write!(f, "Imgur error: {}", e),
Error::SerenityError(e) => write!(f, "Discord error: {}", e),
Error::TeraError(e) => write!(f, "Tera error: {}", e),
Error::NoAlbumFound => write!(f, "No album found"),
}
}
}