Fix flipped social credit bounds for point ranges

This commit is contained in:
Joey Hines 2025-04-21 10:57:39 -06:00
parent 77441b7ccd
commit 283c09bd1b
Signed by: joeyahines
GPG Key ID: 38BA6F25C94C9382
4 changed files with 57 additions and 6 deletions

View File

@ -271,11 +271,20 @@ pub async fn remove_role(
pub async fn add_social_credit_phrase( pub async fn add_social_credit_phrase(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "Phrase to look for, wrap in \"s if you want multiple words"] phrase: String, #[description = "Phrase to look for, wrap in \"s if you want multiple words"] phrase: String,
#[description = "Upper limit of points to add/take away"] upper_limit: i64, #[description = "Lower limit of points to add/take away"] lower_limit: i64,
#[description = "Lower limit of points to take away"] lower_limit: i64, #[description = "Upper limit of points to take away"] upper_limit: i64,
) -> Result<(), Error> { ) -> Result<(), Error> {
if upper_limit < lower_limit {
ctx.reply(format!(
"Invalid range, upper limit ({}) is less than lower limit ({})",
upper_limit, lower_limit
))
.await?;
return Ok(());
}
let social_credit_phrase = let social_credit_phrase =
SocialCreditPhrase::add_new_phrase(&ctx.data().db, phrase, upper_limit, lower_limit)?; SocialCreditPhrase::add_new_phrase(&ctx.data().db, phrase, lower_limit, upper_limit)?;
ctx.reply(format!( ctx.reply(format!(
"Added new phrase '{}' with id `{}`", "Added new phrase '{}' with id `{}`",

View File

@ -21,7 +21,7 @@ use crate::models::lil_fren::lil_fren_task;
use crate::models::social_credit::SocialCreditPhrase; use crate::models::social_credit::SocialCreditPhrase;
use crate::models::task::Task; use crate::models::task::Task;
use crate::user::User; use crate::user::User;
use log::{debug, error, info}; use log::{error, info};
use poise::serenity_prelude::{GuildId, Http, Message, MessageBuilder, ReactionType, RoleId}; use poise::serenity_prelude::{GuildId, Http, Message, MessageBuilder, ReactionType, RoleId};
use poise::{FrameworkOptions, find_command, serenity_prelude as serenity}; use poise::{FrameworkOptions, find_command, serenity_prelude as serenity};
use rand::prelude::IteratorRandom; use rand::prelude::IteratorRandom;
@ -128,7 +128,7 @@ async fn handle_message(
give_coin(&data.db, new_message.author.id, 0.05, 10).await?; give_coin(&data.db, new_message.author.id, 0.05, 10).await?;
if let Some(phrase) = SocialCreditPhrase::check_if_match(&data.db, &new_message.content)? { if let Some(phrase) = SocialCreditPhrase::check_if_match(&data.db, &new_message.content)? {
debug!( info!(
"{} matched phrase '{}' for social credit checking", "{} matched phrase '{}' for social credit checking",
new_message.author.name, phrase.phrase new_message.author.name, phrase.phrase
); );

View File

@ -0,0 +1,34 @@
use j_db::database::Database;
use j_db::migration::Migration;
pub struct Migration7FlipBounds {}
impl Migration for Migration7FlipBounds {
fn up(&self, db: &Database) -> j_db::error::Result<()> {
let tree = db.db.open_tree("SocialCreditPhrase")?;
for entry in tree.iter() {
let (id, scp_bytes) = entry?;
let mut scp = json::parse(std::str::from_utf8(&scp_bytes).unwrap()).unwrap();
let upper = scp["lower_bound"].clone();
let lower = scp["upper_bound"].clone();
scp["lower_bound"] = lower;
scp["upper_bound"] = upper;
tree.insert(id, scp.to_string().as_bytes())?;
}
Ok(())
}
fn down(&self, _db: &Database) -> j_db::error::Result<()> {
Ok(())
}
fn version(&self) -> u64 {
7
}
}

View File

@ -1,6 +1,7 @@
use crate::migrations::migration_4_update_random::Migration4UpdateRandoms; use crate::migrations::migration_4_update_random::Migration4UpdateRandoms;
use crate::migrations::migration_5_update_motivation::Migration5UpdateMotivation; use crate::migrations::migration_5_update_motivation::Migration5UpdateMotivation;
use crate::migrations::migration_6_add_social_credit::Migration6AddSocialCredit; use crate::migrations::migration_6_add_social_credit::Migration6AddSocialCredit;
use crate::migrations::migration_7_flip_bounds::Migration7FlipBounds;
use crate::migrations::migration2_remove_imgur::Migration2RemoveImgur; use crate::migrations::migration2_remove_imgur::Migration2RemoveImgur;
use crate::migrations::migration3_remove_img::Migration3RemoveImage; use crate::migrations::migration3_remove_img::Migration3RemoveImage;
use j_db::database::Database; use j_db::database::Database;
@ -12,8 +13,9 @@ mod migration3_remove_img;
mod migration_4_update_random; mod migration_4_update_random;
mod migration_5_update_motivation; mod migration_5_update_motivation;
mod migration_6_add_social_credit; mod migration_6_add_social_credit;
mod migration_7_flip_bounds;
pub const CURRENT_DB_VERSION: u64 = 6; pub const CURRENT_DB_VERSION: u64 = 7;
#[allow(clippy::single_match)] #[allow(clippy::single_match)]
pub fn do_migration(db: &Database) { pub fn do_migration(db: &Database) {
@ -56,6 +58,12 @@ pub fn do_migration(db: &Database) {
Direction::Up, Direction::Up,
) )
.unwrap(), .unwrap(),
7 => migration::do_migration::<Migration7FlipBounds>(
db,
Migration7FlipBounds {},
Direction::Up,
)
.unwrap(),
_ => {} _ => {}
} }
} }