From 818b8748a1791ac72fb8e70c65e3fc0abed7d06c Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 27 Dec 2024 19:05:35 +0100 Subject: [PATCH] Fix Commands not being Send + Sync --- euphoxide-bot/src/command.rs | 48 ++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/euphoxide-bot/src/command.rs b/euphoxide-bot/src/command.rs index 3866384..4999372 100644 --- a/euphoxide-bot/src/command.rs +++ b/euphoxide-bot/src/command.rs @@ -8,13 +8,15 @@ use std::future::Future; use async_trait::async_trait; use euphoxide::{ - api::{Data, Message, MessageId, ParsedPacket, Send, SendEvent, SendReply}, + api::{self, Data, Message, MessageId, ParsedPacket, SendEvent, SendReply}, client::{ conn::ClientConnHandle, state::{Joined, State}, }, }; +use crate::{bot::BotEvent, instance::InstanceEvent}; + #[non_exhaustive] pub struct Context { pub conn: ClientConnHandle, @@ -27,7 +29,7 @@ impl Context { content: S, ) -> euphoxide::Result>> { self.conn - .send(Send { + .send(api::Send { content: content.to_string(), parent: None, }) @@ -45,7 +47,7 @@ impl Context { content: S, ) -> euphoxide::Result>> { self.conn - .send(Send { + .send(api::Send { content: content.to_string(), parent: Some(parent), }) @@ -121,7 +123,7 @@ pub trait Command { } pub struct Commands { - commands: Vec>>, + commands: Vec + Sync + Send>>, } impl Commands { @@ -129,11 +131,11 @@ impl Commands { Self { commands: vec![] } } - pub fn add(&mut self, command: impl Command + 'static) { + pub fn add(&mut self, command: impl Command + Sync + Send + 'static) { self.commands.push(Box::new(command)); } - pub fn then(mut self, command: impl Command + 'static) -> Self { + pub fn then(mut self, command: impl Command + Sync + Send + 'static) -> Self { self.add(command); self } @@ -142,7 +144,7 @@ impl Commands { self.commands.iter().map(|c| c.info(ctx)).collect() } - pub async fn execute( + pub async fn on_packet( &self, conn: ClientConnHandle, state: State, @@ -168,6 +170,38 @@ impl Commands { Ok(Propagate::Yes) } + + pub async fn on_instance_event( + &self, + event: InstanceEvent, + bot: &mut B, + ) -> Result { + if let InstanceEvent::Packet { + conn, + state, + packet, + .. + } = event + { + self.on_packet(conn, state, packet, bot).await + } else { + Ok(Propagate::Yes) + } + } + + pub async fn on_bot_event(&self, event: BotEvent, bot: &mut B) -> Result { + if let BotEvent::Packet { + conn, + state, + packet, + .. + } = event + { + self.on_packet(conn, state, packet, bot).await + } else { + Ok(Propagate::Yes) + } + } } // Has fewer restrictions on generic types than #[derive(Default)].