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 c899ebd commit e9023f7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 19 deletions.
8 changes: 5 additions & 3 deletions src/botconn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ lazy_static! {

pub async fn call_api(platform:&str,self_id:&str,json:&mut serde_json::Value) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync>> {
let mut bot_select = None;
let platform_t = platform.to_owned();
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.to_owned()) && bot.1.read().await.get_self_id().contains(&self_id.to_owned()) {
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());
}
}
// 使用挑选出来的bot发送消息
if bot_select.is_some() {
return bot_select.unwrap().read().await.call_api(platform, self_id, json).await;
return bot_select.unwrap().read().await.call_api(&platform_t, &self_id_t, json).await;
}
cq_add_log_w(&format!("no such bot:{platform},{self_id}")).unwrap();
cq_add_log_w(&format!("no such bot:platform:`{platform}`,self_id:`{self_id}`")).unwrap();
return Ok(serde_json::json!(""));
}

Expand Down
14 changes: 1 addition & 13 deletions src/botconn/onebot115.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ 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("self_id") {
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") {
Expand All @@ -134,7 +134,6 @@ impl BotConnectTrait for OneBot115Connect {
self.stop_tx = Some(stoptx);

// 这里使用弱引用,防止可能的循环依赖
let self_id_ptr = Arc::<std::sync::RwLock<HashSet<String>>>::downgrade(&self.self_ids);
let is_stop = Arc::<AtomicBool>::downgrade(&self.is_stop);
tokio::spawn(async move {
loop {
Expand All @@ -155,18 +154,7 @@ impl BotConnectTrait for OneBot115Connect {
continue;
}

// 设置self_id
let self_id = read_json_str(&json_dat, "self_id");
if self_id != "" {
if let Some(val) = self_id_ptr.upgrade() {
val.write().unwrap().insert(self_id);
}
else{
break;
}
}
// 获得echo
//let echo = read_json_str(&json_dat, "echo").to_owned();
let post_type = read_json_str(&json_dat, "post_type").to_owned();
tokio::spawn(async move {
if post_type == "" { // 是api回复
Expand Down
3 changes: 0 additions & 3 deletions src/redlang/cqexfun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ pub fn get_app_dir(pkg_name:&str) -> Result<String, Box<dyn std::error::Error>>

fn get_platform(self_t:&RedLang) -> String{
let platform = self_t.get_exmap("机器人平台");
if *platform == "" {
return "onebot11".to_string();
}
return (*platform).to_owned();
}

Expand Down

0 comments on commit e9023f7

Please sign in to comment.