bug fix for bot channel etc.

This commit is contained in:
Sense T 2024-01-13 17:15:54 +08:00
parent e02bca8ad2
commit 3bc14ce343
3 changed files with 43 additions and 37 deletions

View File

@ -18,7 +18,7 @@ use crate::{
messages::{ messages::{
BOT_ABOUT, BOT_TEXT_HANGED, BOT_TEXT_HANGED_SELF, BOT_TEXT_IS_CHANNEL, BOT_TEXT_NO_TARGET, 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_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() { match reply.from() {
Some(user) => { 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() { let is_self = match message.from() {
Some(f) => f.first_name == user.first_name, Some(f) => f.first_name == user.first_name,
None => false, None => false,

View File

@ -1,12 +1,12 @@
use migration::{Migrator, MigratorTrait}; use migration::{Migrator, MigratorTrait};
use models::prelude::*; use models::prelude::*;
use sea_orm::{ use sea_orm::{
ActiveModelTrait, ColumnTrait, ConnectionTrait, Database, DatabaseConnection, prelude::BigDecimal, ActiveModelTrait, ColumnTrait, ConnectionTrait, Database,
DbErr, EntityTrait, QueryFilter, QueryOrder, QuerySelect, QueryTrait, Set, TransactionTrait, FromQueryResult, prelude::BigDecimal, DatabaseConnection, DbErr, EntityTrait, FromQueryResult, QueryFilter, QueryOrder, QuerySelect,
QueryTrait, Set, TransactionTrait,
}; };
use teloxide::types::{Chat, ChatId}; 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)] #[derive(Debug, FromQueryResult)]
pub struct TopData { pub struct TopData {
@ -14,7 +14,6 @@ pub struct TopData {
pub counts: BigDecimal, pub counts: BigDecimal,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Controller { pub struct Controller {
db: DatabaseConnection, db: DatabaseConnection,
@ -31,8 +30,9 @@ impl Controller {
/// Do migrate /// Do migrate
pub async fn migrate(&self) -> Result<(), DbErr> { pub async fn migrate(&self) -> Result<(), DbErr> {
if let Err(err) = Migrator::install(&self.db).await { 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 { if let Err(err) = Migrator::up(&self.db, None).await {
Err(err) Err(err)
} else { } 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() { let query = match chat.is_group() || chat.is_supergroup() {
true => { true => query.filter(StatsColumn::GroupId.eq(chat.id.0)),
Stats::find().select_only() false => query,
.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
}
}; };
match query { log_debug_ln!(
Ok(query) => Some(query), "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) => { Err(error) => {
log_error_ln!("{}", error); log_error_ln!("{}", error);
None None

View File

@ -34,3 +34,7 @@ pub const BOT_TEXT_HANGED_SELF: [&str; 3] = [
BOT_TEXT_HANGED_SELF_2, BOT_TEXT_HANGED_SELF_2,
BOT_TEXT_HANGED_SELF_3, BOT_TEXT_HANGED_SELF_3,
]; ];
pub const BOT_TEXT_HANG_BOT: &'static str = "机器人是无法被吊死的……";
pub const BOT_TEXT_HANG_CHANNEL: &'static str = "这是个频道……";
pub const BOT_TEXT_HANG_ANONYMOUS: &'static str = "这是个幽灵……";