reminder-bot/src/commands/pause.rs

86 lines
2.6 KiB
Rust
Raw Normal View History

2024-03-24 20:23:16 +00:00
use chrono::DateTime;
2024-02-17 20:24:30 +00:00
use serde::{Deserialize, Serialize};
2024-02-17 18:55:16 +00:00
use crate::{
models::CtxData,
time_parser::natural_parser,
utils::{Extract, Recordable},
Context, Error,
};
2024-02-17 18:55:16 +00:00
2024-02-17 20:24:30 +00:00
#[derive(Serialize, Deserialize, Extract)]
pub struct Options {
until: Option<String>,
}
impl Recordable for Options {
async fn run(self, ctx: Context<'_>) -> Result<(), Error> {
let timezone = ctx.timezone().await;
2024-02-17 18:55:16 +00:00
let mut channel = ctx.channel_data().await.unwrap();
2024-02-17 18:55:16 +00:00
match self.until {
Some(until) => {
let parsed = natural_parser(&until, &timezone.to_string()).await;
2024-02-17 18:55:16 +00:00
if let Some(timestamp) = parsed {
2024-03-24 20:23:16 +00:00
match DateTime::from_timestamp(timestamp, 0) {
Some(dt) => {
channel.paused = true;
2024-03-24 20:23:16 +00:00
channel.paused_until = Some(dt.naive_utc());
2024-02-17 18:55:16 +00:00
channel.commit_changes(&ctx.data().database).await;
2024-02-17 18:55:16 +00:00
ctx.say(format!(
"Reminders in this channel have been silenced until **<t:{}:D>**",
timestamp
))
.await?;
}
2024-02-17 18:55:16 +00:00
None => {
ctx.say(
2024-02-17 18:55:16 +00:00
"Time processed could not be interpreted as `DateTime`. Please write the time as clearly as possible",
)
.await?;
}
2024-02-17 18:55:16 +00:00
}
} else {
ctx.say(
"Time could not be processed. Please write the time as clearly as possible",
)
.await?;
2024-02-17 18:55:16 +00:00
}
}
_ => {
channel.paused = !channel.paused;
channel.paused_until = None;
2024-02-17 18:55:16 +00:00
channel.commit_changes(&ctx.data().database).await;
2024-02-17 18:55:16 +00:00
if channel.paused {
ctx.say("Reminders in this channel have been silenced indefinitely").await?;
} else {
ctx.say("Reminders in this channel have been unsilenced").await?;
}
2024-02-17 18:55:16 +00:00
}
}
Ok(())
}
2024-02-17 18:55:16 +00:00
}
2024-02-17 20:24:30 +00:00
/// Pause all reminders on the current channel until a certain time or indefinitely
#[poise::command(
slash_command,
rename = "pause",
identifying_name = "pause",
default_member_permissions = "MANAGE_GUILD"
)]
2024-02-17 20:24:30 +00:00
pub async fn command(
ctx: Context<'_>,
#[description = "When to pause until"] until: Option<String>,
) -> Result<(), Error> {
(Options { until }).run(ctx).await
2024-02-17 20:24:30 +00:00
}