Skip to main content

shirabe_core/
plugin.rs

1use crate::context::Context;
2use crate::error::FrameworkResult;
3use async_trait::async_trait;
4use std::fmt::Debug;
5use std::sync::Arc;
6
7// TODO: 完善插件系统
8/// 插件特性,定义了插件的基本行为。
9///
10/// 插件应该能够响应会话事件,并拥有一个唯一的名称。
11#[async_trait]
12pub trait Plugin: Send + Sync + Debug {
13    /// 返回插件的唯一名称。
14    fn name(&self) -> &'static str;
15
16    /// 当新的会话(通常由传入事件触发)创建时调用此方法。
17    ///
18    /// 插件可以检查会话内容并决定是否以及如何响应该事件。
19    ///
20    /// # 参数
21    ///
22    /// * `session` - 一个 `Arc<Session>`,代表当前的事件上下文。
23    ///
24    /// # 返回
25    ///
26    /// 如果处理成功,返回 `Ok(())`,否则返回一个错误。
27    async fn apply(&self, ctx: Arc<Context>) -> FrameworkResult<()>;
28
29    // 插件加载时调用的方法,用于一次性设置。
30    async fn on_load(&self) -> FrameworkResult<()> {
31        Ok(())
32    }
33
34    // 机器人关闭时调用的方法,用于清理工作。
35    async fn on_unload(&self) -> FrameworkResult<()> {
36        Ok(())
37    }
38}