修改了bot代码,基本实现了签到功能,还需实现定时统计

This commit is contained in:
:NoBody: committed 2026-04-05 22:49:47 +08:00
1 parent f86ed8c286
commit d358ed3ddc
15 files changed
+334 -25

No files matched your search

+59 -3
View File
@@ -1,7 +1,9 @@
use crate::bot::command_parser::parse_command;
use crate::bot::define::{AttendanceHandler, Bot, InboundMessage, MessageEnum, OutboundMessage};
use crate::db::crud_handler;
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use std::path::Path;
use std::sync::{Arc, Mutex, OnceLock};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
@@ -11,13 +13,57 @@ type MessageRx = mpsc::Receiver<MessageEnum>;
static GLOBAL_CHANNEL: OnceLock<(MessageTx, Mutex<Option<MessageRx>>)> = OnceLock::new();
pub fn global_channel() -> &'static (MessageTx, Mutex<Option<MessageRx>>) {
fn global_channel() -> &'static (MessageTx, Mutex<Option<MessageRx>>) {
GLOBAL_CHANNEL.get_or_init(|| {
let (tx, rx) = mpsc::channel(100);
(tx, Mutex::new(Some(rx)))
})
}
pub struct ChannelManager {
tx: MessageTx,
rx: &'static Mutex<Option<MessageRx>>,
filters: Option<Vec<Box<dyn Fn(&MessageEnum) -> MessageEnum + Send + Sync>>>,
}
//channel的包装,提供过滤功能,后续可以考虑增加更多功能,比如广播等
impl ChannelManager {
pub fn new() -> Self {
let (tx, rx) = global_channel();
Self {
tx: tx.clone(),
rx,
filters: None,
}
}
fn filter_message(&self, msg: &MessageEnum) -> MessageEnum {
let mut result_msg = msg.clone();
if let Some(filters) = &self.filters {
for filter in filters {
result_msg = filter(&result_msg);
}
}
result_msg
}
//这里先过滤,在发送,后续可以考虑在接收时过滤
pub async fn send(&self, value: MessageEnum) -> Result<()> {
let tx = self.tx.clone();
let msg = self.filter_message(&value);
tx.send(msg)
.await
.map_err(|e| anyhow!("Failed to send message: {}", e))?;
Ok(())
}
pub fn get_receiver(&self) -> Option<MessageRx> {
self.rx.lock().unwrap().take()
}
}
type SharedBot = Arc<dyn Bot + Send + Sync>;
pub struct CommandHandler {
@@ -62,9 +108,19 @@ async fn start_bot_command_processor(
bot.send_raw_message(&outbound.channel_id, &outbound)
.await?;
}
"checkin"=>{
"checkin" => {
let crud_handler = crud_handler::CrudHandler::new(crate::db::sled_db::SledDB::new(
Path::new("daily_state.sled.db"),
)?);
crud_handler.checkin(&inbound.user_id).await?;
let outbound = OutboundMessage {
channel_id: inbound.channel_id.clone(),
text: format!(" {} 已签到成功", inbound.user_id),
reply_to_event_id: Some(inbound.event_id.clone()),
};
bot.send_raw_message(&outbound.channel_id, &outbound)
.await?;
}
_ => {
return Err(anyhow!("unknown command: {}", command));