Skip to content

Commit

Permalink
Improved the mentioning counting
Browse files Browse the repository at this point in the history
  • Loading branch information
1Git2Clone committed Dec 15, 2024
1 parent 329cbc9 commit 9fbbac8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
15 changes: 9 additions & 6 deletions src/database/bot_mentions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sqlx::sqlite::SqliteQueryResult;
use sqlx::Row;
use sqlx::SqlitePool;

pub async fn fetch_mentions(db: &SqlitePool) -> Result<i64, Error> {
pub async fn fetch_mentions(db: &SqlitePool) -> Result<usize, Error> {
let query = format!(
"SELECT `{}` FROM `{}`",
MENTIONS_TABLE[&MentionsSchema::Mentions],
Expand All @@ -18,25 +18,28 @@ pub async fn fetch_mentions(db: &SqlitePool) -> Result<i64, Error> {
None => return Err(format!("Couldn't find a row to select (SQL: {})", query).into()),
};

let queried_mentions = row.get::<i64, &str>(MENTIONS_TABLE[&MentionsSchema::Mentions]);
let queried_mentions = row.get::<u64, &str>(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<SqliteQueryResult, sqlx::Error> {
let query = format!(
"UPDATE `{}` SET `{}` = ?",
MENTIONS_TABLE_NAME,
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<i64, Error> {
pub async fn add_mentions(db: &SqlitePool, n: usize) -> Result<usize, Error> {
let fetched_mentions = fetch_mentions(db).await?;

update_mentions(db, fetched_mentions + n).await?;
Expand Down
3 changes: 1 addition & 2 deletions src/event_handler/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 24 additions & 12 deletions src/utils/replies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<&str>>()
.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(())
Expand Down

0 comments on commit 9fbbac8

Please sign in to comment.