From 3bc14ce34338c63ea8a2e14678a57d38d373f215 Mon Sep 17 00:00:00 2001 From: Sense T Date: Sat, 13 Jan 2024 17:15:54 +0800 Subject: [PATCH] bug fix for bot channel etc. --- src/commands.rs | 14 ++++++++++- src/db_controller.rs | 60 ++++++++++++++++++-------------------------- src/messages.rs | 6 ++++- 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 285625b..e9ebffa 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -18,7 +18,7 @@ use crate::{ messages::{ BOT_ABOUT, BOT_TEXT_HANGED, BOT_TEXT_HANGED_SELF, BOT_TEXT_IS_CHANNEL, BOT_TEXT_NO_TARGET, BOT_TEXT_TOP_GLOBAL, BOT_TEXT_TOP_GROUP, BOT_TEXT_TOP_NONE, BOT_TEXT_TOP_TEMPLATE, - BOT_TEXT_TOP_TITLE, + BOT_TEXT_TOP_TITLE, BOT_TEXT_HANG_BOT, BOT_TEXT_HANG_ANONYMOUS, BOT_TEXT_HANG_CHANNEL, }, }; @@ -106,6 +106,18 @@ impl CommandHandler { match reply.from() { Some(user) => { + if user.is_bot { + return self.send_text_reply(bot, reply, BOT_TEXT_HANG_BOT.to_string()).await + } + + if user.is_anonymous() { + return self.send_text_reply(bot, reply, BOT_TEXT_HANG_ANONYMOUS.to_string()).await + } + + if user.is_channel() { + return self.send_text_reply(bot, reply, BOT_TEXT_HANG_CHANNEL.to_string()).await + } + let is_self = match message.from() { Some(f) => f.first_name == user.first_name, None => false, diff --git a/src/db_controller.rs b/src/db_controller.rs index e5b7014..72924a7 100644 --- a/src/db_controller.rs +++ b/src/db_controller.rs @@ -1,12 +1,12 @@ use migration::{Migrator, MigratorTrait}; use models::prelude::*; use sea_orm::{ - ActiveModelTrait, ColumnTrait, ConnectionTrait, Database, DatabaseConnection, - DbErr, EntityTrait, QueryFilter, QueryOrder, QuerySelect, QueryTrait, Set, TransactionTrait, FromQueryResult, prelude::BigDecimal, + prelude::BigDecimal, ActiveModelTrait, ColumnTrait, ConnectionTrait, Database, + DatabaseConnection, DbErr, EntityTrait, FromQueryResult, QueryFilter, QueryOrder, QuerySelect, + QueryTrait, Set, TransactionTrait, }; use teloxide::types::{Chat, ChatId}; -use wd_log::{log_debug_ln, log_error_ln, log_info_ln, log_warn_ln}; - +use wd_log::{log_debug_ln, log_error_ln, log_info_ln}; #[derive(Debug, FromQueryResult)] pub struct TopData { @@ -14,7 +14,6 @@ pub struct TopData { pub counts: BigDecimal, } - #[derive(Debug, Clone)] pub struct Controller { db: DatabaseConnection, @@ -31,8 +30,9 @@ impl Controller { /// Do migrate pub async fn migrate(&self) -> Result<(), DbErr> { if let Err(err) = Migrator::install(&self.db).await { - log_warn_ln!("{}", err) + return Err(err); } + if let Err(err) = Migrator::up(&self.db, None).await { Err(err) } else { @@ -81,38 +81,28 @@ impl Controller { } }; + let query = Stats::find() + .select_only() + .column(StatsColumn::Name) + .column_as(StatsColumn::Counts.sum(), "counts") + .group_by(StatsColumn::Name) + .order_by_desc(StatsColumn::Counts.sum()) + .limit(LIMIT); + let query = match chat.is_group() || chat.is_supergroup() { - true => { - Stats::find().select_only() - .column(StatsColumn::Name) - .column_as(StatsColumn::Counts.sum(), "counts") - .filter(StatsColumn::GroupId.eq(chat.id.0)) - .group_by(StatsColumn::Name) - .order_by_desc(StatsColumn::Counts.sum()) - .limit(LIMIT).into_model() - .all(&transcation) - .await - } - false => { - let query = Stats::find() - .select_only() - .column(StatsColumn::Name) - .column_as(StatsColumn::Counts.sum(), "counts") - .group_by(StatsColumn::Name) - .order_by_desc(StatsColumn::Counts.sum()) - .limit(LIMIT); - - log_debug_ln!( - "SQL: {:?}", - query.build(transcation.get_database_backend()).to_string() - ); - - query.into_model().all(&transcation).await - } + true => query.filter(StatsColumn::GroupId.eq(chat.id.0)), + false => query, }; - match query { - Ok(query) => Some(query), + log_debug_ln!( + "SQL: {:?}", + query.build(transcation.get_database_backend()).to_string() + ); + + let result = query.into_model().all(&transcation).await; + + match result { + Ok(result) => Some(result), Err(error) => { log_error_ln!("{}", error); None diff --git a/src/messages.rs b/src/messages.rs index f5a5f17..25bebb4 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -33,4 +33,8 @@ pub const BOT_TEXT_HANGED_SELF: [&str; 3] = [ BOT_TEXT_HANGED_SELF_1, BOT_TEXT_HANGED_SELF_2, BOT_TEXT_HANGED_SELF_3, -]; \ No newline at end of file +]; + +pub const BOT_TEXT_HANG_BOT: &'static str = "机器人是无法被吊死的……"; +pub const BOT_TEXT_HANG_CHANNEL: &'static str = "这是个频道……"; +pub const BOT_TEXT_HANG_ANONYMOUS: &'static str = "这是个幽灵……"; \ No newline at end of file