servers/plugin_test/src/lib.rs

64 lines
1.5 KiB
Rust
Raw Normal View History

2022-06-04 18:10:21 +00:00
use async_trait::async_trait;
2022-06-04 19:34:53 +00:00
use servers::{
plugins::{Command, Event, Plugin, PluginManagerType, Registrar},
tcp::Client,
2022-06-04 19:34:53 +00:00
};
2022-06-04 18:10:21 +00:00
struct PluginTest;
/// Create a new plugin.
2022-06-04 18:10:21 +00:00
#[async_trait]
impl Plugin for PluginTest {
/// Name of the plugin.
2022-06-04 18:10:21 +00:00
fn name(&self) -> &'static str {
"test"
}
/// A function will be executed when plugin loading.
/// Usally used for initialization.
2022-06-04 18:10:21 +00:00
async fn on_plugin_load(&self) {}
}
/// Create a new command.
2022-06-04 18:10:21 +00:00
#[async_trait]
impl Command for PluginTest {
/// Command name
2022-06-04 18:10:21 +00:00
fn name(&self) -> &'static str {
"/test"
}
/// Help message of the command
2022-06-04 18:10:21 +00:00
fn help(&self) -> &'static str {
"test command"
}
/// Command function
async fn execute(&self, client: &mut Client, _args: Vec<&str>, _commands: &PluginManagerType) {
client.send("content").expect("send message")
2022-06-04 18:10:21 +00:00
}
}
/// Create a new event
2022-06-16 17:31:09 +00:00
#[async_trait]
impl Event for PluginTest {
/// Event name (onConnect or onSend)
2022-06-16 17:31:09 +00:00
fn name(&self) -> &'static str {
"onConnect"
}
/// Event function
2022-06-16 17:31:09 +00:00
async fn execute(&self, client: &mut Client) {
client
.send(&format!("Welcome {}", client.stream.peer_addr().unwrap()))
.expect("send message")
}
}
/// Regsiter plugin
2022-06-04 18:10:21 +00:00
#[no_mangle]
pub fn plugin_entry(registrar: &mut dyn Registrar) {
registrar.register_plugin(Box::new(PluginTest));
registrar.register_command(Box::new(PluginTest));
registrar.register_event(Box::new(PluginTest));
2022-06-04 18:10:21 +00:00
}