Add bot
This commit is contained in:
parent
86050fad15
commit
39ab887b47
4 changed files with 67 additions and 0 deletions
|
|
@ -6,6 +6,7 @@ edition = { workspace = true }
|
|||
[dependencies]
|
||||
cookie = { workspace = true }
|
||||
euphoxide = { path = "../euphoxide" }
|
||||
jiff = { workspace = true }
|
||||
log = { workspace = true }
|
||||
tokio = { workspace = true, features = ["rt"] }
|
||||
tokio-tungstenite = { workspace = true }
|
||||
|
|
|
|||
64
euphoxide-bot/src/bot.rs
Normal file
64
euphoxide-bot/src/bot.rs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use jiff::Timestamp;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::{
|
||||
instance::ServerConfig,
|
||||
instances::{Event, Instances, InstancesConfig},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[non_exhaustive]
|
||||
pub struct BotConfig<S> {
|
||||
pub server: ServerConfig,
|
||||
pub instances: InstancesConfig,
|
||||
pub state: S,
|
||||
}
|
||||
|
||||
impl<S> BotConfig<S> {
|
||||
pub fn with_state<S2>(self, state: S2) -> BotConfig<S2> {
|
||||
BotConfig {
|
||||
server: self.server,
|
||||
instances: self.instances,
|
||||
state,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create(self, event_tx: mpsc::Sender<Event>) -> Bot<S> {
|
||||
Bot::new(self, event_tx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for BotConfig<()> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
server: ServerConfig::default(),
|
||||
instances: InstancesConfig::default(),
|
||||
state: (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Bot<S> {
|
||||
pub server_config: ServerConfig,
|
||||
pub state: Arc<S>,
|
||||
pub instances: Instances,
|
||||
pub start_time: Timestamp,
|
||||
}
|
||||
|
||||
impl<S> Bot<S> {
|
||||
pub fn new(config: BotConfig<S>, event_tx: mpsc::Sender<Event>) -> Self {
|
||||
Self {
|
||||
server_config: config.server,
|
||||
state: Arc::new(config.state),
|
||||
instances: Instances::new(config.instances, event_tx),
|
||||
start_time: Timestamp::now(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_event(&self, event: Event) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
|
@ -134,6 +134,7 @@ impl InstancesTask {
|
|||
async fn on_cmd(&mut self, cmd: Command) {
|
||||
match cmd {
|
||||
Command::GetInstances(tx) => {
|
||||
self.purge_instances(); // Not necessary for correctness
|
||||
let _ = tx.send(self.instances.values().cloned().collect());
|
||||
}
|
||||
Command::AddInstance(config, tx) => {
|
||||
|
|
|
|||
|
|
@ -1,2 +1,3 @@
|
|||
pub mod bot;
|
||||
pub mod instance;
|
||||
pub mod instances;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue