From 9fbbac8c72edd6fea8a119c336e63da73e2efa2c Mon Sep 17 00:00:00 2001 From: 1Kill2Steal <171241044+1Git2Clone@users.noreply.github.com> Date: Sun, 15 Dec 2024 18:07:41 +0200 Subject: [PATCH] Improved the mentioning counting --- src/database/bot_mentions.rs | 15 +++++++++------ src/event_handler/handler.rs | 3 +-- src/utils/replies.rs | 36 ++++++++++++++++++++++++------------ 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/database/bot_mentions.rs b/src/database/bot_mentions.rs index c2ed2b3..8277402 100644 --- a/src/database/bot_mentions.rs +++ b/src/database/bot_mentions.rs @@ -5,7 +5,7 @@ use sqlx::sqlite::SqliteQueryResult; use sqlx::Row; use sqlx::SqlitePool; -pub async fn fetch_mentions(db: &SqlitePool) -> Result { +pub async fn fetch_mentions(db: &SqlitePool) -> Result { let query = format!( "SELECT `{}` FROM `{}`", MENTIONS_TABLE[&MentionsSchema::Mentions], @@ -18,14 +18,14 @@ pub async fn fetch_mentions(db: &SqlitePool) -> Result { None => return Err(format!("Couldn't find a row to select (SQL: {})", query).into()), }; - let queried_mentions = row.get::(MENTIONS_TABLE[&MentionsSchema::Mentions]); + let queried_mentions = row.get::(MENTIONS_TABLE[&MentionsSchema::Mentions]); - Ok(queried_mentions) + Ok(queried_mentions as usize) } pub async fn update_mentions( db: &SqlitePool, - updated_mentions: i64, + updated_mentions: usize, ) -> Result { let query = format!( "UPDATE `{}` SET `{}` = ?", @@ -33,10 +33,13 @@ pub async fn update_mentions( MENTIONS_TABLE[&MentionsSchema::Mentions] ); - sqlx::query(&query).bind(updated_mentions).execute(db).await + sqlx::query(&query) + .bind(updated_mentions as i64) + .execute(db) + .await } -pub async fn add_mentions(db: &SqlitePool, n: i64) -> Result { +pub async fn add_mentions(db: &SqlitePool, n: usize) -> Result { let fetched_mentions = fetch_mentions(db).await?; update_mentions(db, fetched_mentions + n).await?; diff --git a/src/event_handler/handler.rs b/src/event_handler/handler.rs index 5a52d15..4a0fb4c 100644 --- a/src/event_handler/handler.rs +++ b/src/event_handler/handler.rs @@ -56,8 +56,7 @@ pub async fn event_handler( &new_message.content, ); let text_patterns = ["hutao", "hu tao"]; - let lowercase_msg = msg.to_lowercase(); - let trimmed_emojis = remove_emojis_and_embeds_from_str(&lowercase_msg); + let trimmed_emojis = remove_emojis_and_embeds_from_str(msg); let db = connect_to_db(DATABASE_FILENAME.to_string()).await; match db.await { diff --git a/src/utils/replies.rs b/src/utils/replies.rs index 72fd329..73a5ffc 100644 --- a/src/utils/replies.rs +++ b/src/utils/replies.rs @@ -8,22 +8,34 @@ pub async fn handle_replies( new_message: &serenity::Message, msg: &str, ) -> Result<(), Error> { - if msg + let no_whitespace = msg .to_lowercase() .split_whitespace() .collect::>() - .join("") - == "damnhutaomains" - { - add_mentions(db, 1).await?; + .join(""); + let hutao_count = no_whitespace.matches("hutao").count(); + if no_whitespace.matches("damnhutaomains").count() > 0 { + add_mentions(db, hutao_count).await?; new_message.reply(ctx, "Any last words?").await?; - } else if msg.contains("hutao") || msg.contains("hu tao") { - let mentions = add_mentions(db, 1).await?; - #[cfg(feature = "debug")] - println!("Mentions: {}", mentions); - new_message - .reply(ctx, format!("Hu Tao has been mentioned {} times", mentions)) - .await?; + } else if hutao_count > 0 { + let mentions = add_mentions(db, hutao_count).await?; + let mut reply = format!( + "Hu Tao has been mentioned {} times | +{} times.", + mentions, hutao_count + ); + match hutao_count { + 10..=50 => { + reply.push_str("\n\nEh!? What's with all these mentions!?"); + } + 51..100 => { + reply.push_str("\n\nPlease stoppp~!"); + } + 100.. => { + reply.push_str("\n\nやめて!!"); + } + _ => (), + }; + new_message.reply(ctx, reply).await?; } Ok(())