2022-08-12 20:32:22 +00:00
|
|
|
use std::any::Any;
|
2022-06-20 21:22:45 +00:00
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
2022-08-12 21:12:29 +00:00
|
|
|
use crate::{plugins::manager::PluginsManager, server::Client};
|
2022-06-20 21:22:45 +00:00
|
|
|
|
2022-08-12 20:32:22 +00:00
|
|
|
// A main plugin trait.
|
2022-06-20 21:22:45 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub trait Plugin: Any + Send + Sync {
|
|
|
|
/// Name of the plugin.
|
|
|
|
fn name(&self) -> &'static str;
|
2022-08-12 20:32:22 +00:00
|
|
|
/// A function that will be executed when the plugin is loaded.
|
|
|
|
async fn on_load(&self);
|
2022-06-20 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 10:33:13 +00:00
|
|
|
/// Add a command to the plugin.
|
2022-06-20 21:22:45 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub trait Command: Any + Send + Sync {
|
|
|
|
/// Name of the command.
|
|
|
|
fn name(&self) -> &'static str;
|
2022-08-12 20:32:22 +00:00
|
|
|
/// Aliases for the command.
|
|
|
|
fn aliases(&self) -> Vec<&'static str>;
|
2022-06-20 21:22:45 +00:00
|
|
|
/// Help message of the command.
|
|
|
|
fn help(&self) -> &'static str;
|
2022-08-12 20:32:22 +00:00
|
|
|
/// Usage message of the command.
|
|
|
|
fn usage(&self) -> &'static str;
|
|
|
|
/// Command function.
|
|
|
|
async fn execute(&self, client: &Client, args: Vec<&str>) -> anyhow::Result<()>;
|
2022-06-20 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 20:32:22 +00:00
|
|
|
/// All possible to run events.
|
|
|
|
pub enum EventType {
|
|
|
|
/// On client connected.
|
|
|
|
OnConnect,
|
|
|
|
/// On client sent message.
|
|
|
|
OnSend,
|
2022-06-20 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 20:32:22 +00:00
|
|
|
/// Add a event to the plugin.
|
|
|
|
#[async_trait]
|
|
|
|
pub trait Event: Any + Send + Sync {
|
|
|
|
/// Type of the event.
|
|
|
|
fn event(&self) -> EventType;
|
|
|
|
/// Event function.
|
|
|
|
async fn execute(&self) -> anyhow::Result<()>;
|
2022-06-20 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Registrar {
|
2022-08-12 20:32:22 +00:00
|
|
|
/// Function to register plugins.
|
|
|
|
fn register_plugins(&mut self, plugin: Box<dyn Plugin>);
|
|
|
|
/// Function to register commands.
|
|
|
|
fn register_commands(&mut self, command: Box<dyn Command>);
|
|
|
|
/// Function to register events.
|
|
|
|
fn register_events(&mut self, event: Box<dyn Event>);
|
2022-06-20 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 20:32:22 +00:00
|
|
|
impl Registrar for PluginsManager {
|
|
|
|
fn register_plugins(&mut self, plugin: Box<dyn Plugin>) {
|
2022-06-20 21:22:45 +00:00
|
|
|
self.plugins.push(plugin)
|
|
|
|
}
|
|
|
|
|
2022-08-12 20:32:22 +00:00
|
|
|
fn register_commands(&mut self, command: Box<dyn Command>) {
|
2022-06-20 21:22:45 +00:00
|
|
|
self.commands.push(command)
|
|
|
|
}
|
|
|
|
|
2022-08-12 20:32:22 +00:00
|
|
|
fn register_events(&mut self, event: Box<dyn Event>) {
|
2022-06-20 21:22:45 +00:00
|
|
|
self.events.push(event)
|
|
|
|
}
|
|
|
|
}
|