Remove Bot completely

This commit is contained in:
Joscha 2024-12-28 19:17:44 +01:00
parent 07d3f7a0f6
commit 346f4ff543
11 changed files with 105 additions and 241 deletions

View file

@ -3,8 +3,6 @@
use async_trait::async_trait;
use euphoxide::{api::Message, nick};
use crate::bot::Bot;
use super::{Command, Context, Info, Propagate};
// TODO Don't ignore leading whitespace?
@ -51,19 +49,13 @@ impl<E, C> Command<E> for Global<C>
where
C: Command<E> + Sync,
{
fn info(&self, ctx: &Context) -> Info {
fn info(&self, ctx: &Context<E>) -> Info {
self.inner
.info(ctx)
.with_prepended_trigger(format!("{}{}", self.prefix, self.name))
}
async fn execute(
&self,
arg: &str,
msg: &Message,
ctx: &Context,
bot: &Bot<E>,
) -> Result<Propagate, E> {
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context<E>) -> Result<Propagate, E> {
let Some((name, rest)) = parse_prefix_initiated(arg, &self.prefix) else {
return Ok(Propagate::Yes);
};
@ -72,7 +64,7 @@ where
return Ok(Propagate::Yes);
}
self.inner.execute(rest, msg, ctx, bot).await
self.inner.execute(rest, msg, ctx).await
}
}
@ -102,19 +94,13 @@ impl<E, C> Command<E> for General<C>
where
C: Command<E> + Sync,
{
fn info(&self, ctx: &Context) -> Info {
fn info(&self, ctx: &Context<E>) -> Info {
self.inner
.info(ctx)
.with_prepended_trigger(format!("{}{}", self.prefix, self.name))
}
async fn execute(
&self,
arg: &str,
msg: &Message,
ctx: &Context,
bot: &Bot<E>,
) -> Result<Propagate, E> {
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context<E>) -> Result<Propagate, E> {
let Some((name, rest)) = parse_prefix_initiated(arg, &self.prefix) else {
return Ok(Propagate::Yes);
};
@ -130,7 +116,7 @@ where
return Ok(Propagate::Yes);
}
self.inner.execute(rest, msg, ctx, bot).await
self.inner.execute(rest, msg, ctx).await
}
}
@ -160,20 +146,14 @@ impl<E, C> Command<E> for Specific<C>
where
C: Command<E> + Sync,
{
fn info(&self, ctx: &Context) -> Info {
fn info(&self, ctx: &Context<E>) -> Info {
let nick = nick::mention(&ctx.joined.session.name);
self.inner
.info(ctx)
.with_prepended_trigger(format!("{}{} @{nick}", self.prefix, self.name))
}
async fn execute(
&self,
arg: &str,
msg: &Message,
ctx: &Context,
bot: &Bot<E>,
) -> Result<Propagate, E> {
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context<E>) -> Result<Propagate, E> {
let Some((name, rest)) = parse_prefix_initiated(arg, &self.prefix) else {
return Ok(Propagate::Yes);
};
@ -190,7 +170,7 @@ where
return Ok(Propagate::Yes);
}
self.inner.execute(rest, msg, ctx, bot).await
self.inner.execute(rest, msg, ctx).await
}
}

View file

