Skip to content

Commit

Permalink
feat(onebot): store receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed Sep 26, 2024
1 parent 6a4a1f5 commit c4e73f4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
11 changes: 5 additions & 6 deletions crates/aionbot-adapter-onebot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ impl Adapter for aionbot_core::event::Event {
pub struct OnebotRuntime {
onebot: Option<Arc<Onebot>>,
state: Arc<StateManager>,
receiver: Option<Receiver<Event>>,
}

impl Default for OnebotRuntime {
fn default() -> Self {
Self {
onebot: None,
state: Arc::new(StateManager::default()),
receiver: None,
}
}
}
Expand All @@ -60,15 +62,12 @@ impl Runtime for OnebotRuntime {
}

async fn finalize(&mut self) -> Result<()> {
let mut rx = self.onebot.as_ref().cloned().unwrap().subscribe().await;
loop {
let event = rx.recv().await?;
println!("Received event: {:?}", event);
}
self.receiver = Some(self.onebot.as_ref().cloned().unwrap().subscribe().await);
Ok(())
}

async fn run(&self) -> Result<()> {
async fn run(&mut self) -> Result<()> {
self.receiver.as_mut().unwrap().recv().await?;
Ok(())
}
}
7 changes: 4 additions & 3 deletions crates/aionbot-adapter-onebot/src/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use tokio_tungstenite::{
pub struct Config {
host: String,
port: u16,
path: String,
access_token: Option<String>,
pub path: String,
pub access_token: Option<String>,
}

impl Default for Config {
Expand Down Expand Up @@ -76,6 +76,7 @@ impl Onebot {
.get("X-Self-ID")
.map(|id| id.to_str().unwrap().to_string())
.unwrap_or_default();
println!("New bot connection: {}", bot_id);
sender.send(Default::default()).unwrap();
Ok(response)
})
Expand All @@ -85,7 +86,7 @@ impl Onebot {
let value = sender.clone();
async move {
println!("Received message: {:?}", &message);
value.send(Default::default());
value.send(Default::default()).unwrap();
}
})
.await;
Expand Down
2 changes: 1 addition & 1 deletion crates/aionbot-core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@ pub trait Runtime {
async move { Ok(()) }
}

fn run(&self) -> impl std::future::Future<Output = Result<()>> + Send;
fn run(&mut self) -> impl std::future::Future<Output = Result<()>> + Send;
}

0 comments on commit c4e73f4

Please sign in to comment.