27 releases (4 breaking)

Uses new Rust 2021

new 0.5.3 Dec 5, 2022
0.5.1 Nov 13, 2022

#289 in Rust patterns

Download history 15/week @ 2022-08-16 18/week @ 2022-08-23 21/week @ 2022-08-30 13/week @ 2022-09-06 28/week @ 2022-09-13 51/week @ 2022-09-20 45/week @ 2022-09-27 246/week @ 2022-10-04 36/week @ 2022-10-11 34/week @ 2022-10-25 148/week @ 2022-11-01 411/week @ 2022-11-08 8/week @ 2022-11-15 1/week @ 2022-11-22

568 downloads per month

MIT license

69KB
2K SLoC

AtriPlugin

crates.io

编写遵循AtriPlugin加载标准的插件

开发示例

Cargo.toml:

[lib]
crate-type = ["cdylib"] # or dylib

[dependencies]
atri_plugin = "0"

首先, 定义一个结构体(struct)或枚举(enum)作为插件的实例

#[atri_plugin::plugin] // 使用此宏标记其为插件
struct MyPlugin {
    listener: Option<ListenerGuard>,
}

为其实现Plugin

use atri_plugin::Plugin;
impl Plugin for MyPlugin {
    fn new() -> Self {
        Self { listener: None }
    }
    fn enable(&mut self) {
        info!("Enable my plugin");

        let guard = Listener::listening_on_always(|e: GroupMessageEvent| async move {
            let message = e.message();
            if message.to_string() == "123" {
                let mut chain = MessageChainBuilder::new();
                chain.push_str("321")
                    .push_str("114514");
                let _ = e.group().send_message(chain).await;
            }
        });
        self.listener = Some(guard);
    }

    fn disable(&mut self) {
        info!("Disable my plugin");
    }
}

也可以为插件实现Drop, 将根据规则释放

最后, 使用cargo build编译得到的动态库即为插件本体

详细文档另请参阅本crate源码

插件依赖

所有的插件依赖都应被放入plugins/dependencies文件夹内, 在加载插件动态库前会先加载此文件夹内所有的动态库文件

Dependencies