From a6571a51c1f6909bf9b2a7991c4d666dc02e72c0 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 28 Dec 2024 18:50:41 +0100 Subject: [PATCH] Remove generic bot state --- euphoxide-bot/src/bot.rs | 18 ++++++---------- euphoxide-bot/src/command.rs | 18 ++++++++-------- euphoxide-bot/src/command/bang.rs | 21 ++++++++----------- euphoxide-bot/src/command/basic.rs | 14 ++++++------- .../src/command/botrulez/full_help.rs | 12 +++++------ euphoxide-bot/src/command/botrulez/ping.rs | 8 +++---- .../src/command/botrulez/short_help.rs | 8 +++---- euphoxide-bot/src/command/botrulez/uptime.rs | 12 +++++------ euphoxide-bot/src/command/clap.rs | 11 +++++----- 9 files changed, 53 insertions(+), 69 deletions(-) diff --git a/euphoxide-bot/src/bot.rs b/euphoxide-bot/src/bot.rs index ecbf777..3f681c8 100644 --- a/euphoxide-bot/src/bot.rs +++ b/euphoxide-bot/src/bot.rs @@ -8,10 +8,9 @@ use tokio::sync::mpsc; use crate::command::Commands; #[non_exhaustive] -pub struct Bot { +pub struct Bot { pub server_config: ServerConfig, - pub commands: Arc>, - pub state: Arc, + pub commands: Arc>, pub clients: MultiClient, pub start_time: Timestamp, } @@ -22,32 +21,28 @@ impl Bot { ServerConfig::default(), MultiClientConfig::default(), commands, - (), event_tx, ) } } -impl Bot { +impl Bot { pub fn new( server_config: ServerConfig, clients_config: MultiClientConfig, - commands: Commands, - state: S, + commands: Commands, event_tx: mpsc::Sender, ) -> 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 Bot +impl Bot where - S: Send + Sync + 'static, E: Debug + 'static, { pub fn handle_event(&self, event: MultiClientEvent) { @@ -60,12 +55,11 @@ where } } -impl Clone for Bot { +impl Clone for Bot { 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, } diff --git a/euphoxide-bot/src/command.rs b/euphoxide-bot/src/command.rs index e993aac..08863a3 100644 --- a/euphoxide-bot/src/command.rs +++ b/euphoxide-bot/src/command.rs @@ -110,7 +110,7 @@ pub enum Propagate { #[allow(unused_variables)] #[async_trait] -pub trait Command { +pub trait Command { fn info(&self, ctx: &Context) -> Info { Info::default() } @@ -120,24 +120,24 @@ pub trait Command { arg: &str, msg: &Message, ctx: &Context, - bot: &Bot, + bot: &Bot, ) -> Result; } -pub struct Commands { - commands: Vec + Sync + Send>>, +pub struct Commands { + commands: Vec + Sync + Send>>, } -impl Commands { +impl Commands { pub fn new() -> Self { Self { commands: vec![] } } - pub fn add(&mut self, command: impl Command + Sync + Send + '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 + Sync + Send + 'static) -> Self { + pub fn then(mut self, command: impl Command + Sync + Send + 'static) -> Self { self.add(command); self } @@ -149,7 +149,7 @@ impl Commands { pub(crate) async fn on_event( &self, event: MultiClientEvent, - bot: &Bot, + bot: &Bot, ) -> Result { let MultiClientEvent::Packet { client, @@ -187,7 +187,7 @@ impl Commands { } // Has fewer restrictions on generic types than #[derive(Default)]. -impl Default for Commands { +impl Default for Commands { fn default() -> Self { Self::new() } diff --git a/euphoxide-bot/src/command/bang.rs b/euphoxide-bot/src/command/bang.rs index 8e87952..021c4fd 100644 --- a/euphoxide-bot/src/command/bang.rs +++ b/euphoxide-bot/src/command/bang.rs @@ -47,10 +47,9 @@ impl Global { } #[async_trait] -impl Command for Global +impl Command for Global where - S: Send + Sync, - C: Command + Sync, + C: Command + Sync, { fn info(&self, ctx: &Context) -> Info { self.inner @@ -63,7 +62,7 @@ where arg: &str, msg: &Message, ctx: &Context, - bot: &Bot, + bot: &Bot, ) -> Result { let Some((name, rest)) = parse_prefix_initiated(arg, &self.prefix) else { return Ok(Propagate::Yes); @@ -99,10 +98,9 @@ impl General { } #[async_trait] -impl Command for General +impl Command for General where - S: Send + Sync, - C: Command + Sync, + C: Command + Sync, { fn info(&self, ctx: &Context) -> Info { self.inner @@ -115,7 +113,7 @@ where arg: &str, msg: &Message, ctx: &Context, - bot: &Bot, + bot: &Bot, ) -> Result { let Some((name, rest)) = parse_prefix_initiated(arg, &self.prefix) else { return Ok(Propagate::Yes); @@ -158,10 +156,9 @@ impl Specific { } #[async_trait] -impl Command for Specific +impl Command for Specific where - S: Send + Sync, - C: Command + Sync, + C: Command + Sync, { fn info(&self, ctx: &Context) -> Info { let nick = nick::mention(&ctx.joined.session.name); @@ -175,7 +172,7 @@ where arg: &str, msg: &Message, ctx: &Context, - bot: &Bot, + bot: &Bot, ) -> Result { let Some((name, rest)) = parse_prefix_initiated(arg, &self.prefix) else { return Ok(Propagate::Yes); diff --git a/euphoxide-bot/src/command/basic.rs b/euphoxide-bot/src/command/basic.rs index 4ec78e9..b6dda4c 100644 --- a/euphoxide-bot/src/command/basic.rs +++ b/euphoxide-bot/src/command/basic.rs @@ -51,10 +51,9 @@ impl Described { } #[async_trait] -impl Command for Described +impl Command for Described where - S: Send + Sync, - C: Command + Sync, + C: Command + Sync, { fn info(&self, ctx: &Context) -> Info { let info = self.inner.info(ctx); @@ -69,7 +68,7 @@ where arg: &str, msg: &Message, ctx: &Context, - bot: &Bot, + bot: &Bot, ) -> Result { self.inner.execute(arg, msg, ctx, bot).await } @@ -90,10 +89,9 @@ impl Prefixed { } #[async_trait] -impl Command for Prefixed +impl Command for Prefixed where - S: Send + Sync, - C: Command + Sync, + C: Command + Sync, { fn info(&self, ctx: &Context) -> Info { self.inner.info(ctx).with_prepended_trigger(&self.prefix) @@ -104,7 +102,7 @@ where arg: &str, msg: &Message, ctx: &Context, - bot: &Bot, + bot: &Bot, ) -> Result { if let Some(rest) = arg.trim_start().strip_prefix(&self.prefix) { self.inner.execute(rest, msg, ctx, bot).await diff --git a/euphoxide-bot/src/command/botrulez/full_help.rs b/euphoxide-bot/src/command/botrulez/full_help.rs index 868da06..627cf5b 100644 --- a/euphoxide-bot/src/command/botrulez/full_help.rs +++ b/euphoxide-bot/src/command/botrulez/full_help.rs @@ -31,7 +31,7 @@ impl FullHelp { self } - fn formulate_reply(&self, ctx: &Context, bot: &Bot) -> String { + fn formulate_reply(&self, ctx: &Context, bot: &Bot) -> String { let mut result = String::new(); if !self.before.is_empty() { @@ -60,9 +60,8 @@ impl FullHelp { } #[async_trait] -impl Command for FullHelp +impl Command for FullHelp where - S: Send + Sync, E: From, { async fn execute( @@ -70,7 +69,7 @@ where arg: &str, msg: &Message, ctx: &Context, - bot: &Bot, + bot: &Bot, ) -> Result { if arg.trim().is_empty() { let reply = self.formulate_reply(ctx, bot); @@ -89,9 +88,8 @@ pub struct FullHelpArgs {} #[cfg(feature = "clap")] #[async_trait] -impl ClapCommand for FullHelp +impl ClapCommand for FullHelp where - S: Send + Sync, E: From, { type Args = FullHelpArgs; @@ -101,7 +99,7 @@ where _args: Self::Args, msg: &Message, ctx: &Context, - bot: &Bot, + bot: &Bot, ) -> Result { let reply = self.formulate_reply(ctx, bot); ctx.reply_only(msg.id, reply).await?; diff --git a/euphoxide-bot/src/command/botrulez/ping.rs b/euphoxide-bot/src/command/botrulez/ping.rs index f419ba5..779a7a6 100644 --- a/euphoxide-bot/src/command/botrulez/ping.rs +++ b/euphoxide-bot/src/command/botrulez/ping.rs @@ -25,7 +25,7 @@ impl Default for Ping { } #[async_trait] -impl Command for Ping +impl Command for Ping where E: From, { @@ -34,7 +34,7 @@ where arg: &str, msg: &Message, ctx: &Context, - _bot: &Bot, + _bot: &Bot, ) -> Result { if arg.trim().is_empty() { ctx.reply_only(msg.id, &self.0).await?; @@ -52,7 +52,7 @@ pub struct PingArgs {} #[cfg(feature = "clap")] #[async_trait] -impl ClapCommand for Ping +impl ClapCommand for Ping where E: From, { @@ -63,7 +63,7 @@ where _args: Self::Args, msg: &Message, ctx: &Context, - _bot: &Bot, + _bot: &Bot, ) -> Result { ctx.reply_only(msg.id, &self.0).await?; Ok(Propagate::No) diff --git a/euphoxide-bot/src/command/botrulez/short_help.rs b/euphoxide-bot/src/command/botrulez/short_help.rs index 6ce5680..0eac90c 100644 --- a/euphoxide-bot/src/command/botrulez/short_help.rs +++ b/euphoxide-bot/src/command/botrulez/short_help.rs @@ -19,7 +19,7 @@ impl ShortHelp { } #[async_trait] -impl Command for ShortHelp +impl Command for ShortHelp where E: From, { @@ -28,7 +28,7 @@ where arg: &str, msg: &Message, ctx: &Context, - _bot: &Bot, + _bot: &Bot, ) -> Result { if arg.trim().is_empty() { ctx.reply_only(msg.id, &self.0).await?; @@ -46,7 +46,7 @@ pub struct ShortHelpArgs {} #[cfg(feature = "clap")] #[async_trait] -impl ClapCommand for ShortHelp +impl ClapCommand for ShortHelp where E: From, { @@ -57,7 +57,7 @@ where _args: Self::Args, msg: &Message, ctx: &Context, - _bot: &Bot, + _bot: &Bot, ) -> Result { ctx.reply_only(msg.id, &self.0).await?; Ok(Propagate::No) diff --git a/euphoxide-bot/src/command/botrulez/uptime.rs b/euphoxide-bot/src/command/botrulez/uptime.rs index 614a01e..ed01c9e 100644 --- a/euphoxide-bot/src/command/botrulez/uptime.rs +++ b/euphoxide-bot/src/command/botrulez/uptime.rs @@ -62,7 +62,7 @@ pub trait HasStartTime { } impl Uptime { - fn formulate_reply(&self, ctx: &Context, bot: &Bot, connected: bool) -> String { + fn formulate_reply(&self, ctx: &Context, bot: &Bot, connected: bool) -> String { let start = bot.start_time; let now = Timestamp::now(); @@ -86,9 +86,8 @@ impl Uptime { } #[async_trait] -impl Command for Uptime +impl Command for Uptime where - S: Send + Sync, E: From, { async fn execute( @@ -96,7 +95,7 @@ where arg: &str, msg: &Message, ctx: &Context, - bot: &Bot, + bot: &Bot, ) -> Result { if arg.trim().is_empty() { let reply = self.formulate_reply(ctx, bot, false); @@ -119,9 +118,8 @@ pub struct UptimeArgs { #[cfg(feature = "clap")] #[async_trait] -impl ClapCommand for Uptime +impl ClapCommand for Uptime where - S: Send + Sync, E: From, { type Args = UptimeArgs; @@ -131,7 +129,7 @@ where args: Self::Args, msg: &Message, ctx: &Context, - bot: &Bot, + bot: &Bot, ) -> Result { let reply = self.formulate_reply(ctx, bot, args.connected); ctx.reply_only(msg.id, reply).await?; diff --git a/euphoxide-bot/src/command/clap.rs b/euphoxide-bot/src/command/clap.rs index fb03974..7d241ef 100644 --- a/euphoxide-bot/src/command/clap.rs +++ b/euphoxide-bot/src/command/clap.rs @@ -9,7 +9,7 @@ use crate::bot::Bot; use super::{Command, Context, Info, Propagate}; #[async_trait] -pub trait ClapCommand { +pub trait ClapCommand { type Args; async fn execute( @@ -17,7 +17,7 @@ pub trait ClapCommand { args: Self::Args, msg: &Message, ctx: &Context, - bot: &Bot, + bot: &Bot, ) -> Result; } @@ -101,11 +101,10 @@ fn parse_quoted_args(text: &str) -> Result, &'static str> { pub struct Clap(pub C); #[async_trait] -impl Command for Clap +impl Command for Clap where - S: Send + Sync, E: From, - C: ClapCommand + Sync, + C: ClapCommand + Sync, C::Args: Parser + Send, { fn info(&self, _ctx: &Context) -> Info { @@ -120,7 +119,7 @@ where arg: &str, msg: &Message, ctx: &Context, - bot: &Bot, + bot: &Bot, ) -> Result { let mut args = match parse_quoted_args(arg) { Ok(args) => args,