@ -3,8 +3,6 @@
use async_trait::async_trait;
use euphoxide::api::Message;
use crate::bot::Bot;
use super::{Command, Context, Info, Propagate};
/// Rewrite or hide command info.
@ -55,7 +53,7 @@ impl<E, C> Command<E> for Described<C>
where
C: Command<E> + Sync,
{
fn info(&self, ctx: &Context) -> Info {
fn info(&self, ctx: &Context<E>) -> Info {
let info = self.inner.info(ctx);
Info {
trigger: self.trigger.clone().unwrap_or(info.trigger),
@ -63,14 +61,8 @@ where
}
}
async fn execute(
&self,
arg: &str,
msg: &Message,
ctx: &Context,
bot: &Bot<E>,
) -> Result<Propagate, E> {
self.inner.execute(arg, msg, ctx, bot).await
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context<E>) -> Result<Propagate, E> {
self.inner.execute(arg, msg, ctx).await
}
}
@ -93,19 +85,13 @@ impl<E, C> Command<E> for Prefixed<C>
where
C: Command<E> + Sync,
{
fn info(&self, ctx: &Context) -> Info {
fn info(&self, ctx: &Context<E>) -> Info {
self.inner.info(ctx).with_prepended_trigger(&self.prefix)
}
async fn execute(
&self,
arg: &str,
msg: &Message,
ctx: &Context,
bot: &Bot<E>,
) -> Result<Propagate, E> {
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context<E>) -> Result<Propagate, E> {
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).await
} else {
Ok(Propagate::Yes)
}

View file

@ -5,10 +5,7 @@ use euphoxide::api::Message;
#[cfg(feature = "clap")]
use crate::command::clap::ClapCommand;
use crate::{
bot::Bot,
command::{Command, Context, Propagate},
};
use crate::command::{Command, Context, Propagate};
#[derive(Default)]
pub struct FullHelp {
@ -31,7 +28,7 @@ impl FullHelp {
self
}
fn formulate_reply<E>(&self, ctx: &Context, bot: &Bot<E>) -> String {
fn formulate_reply<E>(&self, ctx: &Context<E>) -> String {
let mut result = String::new();
if !self.before.is_empty() {
@ -39,7 +36,7 @@ impl FullHelp {
result.push('\n');
}
for info in bot.commands.infos(ctx) {
for info in ctx.commands.infos(ctx) {
if let Some(trigger) = &info.trigger {
result.push_str(trigger);
if let Some(description) = &info.description {
@ -64,15 +61,9 @@ impl<E> Command<E> for FullHelp
where
E: From<euphoxide::Error>,
{
async fn execute(
&self,
arg: &str,
msg: &Message,
ctx: &Context,
bot: &Bot<E>,
) -> Result<Propagate, E> {
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context<E>) -> Result<Propagate, E> {
if arg.trim().is_empty() {
let reply = self.formulate_reply(ctx, bot);
let reply = self.formulate_reply(ctx);
ctx.reply_only(msg.id, reply).await?;
Ok(Propagate::No)
} else {
@ -98,10 +89,9 @@ where
&self,
_args: Self::Args,
msg: &Message,
ctx: &Context,
bot: &Bot<E>,
ctx: &Context<E>,
) -> Result<Propagate, E> {
let reply = self.formulate_reply(ctx, bot);
let reply = self.formulate_reply(ctx);
ctx.reply_only(msg.id, reply).await?;
Ok(Propagate::No)
}

View file

@ -5,10 +5,7 @@ use euphoxide::api::Message;
#[cfg(feature = "clap")]
use crate::command::clap::ClapCommand;
use crate::{
bot::Bot,
command::{Command, Context, Propagate},
};
use crate::command::{Command, Context, Propagate};
pub struct Ping(pub String);
@ -29,13 +26,7 @@ impl<E> Command<E> for Ping
where
E: From<euphoxide::Error>,
{
async fn execute(
&self,
arg: &str,
msg: &Message,
ctx: &Context,
_bot: &Bot<E>,
) -> Result<Propagate, E> {
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context<E>) -> Result<Propagate, E> {
if arg.trim().is_empty() {
ctx.reply_only(msg.id, &self.0).await?;
Ok(Propagate::No)
@ -62,8 +53,7 @@ where
&self,
_args: Self::Args,
msg: &Message,
ctx: &Context,
_bot: &Bot<E>,
ctx: &Context<E>,
) -> Result<Propagate, E> {
ctx.reply_only(msg.id, &self.0).await?;
Ok(Propagate::No)

View file

@ -5,10 +5,7 @@ use euphoxide::api::Message;
#[cfg(feature = "clap")]
use crate::command::clap::ClapCommand;
use crate::{
bot::Bot,
command::{Command, Context, Propagate},
};
use crate::command::{Command, Context, Propagate};
pub struct ShortHelp(pub String);
@ -23,13 +20,7 @@ impl<E> Command<E> for ShortHelp
where
E: From<euphoxide::Error>,
{
async fn execute(
&self,
arg: &str,
msg: &Message,
ctx: &Context,
_bot: &Bot<E>,
) -> Result<Propagate, E> {
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context<E>) -> Result<Propagate, E> {
if arg.trim().is_empty() {
ctx.reply_only(msg.id, &self.0).await?;
Ok(Propagate::No)
@ -56,8 +47,7 @@ where
&self,
_args: Self::Args,
msg: &Message,
ctx: &Context,
_bot: &Bot<E>,
ctx: &Context<E>,
) -> Result<Propagate, E> {
ctx.reply_only(msg.id, &self.0).await?;
Ok(Propagate::No)

View file

@ -6,10 +6,7 @@ use jiff::{Span, Timestamp, Unit};
#[cfg(feature = "clap")]
use crate::command::clap::ClapCommand;
use crate::{
bot::Bot,
command::{Command, Context, Propagate},
};
use crate::command::{Command, Context, Propagate};
pub fn format_time(t: Timestamp) -> String {
t.strftime("%Y-%m-%d %H:%M:%S UTC").to_string()
@ -62,14 +59,8 @@ pub trait HasStartTime {
}
impl Uptime {
fn formulate_reply<E>(
&self,
ctx: &Context,
bot: &Bot<E>,
joined: bool,
connected: bool,
) -> String {
let start = bot.clients.start_time();
fn formulate_reply<E>(&self, ctx: &Context<E>, joined: bool, connected: bool) -> String {
let start = ctx.clients.start_time();
let now = Timestamp::now();
let mut reply = format!(
@ -105,15 +96,9 @@ impl<E> Command<E> for Uptime
where
E: From<euphoxide::Error>,
{
async fn execute(
&self,
arg: &str,
msg: &Message,
ctx: &Context,
bot: &Bot<E>,
) -> Result<Propagate, E> {
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context<E>) -> Result<Propagate, E> {
if arg.trim().is_empty() {
let reply = self.formulate_reply(ctx, bot, false, false);
let reply = self.formulate_reply(ctx, false, false);
ctx.reply_only(msg.id, reply).await?;
Ok(Propagate::No)
} else {
@ -146,10 +131,9 @@ where
&self,
args: Self::Args,
msg: &Message,
ctx: &Context,
bot: &Bot<E>,
ctx: &Context<E>,
) -> Result<Propagate, E> {
let reply = self.formulate_reply(ctx, bot, args.present, args.connected);
let reply = self.formulate_reply(ctx, args.present, args.connected);
ctx.reply_only(msg.id, reply).await?;
Ok(Propagate::No)
}

View file

@ -4,8 +4,6 @@ use async_trait::async_trait;
use clap::{CommandFactory, Parser};
use euphoxide::api::Message;
use crate::bot::Bot;
use super::{Command, Context, Info, Propagate};
#[async_trait]
@ -16,8 +14,7 @@ pub trait ClapCommand<E> {
&self,
args: Self::Args,
msg: &Message,
ctx: &Context,
bot: &Bot<E>,
ctx: &Context<E>,
) -> Result<Propagate, E>;
}
@ -107,20 +104,14 @@ where
C: ClapCommand<E> + Sync,
C::Args: Parser + Send,
{
fn info(&self, _ctx: &Context) -> Info {
fn info(&self, _ctx: &Context<E>) -> Info {
Info {
description: C::Args::command().get_about().map(|s| s.to_string()),
..Info::default()
}
}
async fn execute(
&self,
arg: &str,
msg: &Message,
ctx: &Context,
bot: &Bot<E>,
) -> Result<Propagate, E> {
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context<E>) -> Result<Propagate, E> {
let mut args = match parse_quoted_args(arg) {
Ok(args) => args,
Err(err) => {
@ -141,7 +132,7 @@ where
}
};
self.0.execute(args, msg, ctx, bot).await
self.0.execute(args, msg, ctx).await
}
}