Skip to content

Commit

Permalink
modified: Cargo.toml
Browse files Browse the repository at this point in the history
	modified:   src/bot.rs
	modified:   src/bot/handler.rs
	modified:   src/bot/plugin_builder.rs
	modified:   src/bot/plugin_builder/event.rs
	modified:   src/bot/plugin_builder/event/msg_event.rs
  • Loading branch information
Threkork committed Aug 31, 2024
1 parent 69de6e6 commit f2df99d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kovi"
version = "0.7.0"
version = "0.7.1"
edition = "2021"
description = "A OneBot V11 bot plugin framework"
license = "MPL-2.0"
Expand Down
4 changes: 2 additions & 2 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl Bot {
Ok(v) => v,
Err(e) => {
exit_and_eprintln(e, event_tx);
panic!("connect error")
return;
}
};

Expand Down Expand Up @@ -332,7 +332,7 @@ impl Bot {
Ok(v) => v,
Err(e) => {
exit_and_eprintln(e, event_tx);
panic!("connect error")
return;
}
};

Expand Down
49 changes: 34 additions & 15 deletions src/bot/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use plugin_builder::{
ListenFn,
};
use serde_json::{json, Value};
use std::sync::{mpsc, Arc, RwLock};
use std::{
sync::{mpsc, Arc, RwLock},
thread,
};


/// Kovi内部事件
Expand Down Expand Up @@ -166,23 +169,30 @@ impl Bot {
async fn handle_msg(listen: ListenFn, e: Arc<AllMsgEvent>, bot: Arc<RwLock<Bot>>) {
match listen {
ListenFn::MsgFn(handler) => {
handler(&e);
let e_clone = Arc::clone(&e);
thread::spawn(move || {
handler(&e_clone);
});
}
ListenFn::MsgAsyncFn(handler) => {
handler(e).await;
}

ListenFn::AdminMsgFn(handler) => {
let user_id = e.user_id;
let admin_vec = {
let bot = bot.read().unwrap();
let mut admin_vec = bot.information.admin.clone();
admin_vec.push(bot.information.main_admin);
admin_vec
};
if admin_vec.contains(&user_id) {
handler(&e);
}
let e_clone = Arc::clone(&e);
let bot_clone = Arc::clone(&bot);
thread::spawn(move || {
let user_id = e_clone.user_id;
let admin_vec = {
let bot = bot_clone.read().unwrap();
let mut admin_vec = bot.information.admin.clone();
admin_vec.push(bot.information.main_admin);
admin_vec
};
if admin_vec.contains(&user_id) {
handler(&e_clone);
}
});
}
ListenFn::AdminMsgAsyncFn(handler) => {
let user_id = e.user_id;
Expand All @@ -203,7 +213,10 @@ async fn handle_msg(listen: ListenFn, e: Arc<AllMsgEvent>, bot: Arc<RwLock<Bot>>
async fn handler_notice(listen: ListenFn, e: Arc<AllNoticeEvent>) {
match listen {
ListenFn::AllNoticeFn(handler) => {
handler(&e);
let e_clone = Arc::clone(&e);
thread::spawn(move || {
handler(&e_clone);
});
}
ListenFn::AllNoticeAsyncFn(handler) => {
handler(e).await;
Expand All @@ -215,7 +228,10 @@ async fn handler_notice(listen: ListenFn, e: Arc<AllNoticeEvent>) {
async fn handler_request(listen: ListenFn, e: Arc<AllRequestEvent>) {
match listen {
ListenFn::AllRequestFn(handler) => {
handler(&e);
let e_clone = Arc::clone(&e);
thread::spawn(move || {
handler(&e_clone);
});
}
ListenFn::AllRequestAsyncFn(handler) => {
handler(e).await;
Expand All @@ -228,7 +244,10 @@ async fn handler_kovi_drop(listen: ListenFn) {
match listen {
ListenFn::KoviEventDropFn(handler) => {
info!("A plugin is performing its shutdown tasks, please wait. 有插件正在进行结束工作,请稍候。");
handler();
let join = thread::spawn(move || {
handler();
});
join.join().unwrap();
}
ListenFn::KoviEventDropAsyncFn(handler) => {
info!("A plugin is performing its shutdown tasks, please wait. 有插件正在进行结束工作,请稍候。");
Expand Down
2 changes: 1 addition & 1 deletion src/bot/plugin_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl PluginBuilder {

let all_listen = bot.plugins.get_mut(&self.name).unwrap();

all_listen.push(ListenFn::MsgAsyncFn(Arc::new(move |event| {
all_listen.push(ListenFn::AdminMsgAsyncFn(Arc::new(move |event| {
Box::pin(handler(Pin::new(event)))
})));
}
Expand Down
6 changes: 3 additions & 3 deletions src/bot/plugin_builder/event.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
pub use msg_event::AllMsgEvent;
use serde::{Deserialize, Serialize};
use serde::{ Deserialize, Serialize};
use serde_json::{self, Value};

mod msg_event;


#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub enum Sex {
Male,
Female,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[derive(Debug, Clone)]
pub struct Sender {
pub user_id: i64,
pub nickname: Option<String>,
Expand Down
2 changes: 0 additions & 2 deletions src/bot/plugin_builder/event/msg_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ impl AllMsgEvent {
) -> Result<AllMsgEvent, Box<dyn std::error::Error>> {
let temp: Value = serde_json::from_str(msg)?;


let temp_object = temp.as_object().unwrap();


let temp_sender = temp_object["sender"].as_object().unwrap();

let sender = {
Expand Down

0 comments on commit f2df99d

Please sign in to comment.