From c380e71dda5802895ad58b5e7e222de16f5e4e3c Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 10 Mar 2024 14:29:47 -0600 Subject: [PATCH] Added mono service commands for all service actions + Made all commands ephemeral --- src/discord/mod.rs | 78 +++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/discord/mod.rs b/src/discord/mod.rs index 3c0acc9..087309c 100644 --- a/src/discord/mod.rs +++ b/src/discord/mod.rs @@ -3,8 +3,8 @@ use crate::models::service::{Service, ServiceGroup}; use j_db::database::Database; use j_db::model::JdbModel; use log::info; -use poise::serenity_prelude as serenity; use poise::serenity_prelude::MessageBuilder; +use poise::{serenity_prelude as serenity, ChoiceParameter}; #[allow(dead_code)] struct Data { @@ -14,7 +14,7 @@ struct Data { type Error = Box; type Context<'a> = poise::Context<'a, Data, Error>; -#[poise::command(slash_command)] +#[poise::command(slash_command, ephemeral)] async fn add_service( ctx: Context<'_>, #[description = "Service unit name to add"] service_name: String, @@ -41,7 +41,7 @@ async fn add_service( Ok(()) } -#[poise::command(slash_command)] +#[poise::command(slash_command, ephemeral)] async fn add_service_group( ctx: Context<'_>, #[description = "New group name"] group_name: String, @@ -54,7 +54,7 @@ async fn add_service_group( Ok(()) } -#[poise::command(slash_command)] +#[poise::command(slash_command, ephemeral)] async fn add_service_to_group( ctx: Context<'_>, #[description = "Service name"] service_name: String, @@ -83,7 +83,7 @@ async fn add_service_to_group( Ok(()) } -#[poise::command(slash_command)] +#[poise::command(slash_command, ephemeral)] async fn list_services( ctx: Context<'_>, #[description = "Service group name"] group: Option, @@ -131,47 +131,48 @@ async fn list_services( Ok(()) } -#[poise::command(slash_command)] -async fn restart_service( - ctx: Context<'_>, - #[description = "Service unit name"] service_name: String, -) -> Result<(), Error> { - let service: Option = ctx - .data() - .db - .filter(|_, s: &Service| s.name.contains(&service_name))? - .next(); - - if let Some(service) = service { - systemctl::restart(&service.name)?; - ctx.reply(format!("`{}` has been restarted", service_name)) - .await?; - } else { - ctx.reply(format!("Unknown service `{}`", service_name)) - .await?; - } - - Ok(()) +#[derive(Debug, Clone, ChoiceParameter)] +pub enum ServiceAction { + Start, + Stop, + Restart, + Status, } -#[poise::command(slash_command)] -async fn service_status( +#[poise::command(slash_command, ephemeral)] +async fn service( ctx: Context<'_>, + #[description = "Action to preform on service"] action: ServiceAction, #[description = "Service unit name"] service_name: String, ) -> Result<(), Error> { - let service: Option = ctx - .data() - .db - .filter(|_, s: &Service| s.name.contains(&service_name))? - .next(); + let service: Option = Service::find_service_by_name(&ctx.data().db, &service_name)?; if let Some(service) = service { - let status = systemctl::status(&service.name)?; - let mut msg = MessageBuilder::new(); + match action { + ServiceAction::Start => { + systemctl::start(&service_name)?; + ctx.reply(format!("`{}` has been started", service_name)) + .await?; + } + ServiceAction::Stop => { + systemctl::stop(&service_name)?; + ctx.reply(format!("`{}` has been stopped", service_name)) + .await?; + } + ServiceAction::Restart => { + systemctl::stop(&service_name)?; + ctx.reply(format!("`{}` has been restarted", service_name)) + .await?; + } + ServiceAction::Status => { + let status = systemctl::status(&service.name)?; + let mut msg = MessageBuilder::new(); - msg.push_codeblock_safe(status, None); + msg.push_codeblock_safe(status, None); - ctx.reply(msg.build()).await?; + ctx.reply(msg.build()).await?; + } + } } else { ctx.reply(format!("Unknown service `{}`", service_name)) .await?; @@ -194,9 +195,8 @@ pub async fn run_bot(db: Database, config: DaemonConfig) { list_services(), add_service(), add_service_group(), - restart_service(), - service_status(), add_service_to_group(), + service(), ], ..Default::default() })