Remove generic bot state

This commit is contained in:
Joscha 2024-12-28 18:50:41 +01:00
parent c356c5f2ae
commit 51ee7b9246
9 changed files with 53 additions and 69 deletions

View file

@ -8,10 +8,9 @@ use tokio::sync::mpsc;
use crate::command::Commands; use crate::command::Commands;
#[non_exhaustive] #[non_exhaustive]
pub struct Bot<S = (), E = euphoxide::Error> { pub struct Bot<E = euphoxide::Error> {
pub server_config: ServerConfig, pub server_config: ServerConfig,
pub commands: Arc<Commands<S, E>>, pub commands: Arc<Commands<E>>,
pub state: Arc<S>,
pub clients: MultiClient, pub clients: MultiClient,
pub start_time: Timestamp, pub start_time: Timestamp,
} }
@ -22,32 +21,28 @@ impl Bot {
ServerConfig::default(), ServerConfig::default(),
MultiClientConfig::default(), MultiClientConfig::default(),
commands, commands,
(),
event_tx, event_tx,
) )
} }
} }
impl<S, E> Bot<S, E> { impl<E> Bot<E> {
pub fn new( pub fn new(
server_config: ServerConfig, server_config: ServerConfig,
clients_config: MultiClientConfig, clients_config: MultiClientConfig,
commands: Commands<S, E>, commands: Commands<E>,
state: S,
event_tx: mpsc::Sender<MultiClientEvent>, event_tx: mpsc::Sender<MultiClientEvent>,
) -> Self { ) -> Self {
Self { Self {
server_config, server_config,
commands: Arc::new(commands), commands: Arc::new(commands),
state: Arc::new(state),
clients: MultiClient::new_with_config(clients_config, event_tx), clients: MultiClient::new_with_config(clients_config, event_tx),
start_time: Timestamp::now(), start_time: Timestamp::now(),
} }
} }
} }
impl<S, E> Bot<S, E> impl<E> Bot<E>
where where
S: Send + Sync + 'static,
E: Debug + 'static, E: Debug + 'static,
{ {
pub fn handle_event(&self, event: MultiClientEvent) { pub fn handle_event(&self, event: MultiClientEvent) {
@ -60,12 +55,11 @@ where
} }
} }
impl<S, E> Clone for Bot<S, E> { impl<E> Clone for Bot<E> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
server_config: self.server_config.clone(), server_config: self.server_config.clone(),
commands: self.commands.clone(), commands: self.commands.clone(),
state: self.state.clone(),
clients: self.clients.clone(), clients: self.clients.clone(),
start_time: self.start_time, start_time: self.start_time,
} }

View file

