Create euphoxide-bot crate
This commit is contained in:
parent
42f5c9cb21
commit
39b6c341fa
13 changed files with 1222 additions and 1 deletions
73
euphoxide-bot/src/bot.rs
Normal file
73
euphoxide-bot/src/bot.rs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
use std::{fmt::Debug, sync::Arc};
|
||||
|
||||
use euphoxide_client::{MultiClient, MultiClientConfig, MultiClientEvent, ServerConfig};
|
||||
use jiff::Timestamp;
|
||||
use log::error;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::command::Commands;
|
||||
|
||||
#[non_exhaustive]
|
||||
pub struct Bot<S = (), E = euphoxide::Error> {
|
||||
pub server_config: ServerConfig,
|
||||
pub commands: Arc<Commands<S, E>>,
|
||||
pub state: Arc<S>,
|
||||
pub clients: MultiClient,
|
||||
pub start_time: Timestamp,
|
||||
}
|
||||
|
||||
impl Bot {
|
||||
pub fn new_simple(commands: Commands, event_tx: mpsc::Sender<MultiClientEvent>) -> Self {
|
||||
Self::new(
|
||||
ServerConfig::default(),
|
||||
MultiClientConfig::default(),
|
||||
commands,
|
||||
(),
|
||||
event_tx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, E> Bot<S, E> {
|
||||
pub fn new(
|
||||
server_config: ServerConfig,
|
||||
clients_config: MultiClientConfig,
|
||||
commands: Commands<S, E>,
|
||||
state: S,
|
||||
event_tx: mpsc::Sender<MultiClientEvent>,
|
||||
) -> Self {
|
||||
Self {
|
||||
server_config,
|
||||
commands: Arc::new(commands),
|
||||
state: Arc::new(state),
|
||||
clients: MultiClient::new_with_config(clients_config, event_tx),
|
||||
start_time: Timestamp::now(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<S, E> Bot<S, E>
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
E: Debug + 'static,
|
||||
{
|
||||
pub fn handle_event(&self, event: MultiClientEvent) {
|
||||
let bot = self.clone();
|
||||
tokio::task::spawn(async move {
|
||||
if let Err(err) = bot.commands.on_event(event, &bot).await {
|
||||
error!("while handling event: {err:#?}");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, E> Clone for Bot<S, E> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
server_config: self.server_config.clone(),
|
||||
commands: self.commands.clone(),
|
||||
state: self.state.clone(),
|
||||
clients: self.clients.clone(),
|
||||
start_time: self.start_time,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue