mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2024-11-23 12:41:05 +08:00
试试新的共享方式,虽然略微麻烦,但至少比之前全局共享好
This commit is contained in:
parent
7d2707b35e
commit
9002103c7b
|
@ -1,4 +1,7 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
use crate::data_struct::tailchat::UserId;
|
use crate::data_struct::tailchat::UserId;
|
||||||
|
|
||||||
|
@ -29,3 +32,31 @@ pub struct UpdateDMConverse {
|
||||||
#[serde(rename = "updatedAt")]
|
#[serde(rename = "updatedAt")]
|
||||||
pub updated_at: String,
|
pub updated_at: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type Writeable<T> = Arc<RwLock<T>>;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct BotStatus {
|
||||||
|
user_id: Writeable<UserId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BotStatus {
|
||||||
|
pub fn new(user_id: UserId) -> Self {
|
||||||
|
Self {
|
||||||
|
user_id: Arc::new(RwLock::new(user_id)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_user_id(&self) -> UserId { self.user_id.read().await.clone() }
|
||||||
|
|
||||||
|
pub fn block_get_user_id(&self) -> UserId {
|
||||||
|
tokio::task::block_in_place(|| {
|
||||||
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
|
rt.block_on(self.get_user_id())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_user_id(&self, user_id: UserId) {
|
||||||
|
self.user_id.write().await.clone_from(&user_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user