kovi-onebot 0.13.0

OneBot V11 protocol driver for Kovi bot framework
Documentation
use crate::event::PostType;
use kovi::bot::BotInformation;
use kovi::error::EventBuildError;
use kovi::event::{Event, InternalEvent};
use kovi::types::ApiAndOptOneshot;
use serde_json::Value;
use serde_json::value::Index;
use tokio::sync::mpsc;

#[derive(Debug, Clone)]
pub struct RequestEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 登陆号
    pub self_id: i64,
    /// 上报类型
    pub post_type: PostType,
    /// 请求类型
    pub request_type: String,

    /// 原始的onebot消息,已处理成json格式
    pub original_json: Value,
}
impl Event for RequestEvent {
    fn de(
        event: &InternalEvent,
        _: &BotInformation,
        _: &mpsc::Sender<ApiAndOptOneshot>,
    ) -> Option<Self> {
        let InternalEvent::DriverEvent(json) = event else {
            return None;
        };

        Self::new(json).ok()
    }
}

impl RequestEvent {
    pub(crate) fn new(temp: &Value) -> Result<RequestEvent, EventBuildError> {
        let time = temp
            .get("time")
            .and_then(Value::as_i64)
            .ok_or(EventBuildError::ParseError("time".to_string()))?;
        let self_id = temp
            .get("self_id")
            .and_then(Value::as_i64)
            .ok_or(EventBuildError::ParseError("self_id".to_string()))?;
        let post_type = temp
            .get("post_type")
            .and_then(|v| serde_json::from_value::<PostType>(v.clone()).ok())
            .ok_or(EventBuildError::ParseError("Invalid post_type".to_string()))?;
        let request_type = temp
            .get("request_type")
            .and_then(Value::as_str)
            .map(String::from)
            .ok_or(EventBuildError::ParseError("request_type".to_string()))?;
        Ok(RequestEvent {
            time,
            self_id,
            post_type,
            request_type,
            original_json: temp.clone(),
        })
    }
}

impl RequestEvent {
    /// 直接从原始的 Json Value 获取某值
    ///
    /// # example
    ///
    /// ```ignore
    /// use kovi::PluginBuilder;
    ///
    /// PluginBuilder::on_request(|event| async move {
    ///     let time = event.get("time").and_then(|v| v.as_i64()).unwrap();
    ///
    ///     assert_eq!(time, event.time);
    /// });
    /// ```
    pub fn get<I: Index>(&self, index: I) -> Option<&Value> {
        self.original_json.get(index)
    }
}

impl<I> std::ops::Index<I> for RequestEvent
where
    I: Index,
{
    type Output = Value;

    fn index(&self, index: I) -> &Self::Output {
        &self.original_json[index]
    }
}