Skip to content

Commit

Permalink
some fix
Browse files Browse the repository at this point in the history
  • Loading branch information
super1207 committed Dec 21, 2023
1 parent e9023f7 commit bd75af1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
11 changes: 7 additions & 4 deletions src/botconn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use self::{onebot11::OneBot11Connect, onebot115::OneBot115Connect};

#[async_trait]
trait BotConnectTrait:Send + Sync {
fn get_platform(&self) -> Vec<String>;
async fn call_api(&self,platform:&str,self_id:&str,json:&mut serde_json::Value) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync>>;
fn get_self_id(&self) -> Vec<String>;
fn get_platform_and_self_id(&self) -> Vec<(String,String)>;
fn get_url(&self) -> String;
fn get_alive(&self) -> bool;
async fn connect(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
Expand All @@ -34,8 +33,12 @@ pub async fn call_api(platform:&str,self_id:&str,json:&mut serde_json::Value) ->
let self_id_t = self_id.to_owned();
// 挑选出对应的bot
for bot in &*G_BOT_MAP.read().await {
if bot.1.read().await.get_platform().contains(&platform_t) && bot.1.read().await.get_self_id().contains(&self_id_t) {
bot_select = Some(bot.1.clone());
let platform_and_self_id = bot.1.read().await.get_platform_and_self_id();
for (platform,self_id) in platform_and_self_id {
if platform == platform_t && self_id == self_id_t {
bot_select = Some(bot.1.clone());
break;
}
}
}
// 使用挑选出来的bot发送消息
Expand Down
11 changes: 3 additions & 8 deletions src/botconn/onebot11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct OneBot11Connect {
pub self_id:Arc<std::sync::RwLock<String>>,
pub url:String,
pub tx:Option<tokio::sync::mpsc::Sender<serde_json::Value>>,
pub platforms:Vec<String>,
pub is_stop:Arc<AtomicBool>,
pub stop_tx :Option<tokio::sync::mpsc::Sender<bool>>,
}
Expand Down Expand Up @@ -67,7 +66,6 @@ impl OneBot11Connect {
self_id:Arc::new(RwLock::new("".to_owned())),
url:url.to_owned(),
tx:None,
platforms:vec!["onebot11".to_string()],
is_stop:Arc::new(AtomicBool::new(false)),
stop_tx: None

Expand Down Expand Up @@ -239,10 +237,6 @@ impl BotConnectTrait for OneBot11Connect {
Ok(())
}

fn get_platform(&self) -> Vec<String> {
return self.platforms.clone();
}

fn get_url(&self) -> String {
return self.url.clone();
}
Expand Down Expand Up @@ -304,9 +298,10 @@ impl BotConnectTrait for OneBot11Connect {
}
}

fn get_self_id(&self) -> Vec<String> {
fn get_platform_and_self_id(&self) -> Vec<(String, String)> {
let lk = self.self_id.read().unwrap();
let self_id = (*lk).clone();
return vec![self_id];
let platform = "onebot11".to_owned();
return vec![(platform,self_id)];
}
}
33 changes: 14 additions & 19 deletions src/botconn/onebot115.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{sync::{atomic::AtomicBool, Arc, RwLock}, collections::HashSet, str::FromStr};
use std::{sync::{atomic::AtomicBool, Arc, RwLock}, str::FromStr};

use async_trait::async_trait;
use futures_util::{StreamExt, SinkExt};
Expand All @@ -11,10 +11,9 @@ use super::BotConnectTrait;

#[derive(Debug)]
pub struct OneBot115Connect {
pub self_ids:Arc<std::sync::RwLock<HashSet<String>>>,
pub url:String,
pub tx:Option<tokio::sync::mpsc::Sender<serde_json::Value>>,
pub platforms:Arc<std::sync::RwLock<HashSet<String>>>,
pub platforms:Arc<std::sync::RwLock<Vec<(String,String)>>>,
pub is_stop:Arc<AtomicBool>,
pub stop_tx :Option<tokio::sync::mpsc::Sender<bool>>,
}
Expand Down Expand Up @@ -70,10 +69,9 @@ fn get_json_dat(msg:Result<hyper_tungstenite::tungstenite::Message, hyper_tungst
impl OneBot115Connect {
pub fn build(url:&str) -> Self {
OneBot115Connect {
self_ids:Arc::new(RwLock::new(HashSet::new())),
url:url.to_owned(),
tx:None,
platforms:Arc::new(RwLock::new(HashSet::new())),
platforms:Arc::new(RwLock::new(Vec::new())),
is_stop:Arc::new(AtomicBool::new(false)),
stop_tx: None

Expand Down Expand Up @@ -109,12 +107,9 @@ impl BotConnectTrait for OneBot115Connect {
cq_add_log_w(&format!("收到数据:{}",myself.to_string())).unwrap();
let bot_arr = myself.get("data").ok_or("data not found")?.as_array().ok_or("data not array")?;
for bot in bot_arr {
if let Some(self_id) = bot.get("bot_id") {
self.self_ids.write().unwrap().insert(self_id.as_str().ok_or("self_id not string")?.to_string());
}
if let Some(platform) = bot.get("platform") {
self.platforms.write().unwrap().insert(platform.as_str().ok_or("platform not string")?.to_string());
}
let platform = bot.get("platform").ok_or("platform not found")?.as_str().ok_or("platform not str")?;
let bot_id = bot.get("bot_id").ok_or("bot_id not found")?.as_str().ok_or("bot_id not str")?;
self.platforms.write().unwrap().push((platform.to_string(),bot_id.to_string()));
}
}
let url_ws = self.url.replacen("ovo://", "ws://", 1);
Expand Down Expand Up @@ -225,10 +220,10 @@ impl BotConnectTrait for OneBot115Connect {
Ok(())
}

fn get_platform(&self) -> Vec<String> {
let ret_vec = self.platforms.read().unwrap().iter().map(|x| x.to_string()).collect();
return ret_vec;
}
// fn get_platform(&self) -> Vec<String> {
// let ret_vec = self.platforms.read().unwrap().iter().map(|x| x.to_string()).collect();
// return ret_vec;
// }

fn get_url(&self) -> String {
return self.url.clone();
Expand Down Expand Up @@ -257,9 +252,9 @@ impl BotConnectTrait for OneBot115Connect {
return Ok(ret)
}

fn get_self_id(&self) -> Vec<String> {
let lk = self.self_ids.read().unwrap();
let self_ids = (*lk).clone();
return self_ids.iter().map(|x| x.to_string()).collect();
fn get_platform_and_self_id(&self) -> Vec<(String,String)> {
let lk = self.platforms.read().unwrap();
let platforms = (*lk).clone();
return platforms;
}
}

0 comments on commit bd75af1

Please sign in to comment.