Implement Command for Uptime
This commit is contained in:
parent
1844cddcc0
commit
a6331d50b8
2 changed files with 44 additions and 19 deletions
|
|
@ -13,6 +13,9 @@ Procedure when bumping the version number:
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- `bot::botrulez::Uptime` now implements `bot::command::Command`
|
||||||
|
|
||||||
## v0.3.1 - 2023-02-26
|
## v0.3.1 - 2023-02-26
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use time::macros::format_description;
|
||||||
use time::{Duration, OffsetDateTime, UtcOffset};
|
use time::{Duration, OffsetDateTime, UtcOffset};
|
||||||
|
|
||||||
use crate::api::Message;
|
use crate::api::Message;
|
||||||
use crate::bot::command::{ClapCommand, Context};
|
use crate::bot::command::{ClapCommand, Command, Context};
|
||||||
use crate::conn;
|
use crate::conn;
|
||||||
|
|
||||||
pub fn format_time(t: OffsetDateTime) -> String {
|
pub fn format_time(t: OffsetDateTime) -> String {
|
||||||
|
|
@ -51,6 +51,45 @@ pub trait HasStartTime {
|
||||||
fn start_time(&self) -> OffsetDateTime;
|
fn start_time(&self) -> OffsetDateTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Uptime {
|
||||||
|
fn formulate_reply<B: HasStartTime>(&self, ctx: &Context, bot: &B, connected: bool) -> String {
|
||||||
|
let start = bot.start_time();
|
||||||
|
let now = OffsetDateTime::now_utc();
|
||||||
|
|
||||||
|
let mut reply = format!(
|
||||||
|
"/me has been up since {} ({})",
|
||||||
|
format_time(start),
|
||||||
|
format_duration(start - now),
|
||||||
|
);
|
||||||
|
|
||||||
|
if connected {
|
||||||
|
let since = ctx.joined.since;
|
||||||
|
reply.push_str(&format!(
|
||||||
|
", connected since {} ({})",
|
||||||
|
format_time(since),
|
||||||
|
format_duration(since - now),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
reply
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl<B, E> Command<B, E> for Uptime
|
||||||
|
where
|
||||||
|
B: HasStartTime + Send,
|
||||||
|
E: From<conn::Error>,
|
||||||
|
{
|
||||||
|
async fn execute(&self, arg: &str, msg: &Message, ctx: &Context, bot: &mut B) -> Result<(), E> {
|
||||||
|
if arg.trim().is_empty() {
|
||||||
|
let reply = self.formulate_reply(ctx, bot, false);
|
||||||
|
ctx.reply(msg.id, reply).await?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Show how long the bot has been online.
|
/// Show how long the bot has been online.
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
|
|
@ -74,24 +113,7 @@ where
|
||||||
ctx: &Context,
|
ctx: &Context,
|
||||||
bot: &mut B,
|
bot: &mut B,
|
||||||
) -> Result<(), E> {
|
) -> Result<(), E> {
|
||||||
let start = bot.start_time();
|
let reply = self.formulate_reply(ctx, bot, args.connected);
|
||||||
let now = OffsetDateTime::now_utc();
|
|
||||||
|
|
||||||
let mut reply = format!(
|
|
||||||
"/me has been up since {} ({})",
|
|
||||||
format_time(start),
|
|
||||||
format_duration(start - now),
|
|
||||||
);
|
|
||||||
|
|
||||||
if args.connected {
|
|
||||||
let since = ctx.joined.since;
|
|
||||||
reply.push_str(&format!(
|
|
||||||
", connected since {} ({})",
|
|
||||||
format_time(since),
|
|
||||||
format_duration(since - now),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.reply(msg.id, reply).await?;
|
ctx.reply(msg.id, reply).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue