diff options
Diffstat (limited to 'src/commands/cron.rs')
| -rw-r--r-- | src/commands/cron.rs | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/src/commands/cron.rs b/src/commands/cron.rs new file mode 100644 index 0000000..48f6ff1 --- /dev/null +++ b/src/commands/cron.rs | |||
| @@ -0,0 +1,155 @@ | |||
| 1 | use poise::serenity_prelude as serenity; | ||
| 2 | |||
| 3 | use super::autocomplete::audio_file; | ||
| 4 | use crate::Data; | ||
| 5 | use crate::database; | ||
| 6 | use crate::error::{Error, Result}; | ||
| 7 | use crate::lang; | ||
| 8 | |||
| 9 | type Context<'a> = poise::Context<'a, Data, Error>; | ||
| 10 | |||
| 11 | /// Cron job management | ||
| 12 | #[poise::command( | ||
| 13 | slash_command, | ||
| 14 | subcommands("dodaj_audio", "dodaj_wiadomość", "pokaż", "usuń", "pauza", "wznów",) | ||
| 15 | )] | ||
| 16 | pub async fn cron(_ctx: Context<'_>) -> Result<()> { | ||
| 17 | Ok(()) | ||
| 18 | } | ||
| 19 | |||
| 20 | /// Schedule audio playback | ||
| 21 | #[poise::command(slash_command)] | ||
| 22 | pub async fn dodaj_audio( | ||
| 23 | ctx: Context<'_>, | ||
| 24 | #[description = "Cron expression (e.g. \"37 21 * * *\" = every day at 21:37)"] cron_expression: String, | ||
| 25 | #[description = "Target voice channel"] channel: serenity::Channel, | ||
| 26 | #[description = "Copyparty URL of audio file"] | ||
| 27 | #[autocomplete = "audio_file"] | ||
| 28 | audio_filename: String, | ||
| 29 | ) -> Result<()> { | ||
| 30 | let channel_id = match channel { | ||
| 31 | serenity::Channel::Guild(ch) => ch.id.get() as i64, | ||
| 32 | _ => return Err(lang::err_bad_channel_type()), | ||
| 33 | }; | ||
| 34 | |||
| 35 | database::add_cron_job( | ||
| 36 | &ctx.data().db_pool, | ||
| 37 | &cron_expression, | ||
| 38 | "audio", | ||
| 39 | channel_id, | ||
| 40 | Some(&audio_filename), | ||
| 41 | None, | ||
| 42 | ) | ||
| 43 | .await?; | ||
| 44 | |||
| 45 | ctx.say(lang::fmt_cron_audio_queued(&cron_expression)) | ||
| 46 | .await?; | ||
| 47 | Ok(()) | ||
| 48 | } | ||
| 49 | |||
| 50 | /// Schedule a periodic message | ||
| 51 | #[poise::command(slash_command)] | ||
| 52 | pub async fn dodaj_wiadomość( | ||
| 53 | ctx: Context<'_>, | ||
| 54 | #[description = "Cron expression (e.g. \"37 21 * * *\" = every day at 21:37)"] cron_expression: String, | ||
| 55 | #[description = "Docelowy kanał tekstowy"] channel: serenity::Channel, | ||
| 56 | #[description = "Zawartość wiadomości"] message: String, | ||
| 57 | ) -> Result<()> { | ||
| 58 | let channel_id = match channel { | ||
| 59 | serenity::Channel::Guild(ch) => ch.id.get() as i64, | ||
| 60 | _ => return Err(lang::err_bad_channel_type()), | ||
| 61 | }; | ||
| 62 | |||
| 63 | database::add_cron_job( | ||
| 64 | &ctx.data().db_pool, | ||
| 65 | &cron_expression, | ||
| 66 | "wiadomość", | ||
| 67 | channel_id, | ||
| 68 | None, | ||
| 69 | Some(&message), | ||
| 70 | ) | ||
| 71 | .await?; | ||
| 72 | |||
| 73 | ctx.say(lang::L_CRON_QUEUED).await?; | ||
| 74 | Ok(()) | ||
| 75 | } | ||
| 76 | |||
| 77 | /// List all cron jobs | ||
| 78 | #[poise::command(slash_command)] | ||
| 79 | pub async fn pokaż(ctx: Context<'_>) -> Result<()> { | ||
| 80 | let jobs = database::list_cron_jobs(&ctx.data().db_pool).await?; | ||
| 81 | if jobs.is_empty() { | ||
| 82 | ctx.say(lang::L_CRON_NONE).await?; | ||
| 83 | return Ok(()); | ||
| 84 | } | ||
| 85 | |||
| 86 | let mut embed = serenity::CreateEmbed::default() | ||
| 87 | .title(lang::L_CRON_TITLE) | ||
| 88 | .color(0x00aaff); | ||
| 89 | |||
| 90 | for job in &jobs { | ||
| 91 | let status = if job.active == 1 { | ||
| 92 | lang::L_CRON_ACTIVE | ||
| 93 | } else { | ||
| 94 | lang::L_CRON_PAUSED | ||
| 95 | }; | ||
| 96 | let desc = match job.r#type.as_str() { | ||
| 97 | "audio" => lang::fmt_cron_label_audio(job.audio_filename.as_deref().unwrap_or("?")), | ||
| 98 | _ => lang::fmt_cron_label_msg(job.message.as_deref().unwrap_or("?")), | ||
| 99 | }; | ||
| 100 | embed = embed.field( | ||
| 101 | lang::fmt_cron_job_status(job.id, status), | ||
| 102 | lang::fmt_cron_field(&job.cron_expression, job.channel_id, &desc), | ||
| 103 | false, | ||
| 104 | ); | ||
| 105 | } | ||
| 106 | |||
| 107 | ctx.send(poise::CreateReply::default().embed(embed)).await?; | ||
| 108 | Ok(()) | ||
| 109 | } | ||
| 110 | |||
| 111 | /// Remove a cron job | ||
| 112 | #[poise::command(slash_command)] | ||
| 113 | pub async fn usuń( | ||
| 114 | ctx: Context<'_>, | ||
| 115 | #[description = "ID pracy do usunięcia"] job_id: i64, | ||
| 116 | ) -> Result<()> { | ||
| 117 | if database::delete_cron_job(&ctx.data().db_pool, job_id).await? { | ||
| 118 | ctx.say(lang::fmt_cron_deleted(job_id)).await?; | ||
| 119 | } else { | ||
| 120 | ctx.say(lang::fmt_cron_not_found(job_id)).await?; | ||
| 121 | } | ||
| 122 | Ok(()) | ||
| 123 | } | ||
| 124 | |||
| 125 | /// Pause a cron job | ||
| 126 | #[poise::command(slash_command)] | ||
| 127 | pub async fn pauza( | ||
| 128 | ctx: Context<'_>, | ||
| 129 | #[description = "ID pracy do zapauzowania"] job_id: i64, | ||
| 130 | ) -> Result<()> { | ||
| 131 | if database::set_cron_job_active(&ctx.data().db_pool, job_id, false).await? { | ||
| 132 | ctx.say(lang::fmt_cron_paused(job_id)).await?; | ||
| 133 | } else { | ||
| 134 | ctx.say(lang::fmt_cron_not_found(job_id)).await?; | ||
| 135 | } | ||
| 136 | Ok(()) | ||
| 137 | } | ||
| 138 | |||
| 139 | /// Resume a paused cron job | ||
| 140 | #[poise::command(slash_command)] | ||
| 141 | pub async fn wznów( | ||
| 142 | ctx: Context<'_>, | ||
| 143 | #[description = "ID pracy cron do wznowienia"] job_id: i64, | ||
| 144 | ) -> Result<()> { | ||
| 145 | if database::set_cron_job_active(&ctx.data().db_pool, job_id, true).await? { | ||
| 146 | ctx.say(lang::fmt_cron_resumed(job_id)).await?; | ||
| 147 | } else { | ||
| 148 | ctx.say(lang::fmt_cron_not_found(job_id)).await?; | ||
| 149 | } | ||
| 150 | Ok(()) | ||
| 151 | } | ||
| 152 | |||
| 153 | pub fn commands() -> Vec<poise::Command<Data, Error>> { | ||
| 154 | vec![cron()] | ||
| 155 | } | ||