@ -110,7 +110,7 @@ pub enum Propagate {
#[allow(unused_variables)] #[allow(unused_variables)]
#[async_trait] #[async_trait]
pub trait Command<S = (), E = euphoxide::Error> { pub trait Command<E = euphoxide::Error> {
fn info(&self, ctx: &Context) -> Info { fn info(&self, ctx: &Context) -> Info {
Info::default() Info::default()
} }
@ -120,24 +120,24 @@ pub trait Command<S = (), E = euphoxide::Error> {
arg: &str, arg: &str,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E>; ) -> Result<Propagate, E>;
} }
pub struct Commands<B = (), E = euphoxide::Error> { pub struct Commands<E = euphoxide::Error> {
commands: Vec<Box<dyn Command<B, E> + Sync + Send>>, commands: Vec<Box<dyn Command<E> + Sync + Send>>,
} }
impl<S, E> Commands<S, E> { impl<E> Commands<E> {
pub fn new() -> Self { pub fn new() -> Self {
Self { commands: vec![] } Self { commands: vec![] }
} }
pub fn add(&mut self, command: impl Command<S, E> + Sync + Send + 'static) { pub fn add(&mut self, command: impl Command<E> + Sync + Send + 'static) {
self.commands.push(Box::new(command)); self.commands.push(Box::new(command));
} }
pub fn then(mut self, command: impl Command<S, E> + Sync + Send + 'static) -> Self { pub fn then(mut self, command: impl Command<E> + Sync + Send + 'static) -> Self {
self.add(command); self.add(command);
self self
} }
@ -149,7 +149,7 @@ impl<S, E> Commands<S, E> {
pub(crate) async fn on_event( pub(crate) async fn on_event(
&self, &self,
event: MultiClientEvent, event: MultiClientEvent,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
let MultiClientEvent::Packet { let MultiClientEvent::Packet {
client, client,
@ -187,7 +187,7 @@ impl<S, E> Commands<S, E> {
} }
// Has fewer restrictions on generic types than #[derive(Default)]. // Has fewer restrictions on generic types than #[derive(Default)].
impl<S, E> Default for Commands<S, E> { impl<E> Default for Commands<E> {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }

View file

@ -47,10 +47,9 @@ impl<C> Global<C> {
} }
#[async_trait] #[async_trait]
impl<S, E, C> Command<S, E> for Global<C> impl<E, C> Command<E> for Global<C>
where where
S: Send + Sync, C: Command<E> + Sync,
C: Command<S, E> + Sync,
{ {
fn info(&self, ctx: &Context) -> Info { fn info(&self, ctx: &Context) -> Info {
self.inner self.inner
@ -63,7 +62,7 @@ where
arg: &str, arg: &str,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
let Some((name, rest)) = parse_prefix_initiated(arg, &self.prefix) else { let Some((name, rest)) = parse_prefix_initiated(arg, &self.prefix) else {
return Ok(Propagate::Yes); return Ok(Propagate::Yes);
@ -99,10 +98,9 @@ impl<C> General<C> {
} }
#[async_trait] #[async_trait]
impl<S, E, C> Command<S, E> for General<C> impl<E, C> Command<E> for General<C>
where where
S: Send + Sync, C: Command<E> + Sync,
C: Command<S, E> + Sync,
{ {
fn info(&self, ctx: &Context) -> Info { fn info(&self, ctx: &Context) -> Info {
self.inner self.inner
@ -115,7 +113,7 @@ where
arg: &str, arg: &str,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
let Some((name, rest)) = parse_prefix_initiated(arg, &self.prefix) else { let Some((name, rest)) = parse_prefix_initiated(arg, &self.prefix) else {
return Ok(Propagate::Yes); return Ok(Propagate::Yes);
@ -158,10 +156,9 @@ impl<C> Specific<C> {
} }
#[async_trait] #[async_trait]
impl<S, E, C> Command<S, E> for Specific<C> impl<E, C> Command<E> for Specific<C>
where where
S: Send + Sync, C: Command<E> + Sync,
C: Command<S, E> + Sync,
{ {
fn info(&self, ctx: &Context) -> Info { fn info(&self, ctx: &Context) -> Info {
let nick = nick::mention(&ctx.joined.session.name); let nick = nick::mention(&ctx.joined.session.name);
@ -175,7 +172,7 @@ where
arg: &str, arg: &str,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
let Some((name, rest)) = parse_prefix_initiated(arg, &self.prefix) else { let Some((name, rest)) = parse_prefix_initiated(arg, &self.prefix) else {
return Ok(Propagate::Yes); return Ok(Propagate::Yes);

View file

@ -51,10 +51,9 @@ impl<C> Described<C> {
} }
#[async_trait] #[async_trait]
impl<S, E, C> Command<S, E> for Described<C> impl<E, C> Command<E> for Described<C>
where where
S: Send + Sync, C: Command<E> + Sync,
C: Command<S, E> + Sync,
{ {
fn info(&self, ctx: &Context) -> Info { fn info(&self, ctx: &Context) -> Info {
let info = self.inner.info(ctx); let info = self.inner.info(ctx);
@ -69,7 +68,7 @@ where
arg: &str, arg: &str,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
self.inner.execute(arg, msg, ctx, bot).await self.inner.execute(arg, msg, ctx, bot).await
} }
@ -90,10 +89,9 @@ impl<C> Prefixed<C> {
} }
#[async_trait] #[async_trait]
impl<S, E, C> Command<S, E> for Prefixed<C> impl<E, C> Command<E> for Prefixed<C>
where where
S: Send + Sync, C: Command<E> + Sync,
C: Command<S, E> + Sync,
{ {
fn info(&self, ctx: &Context) -> Info { fn info(&self, ctx: &Context) -> Info {
self.inner.info(ctx).with_prepended_trigger(&self.prefix) self.inner.info(ctx).with_prepended_trigger(&self.prefix)
@ -104,7 +102,7 @@ where
arg: &str, arg: &str,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
if let Some(rest) = arg.trim_start().strip_prefix(&self.prefix) { if let Some(rest) = arg.trim_start().strip_prefix(&self.prefix) {
self.inner.execute(rest, msg, ctx, bot).await self.inner.execute(rest, msg, ctx, bot).await

View file

@ -31,7 +31,7 @@ impl FullHelp {
self self
} }
fn formulate_reply<S, E>(&self, ctx: &Context, bot: &Bot<S, E>) -> String { fn formulate_reply<E>(&self, ctx: &Context, bot: &Bot<E>) -> String {
let mut result = String::new(); let mut result = String::new();
if !self.before.is_empty() { if !self.before.is_empty() {
@ -60,9 +60,8 @@ impl FullHelp {
} }
#[async_trait] #[async_trait]
impl<S, E> Command<S, E> for FullHelp impl<E> Command<E> for FullHelp
where where
S: Send + Sync,
E: From<euphoxide::Error>, E: From<euphoxide::Error>,
{ {
async fn execute( async fn execute(
@ -70,7 +69,7 @@ where
arg: &str, arg: &str,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
if arg.trim().is_empty() { if arg.trim().is_empty() {
let reply = self.formulate_reply(ctx, bot); let reply = self.formulate_reply(ctx, bot);
@ -89,9 +88,8 @@ pub struct FullHelpArgs {}
#[cfg(feature = "clap")] #[cfg(feature = "clap")]
#[async_trait] #[async_trait]
impl<S, E> ClapCommand<S, E> for FullHelp impl<E> ClapCommand<E> for FullHelp
where where
S: Send + Sync,
E: From<euphoxide::Error>, E: From<euphoxide::Error>,
{ {
type Args = FullHelpArgs; type Args = FullHelpArgs;
@ -101,7 +99,7 @@ where
_args: Self::Args, _args: Self::Args,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
let reply = self.formulate_reply(ctx, bot); let reply = self.formulate_reply(ctx, bot);
ctx.reply_only(msg.id, reply).await?; ctx.reply_only(msg.id, reply).await?;

View file

@ -25,7 +25,7 @@ impl Default for Ping {
} }
#[async_trait] #[async_trait]
impl<S, E> Command<S, E> for Ping impl<E> Command<E> for Ping
where where
E: From<euphoxide::Error>, E: From<euphoxide::Error>,
{ {
@ -34,7 +34,7 @@ where
arg: &str, arg: &str,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
_bot: &Bot<S, E>, _bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
if arg.trim().is_empty() { if arg.trim().is_empty() {
ctx.reply_only(msg.id, &self.0).await?; ctx.reply_only(msg.id, &self.0).await?;
@ -52,7 +52,7 @@ pub struct PingArgs {}
#[cfg(feature = "clap")] #[cfg(feature = "clap")]
#[async_trait] #[async_trait]
impl<S, E> ClapCommand<S, E> for Ping impl<E> ClapCommand<E> for Ping
where where
E: From<euphoxide::Error>, E: From<euphoxide::Error>,
{ {
@ -63,7 +63,7 @@ where
_args: Self::Args, _args: Self::Args,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
_bot: &Bot<S, E>, _bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
ctx.reply_only(msg.id, &self.0).await?; ctx.reply_only(msg.id, &self.0).await?;
Ok(Propagate::No) Ok(Propagate::No)

View file

@ -19,7 +19,7 @@ impl ShortHelp {
} }
#[async_trait] #[async_trait]
impl<S, E> Command<S, E> for ShortHelp impl<E> Command<E> for ShortHelp
where where
E: From<euphoxide::Error>, E: From<euphoxide::Error>,
{ {
@ -28,7 +28,7 @@ where
arg: &str, arg: &str,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
_bot: &Bot<S, E>, _bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
if arg.trim().is_empty() { if arg.trim().is_empty() {
ctx.reply_only(msg.id, &self.0).await?; ctx.reply_only(msg.id, &self.0).await?;
@ -46,7 +46,7 @@ pub struct ShortHelpArgs {}
#[cfg(feature = "clap")] #[cfg(feature = "clap")]
#[async_trait] #[async_trait]
impl<S, E> ClapCommand<S, E> for ShortHelp impl<E> ClapCommand<E> for ShortHelp
where where
E: From<euphoxide::Error>, E: From<euphoxide::Error>,
{ {
@ -57,7 +57,7 @@ where
_args: Self::Args, _args: Self::Args,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
_bot: &Bot<S, E>, _bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
ctx.reply_only(msg.id, &self.0).await?; ctx.reply_only(msg.id, &self.0).await?;
Ok(Propagate::No) Ok(Propagate::No)

View file

@ -62,7 +62,7 @@ pub trait HasStartTime {
} }
impl Uptime { impl Uptime {
fn formulate_reply<S, E>(&self, ctx: &Context, bot: &Bot<S, E>, connected: bool) -> String { fn formulate_reply<E>(&self, ctx: &Context, bot: &Bot<E>, connected: bool) -> String {
let start = bot.start_time; let start = bot.start_time;
let now = Timestamp::now(); let now = Timestamp::now();
@ -86,9 +86,8 @@ impl Uptime {
} }
#[async_trait] #[async_trait]
impl<S, E> Command<S, E> for Uptime impl<E> Command<E> for Uptime
where where
S: Send + Sync,
E: From<euphoxide::Error>, E: From<euphoxide::Error>,
{ {
async fn execute( async fn execute(
@ -96,7 +95,7 @@ where
arg: &str, arg: &str,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
if arg.trim().is_empty() { if arg.trim().is_empty() {
let reply = self.formulate_reply(ctx, bot, false); let reply = self.formulate_reply(ctx, bot, false);
@ -119,9 +118,8 @@ pub struct UptimeArgs {
#[cfg(feature = "clap")] #[cfg(feature = "clap")]
#[async_trait] #[async_trait]
impl<S, E> ClapCommand<S, E> for Uptime impl<E> ClapCommand<E> for Uptime
where where
S: Send + Sync,
E: From<euphoxide::Error>, E: From<euphoxide::Error>,
{ {
type Args = UptimeArgs; type Args = UptimeArgs;
@ -131,7 +129,7 @@ where
args: Self::Args, args: Self::Args,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
let reply = self.formulate_reply(ctx, bot, args.connected); let reply = self.formulate_reply(ctx, bot, args.connected);
ctx.reply_only(msg.id, reply).await?; ctx.reply_only(msg.id, reply).await?;

View file

@ -9,7 +9,7 @@ use crate::bot::Bot;
use super::{Command, Context, Info, Propagate}; use super::{Command, Context, Info, Propagate};
#[async_trait] #[async_trait]
pub trait ClapCommand<S, E> { pub trait ClapCommand<E> {
type Args; type Args;
async fn execute( async fn execute(
@ -17,7 +17,7 @@ pub trait ClapCommand<S, E> {
args: Self::Args, args: Self::Args,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E>; ) -> Result<Propagate, E>;
} }
@ -101,11 +101,10 @@ fn parse_quoted_args(text: &str) -> Result<Vec<String>, &'static str> {
pub struct Clap<C>(pub C); pub struct Clap<C>(pub C);
#[async_trait] #[async_trait]
impl<S, E, C> Command<S, E> for Clap<C> impl<E, C> Command<E> for Clap<C>
where where
S: Send + Sync,
E: From<euphoxide::Error>, E: From<euphoxide::Error>,
C: ClapCommand<S, E> + Sync, C: ClapCommand<E> + Sync,
C::Args: Parser + Send, C::Args: Parser + Send,
{ {
fn info(&self, _ctx: &Context) -> Info { fn info(&self, _ctx: &Context) -> Info {
@ -120,7 +119,7 @@ where
arg: &str, arg: &str,
msg: &Message, msg: &Message,
ctx: &Context, ctx: &Context,
bot: &Bot<S, E>, bot: &Bot<E>,
) -> Result<Propagate, E> { ) -> Result<Propagate, E> {
let mut args = match parse_quoted_args(arg) { let mut args = match parse_quoted_args(arg) {
Ok(args) => args, Ok(args) => args,