Updated motivations to use new randoms
+ Handle task failures more gracefully
This commit is contained in:
parent
f5ec1cd000
commit
2f7954632d
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -787,7 +787,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "fren"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"axum-macros",
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "fren"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
@ -10,12 +10,7 @@ use serenity::model::channel::Message;
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[group]
|
||||
#[commands(
|
||||
motivation,
|
||||
motivation_add_album,
|
||||
motivation_add_action,
|
||||
motivation_add_goal
|
||||
)]
|
||||
#[commands(motivation, motivation_add_album)]
|
||||
pub struct Motivate;
|
||||
|
||||
pub async fn create_motivation_image(motivation: Motivation) -> Result<Vec<u8>, CommandError> {
|
||||
@ -132,33 +127,3 @@ async fn motivation_add_album(ctx: &Context, msg: &Message, args: Args) -> Comma
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[command]
|
||||
#[description("Add an action to the motivation generator")]
|
||||
async fn motivation_add_action(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
let data = ctx.data.read().await;
|
||||
let global_data = data.get::<GlobalData>().unwrap();
|
||||
|
||||
let action = args.rest();
|
||||
|
||||
MotivationConfig::add_action(&global_data.db, action)?;
|
||||
|
||||
msg.reply(&ctx.http, "Done ;)").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[command]
|
||||
#[description("Add goal to the motivation generator")]
|
||||
async fn motivation_add_goal(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
let data = ctx.data.read().await;
|
||||
let global_data = data.get::<GlobalData>().unwrap();
|
||||
|
||||
let goal = args.rest();
|
||||
|
||||
MotivationConfig::add_goal(&global_data.db, goal)?;
|
||||
|
||||
msg.reply(&ctx.http, "Done ;)").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
38
src/migrations/migration_5_update_motivation.rs
Normal file
38
src/migrations/migration_5_update_motivation.rs
Normal file
@ -0,0 +1,38 @@
|
||||
use crate::models::motivation::{ACTION_RANDOM_GROUP, GOAL_RANDOM_GROUP};
|
||||
use crate::models::random::RandomConfig;
|
||||
use j_db::database::Database;
|
||||
use j_db::migration::Migration;
|
||||
|
||||
pub struct Migration5UpdateMotivation {}
|
||||
|
||||
impl Migration for Migration5UpdateMotivation {
|
||||
fn up(&self, db: &Database) -> j_db::error::Result<()> {
|
||||
let tree = db.db.open_tree("Motivation")?;
|
||||
let (id, config_bytes) = tree.first()?.unwrap();
|
||||
|
||||
let mut config = json::parse(std::str::from_utf8(&config_bytes).unwrap()).unwrap();
|
||||
|
||||
for action in config["action"].members() {
|
||||
RandomConfig::add_random(db, ACTION_RANDOM_GROUP, &action.to_string()).ok();
|
||||
}
|
||||
|
||||
for goal in config["goal"].members() {
|
||||
RandomConfig::add_random(db, GOAL_RANDOM_GROUP, &goal.to_string()).ok();
|
||||
}
|
||||
|
||||
config.remove("action");
|
||||
config.remove("goal");
|
||||
|
||||
tree.insert(id, config.to_string().into_bytes()).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn down(&self, _db: &Database) -> j_db::error::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn version(&self) -> u64 {
|
||||
5
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
use crate::migrations::migration2_remove_imgur::Migration2RemoveImgur;
|
||||
use crate::migrations::migration3_remove_img::Migration3RemoveImage;
|
||||
use crate::migrations::migration_4_update_random::Migration4UpdateRandoms;
|
||||
use crate::migrations::migration_5_update_motivation::Migration5UpdateMotivation;
|
||||
use j_db::database::Database;
|
||||
use j_db::migration;
|
||||
use j_db::migration::Direction;
|
||||
@ -8,8 +9,9 @@ use j_db::migration::Direction;
|
||||
mod migration2_remove_imgur;
|
||||
mod migration3_remove_img;
|
||||
mod migration_4_update_random;
|
||||
mod migration_5_update_motivation;
|
||||
|
||||
const CURRENT_DB_VERSION: u64 = 4;
|
||||
const CURRENT_DB_VERSION: u64 = 5;
|
||||
|
||||
#[allow(clippy::single_match)]
|
||||
pub fn do_migration(db: &Database) {
|
||||
@ -40,6 +42,12 @@ pub fn do_migration(db: &Database) {
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
5 => migration::do_migration::<Migration5UpdateMotivation>(
|
||||
db,
|
||||
Migration5UpdateMotivation {},
|
||||
Direction::Up,
|
||||
)
|
||||
.unwrap(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,19 +1,21 @@
|
||||
use crate::album_manager::{AlbumManager, AlbumQuery, Image};
|
||||
use crate::error::Error;
|
||||
use crate::models::random::RandomConfig;
|
||||
use j_db::database::Database;
|
||||
use j_db::error::JDbError;
|
||||
use j_db::model::JdbModel;
|
||||
use rand::prelude::{IteratorRandom, SliceRandom};
|
||||
use rand::prelude::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub const GOAL_RANDOM_GROUP: &str = "Goal";
|
||||
pub const ACTION_RANDOM_GROUP: &str = "Action";
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct MotivationConfig {
|
||||
id: Option<u64>,
|
||||
pub album: Vec<u64>,
|
||||
pub action: Vec<String>,
|
||||
pub goal: Vec<String>,
|
||||
}
|
||||
|
||||
impl JdbModel for MotivationConfig {
|
||||
@ -42,8 +44,6 @@ impl MotivationConfig {
|
||||
.unwrap_or(Self {
|
||||
id: None,
|
||||
album: vec![],
|
||||
action: vec![],
|
||||
goal: vec![],
|
||||
}))
|
||||
}
|
||||
|
||||
@ -56,24 +56,6 @@ impl MotivationConfig {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_action(db: &Database, action: &str) -> Result<(), Error> {
|
||||
let mut motivation = Self::get_motivation_config(db)?;
|
||||
motivation.action.push(action.to_string());
|
||||
|
||||
db.insert(motivation)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_goal(db: &Database, goal: &str) -> Result<(), Error> {
|
||||
let mut motivation = Self::get_motivation_config(db)?;
|
||||
motivation.goal.push(goal.to_string());
|
||||
|
||||
db.insert(motivation)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn generate_motivation(
|
||||
db: &Database,
|
||||
picox: &AlbumManager,
|
||||
@ -112,9 +94,11 @@ impl MotivationConfig {
|
||||
.choose(&mut thread_rng())
|
||||
.ok_or(Error::NoAlbumFound)?;
|
||||
|
||||
let action = motivation.action.iter().choose(&mut thread_rng()).unwrap();
|
||||
let actions = RandomConfig::get_random(db, ACTION_RANDOM_GROUP)?.unwrap_or_default();
|
||||
let goals = RandomConfig::get_random(db, GOAL_RANDOM_GROUP)?.unwrap_or_default();
|
||||
|
||||
let goal = motivation.goal.iter().choose(&mut thread_rng()).unwrap();
|
||||
let action = actions.get_response(db)?.unwrap_or_default();
|
||||
let goal = goals.get_response(db)?.unwrap_or_default();
|
||||
|
||||
let image = picox.get_image_by_id(*image).await.unwrap();
|
||||
|
||||
|
||||
@ -43,7 +43,7 @@ impl JdbModel for Random {
|
||||
!self.response.eq_ignore_ascii_case(&other.response)
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
|
||||
pub struct RandomConfig {
|
||||
id: Option<u64>,
|
||||
pub name: String,
|
||||
|
||||
@ -173,7 +173,12 @@ impl Task {
|
||||
}
|
||||
TaskType::HandleReload => {
|
||||
println!("Reloading config...");
|
||||
global_data.reload().await.unwrap();
|
||||
let res = global_data.reload().await;
|
||||
|
||||
match res {
|
||||
Ok(_) => println!("Finished reloading config!"),
|
||||
Err(err) => println!("Error reloading config: {:?}", err),
|
||||
}
|
||||
|
||||
Task::add_task(
|
||||
&global_data.db,
|
||||
@ -183,7 +188,13 @@ impl Task {
|
||||
}
|
||||
TaskType::RestockShop => {
|
||||
println!("Restocking Shop...");
|
||||
restock_shop(ctx, global_data).await.unwrap();
|
||||
let res = restock_shop(ctx, global_data).await;
|
||||
|
||||
match res {
|
||||
Ok(_) => println!("Finished restocking shop!"),
|
||||
Err(err) => println!("Error restocking shop: {:?}", err),
|
||||
}
|
||||
|
||||
Task::add_task(
|
||||
&global_data.db,
|
||||
TaskType::RestockShop,
|
||||
|
||||
@ -45,7 +45,7 @@ impl User {
|
||||
Self {
|
||||
id: None,
|
||||
user_id,
|
||||
coin_count: -100_000_00,
|
||||
coin_count: 0,
|
||||
inventory: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user