Remove generic bot state

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

View file

@ -8,10 +8,9 @@ use tokio::sync::mpsc;
use crate::command::Commands;
#[non_exhaustive]
pub struct Bot<S = (), E = euphoxide::Error> {
pub struct Bot<E = euphoxide::Error> {
pub server_config: ServerConfig,
pub commands: Arc<Commands<S, E>>,
pub state: Arc<S>,
pub commands: Arc<Commands<E>>,
pub clients: MultiClient,
pub start_time: Timestamp,
}
@ -22,32 +21,28 @@ impl Bot {
ServerConfig::default(),
MultiClientConfig::default(),
commands,
(),
event_tx,
)
}
}
impl<S, E> Bot<S, E> {
impl<E> Bot<E> {
pub fn new(
server_config: ServerConfig,
clients_config: MultiClientConfig,
commands: Commands<S, E>,
state: S,
commands: Commands<E>,
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>
impl<E> Bot<E>
where
S: Send + Sync + 'static,
E: Debug + 'static,
{
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 {
Self {
server_config: self.server_config.clone(),
commands: self.commands.clone(),
state: self.state.clone(),
clients: self.clients.clone(),
start_time: self.start_time,
}

View file

@ -110,7 +110,7 @@ pub enum Propagate {
#[allow(unused_variables)]
#[async_trait]
pub trait Command<S = (), E = euphoxide::Error> {
pub trait Command<E = euphoxide::Error> {
fn info(&self, ctx: &Context) -> Info {
Info::default()
}
@ -120,24 +120,24 @@ pub trait Command<S = (), E = euphoxide::Error> {
arg: &str,
msg: &Message,
ctx: &Context,
bot: &Bot<S, E>,
bot: &Bot<E>,
) -> Result<Propagate, E>;
}
pub struct Commands<B = (), E = euphoxide::Error> {
commands: Vec<Box<dyn Command<B, E> + Sync + Send>>,
pub struct Commands<E = euphoxide::Error> {
commands: Vec<Box<dyn Command<E> + Sync + Send>>,
}
impl<S, E> Commands<S, E> {
impl<E> Commands<E> {
pub fn new() -> Self {
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));
}
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
}
@ -149,7 +149,7 @@ impl<S, E> Commands<S, E> {
pub(crate) async fn on_event(
&self,
event: MultiClientEvent,
bot: &Bot<S, E>,
bot: &Bot<E>,
) -> Result<Propagate, E> {
let MultiClientEvent::Packet {
client,
@ -187,7 +187,7 @@ impl<S, E> Commands<S, E> {
}
// 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 {
Self::new()
}

View file

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

View file

@ -51,10 +51,9 @@ impl<C> Described<C> {
}
#[async_trait]
impl<S, E, C> Command<S, E> for Described<C>
impl<E, C> Command<E> for Described<C>
where
S: Send + Sync,
C: Command<S, E> + Sync,
C: Command<E> + 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<S, E>,
bot: &Bot<E>,
) -> Result<Propagate, E> {
self.inner.execute(arg, msg, ctx, bot).await
}
@ -90,10 +89,9 @@ impl<C> Prefixed<C> {
}
#[async_trait]
impl<S, E, C> Command<S, E> for Prefixed<C>
impl<E, C> Command<E> for Prefixed<C>
where
S: Send + Sync,
C: Command<S, E> + Sync,
C: Command<E> + 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<S, E>,
bot: &Bot<E>,
) -> Result<Propagate, E> {
if let Some(rest) = arg.trim_start().strip_prefix(&self.prefix) {
self.inner.execute(rest, msg, ctx, bot).await

View file

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

View file

@ -25,7 +25,7 @@ impl Default for Ping {
}
#[async_trait]
impl<S, E> Command<S, E> for Ping
impl<E> Command<E> for Ping
where
E: From<euphoxide::Error>,
{
@ -34,7 +34,7 @@ where
arg: &str,
msg: &Message,
ctx: &Context,
_bot: &Bot<S, E>,
_bot: &Bot<E>,
) -> Result<Propagate, E> {
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<S, E> ClapCommand<S, E> for Ping
impl<E> ClapCommand<E> for Ping
where
E: From<euphoxide::Error>,
{
@ -63,7 +63,7 @@ where
_args: Self::Args,
msg: &Message,
ctx: &Context,
_bot: &Bot<S, E>,
_bot: &Bot<E>,
) -> Result<Propagate, E> {
ctx.reply_only(msg.id, &self.0).await?;
Ok(Propagate::No)

View file

@ -19,7 +19,7 @@ impl ShortHelp {
}
#[async_trait]
impl<S, E> Command<S, E> for ShortHelp
impl<E> Command<E> for ShortHelp
where
E: From<euphoxide::Error>,
{
@ -28,7 +28,7 @@ where
arg: &str,
msg: &Message,
ctx: &Context,
_bot: &Bot<S, E>,
_bot: &Bot<E>,
) -> Result<Propagate, E> {
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<S, E> ClapCommand<S, E> for ShortHelp
impl<E> ClapCommand<E> for ShortHelp
where
E: From<euphoxide::Error>,
{
@ -57,7 +57,7 @@ where
_args: Self::Args,
msg: &Message,
ctx: &Context,
_bot: &Bot<S, E>,
_bot: &Bot<E>,
) -> Result<Propagate, E> {
ctx.reply_only(msg.id, &self.0).await?;
Ok(Propagate::No)

View file

@ -62,7 +62,7 @@ pub trait HasStartTime {
}
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 now = Timestamp::now();
@ -86,9 +86,8 @@ impl Uptime {
}
#[async_trait]
impl<S, E> Command<S, E> for Uptime
impl<E> Command<E> for Uptime
where
S: Send + Sync,
E: From<euphoxide::Error>,
{
async fn execute(
@ -96,7 +95,7 @@ where
arg: &str,
msg: &Message,
ctx: &Context,
bot: &Bot<S, E>,
bot: &Bot<E>,
) -> Result<Propagate, E> {
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<S, E> ClapCommand<S, E> for Uptime
impl<E> ClapCommand<E> for Uptime
where
S: Send + Sync,
E: From<euphoxide::Error>,
{
type Args = UptimeArgs;
@ -131,7 +129,7 @@ where
args: Self::Args,
msg: &Message,
ctx: &Context,
bot: &Bot<S, E>,
bot: &Bot<E>,
) -> Result<Propagate, E> {
let reply = self.formulate_reply(ctx, bot, args.connected);
ctx.reply_only(msg.id, reply).await?;

View file

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