From 9330fd209eec1e06c6358b678026aa7ce605c92b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20H=C3=A4cker?= Date: Sun, 19 Sep 2021 18:08:08 +0200 Subject: [PATCH] Implement new !details command --- README.md | 3 ++- src/bot.rs | 27 +++++++++++++++++++++++++++ src/project.rs | 14 ++++++++++++++ src/section.rs | 12 ++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 77223df..c3f19f3 100644 --- a/README.md +++ b/README.md @@ -32,12 +32,13 @@ In this closed room administrative commands can be executed. | Command | Description | | --------------- | ---------------------------------------------------------- | | !clear | Clears all stored news | +| !details "term" | Shows section/project details (term can be emoji or name) | | !list-config | Lists current bot configuration | | !list-projects | Lists configured projects | | !list-sections | Lists configured sections | | !render | Creates a markdown file with the stored news | | !restart | Restarts the bot, useful when you edited the configuration | -| !say "message" | Sends a message in reporting room | +| !say "message" | Sends a message in reporting room | | !status | Shows saved messages | ### Configuration diff --git a/src/bot.rs b/src/bot.rs index dee439c..34ae48a 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -607,6 +607,7 @@ impl EventCallback { match command { "!clear" => self.clear_command().await, + "!details" => self.details_command(&args).await, "!help" => self.help_command().await, "!list-config" => self.list_config_command().await, "!list-projects" => self.list_projects_command().await, @@ -622,6 +623,7 @@ impl EventCallback { async fn help_command(&self) { let help = "Available commands: \n\n\ !clear \n\ + !details \n\ !list-config \n\ !list-projects \n\ !list-sections \n\ @@ -650,6 +652,31 @@ impl EventCallback { .await; } + async fn details_command(&self, term: &str) { + let result_project = self.0.config.project_by_name(term); + let result_section = self.0.config.section_by_name(term); + let result_reaction = self.0.config.reaction_type_by_emoji(term); + + let msg = if let Some(project) = result_project { + project.html_details() + } else if let Some(section) = result_section { + section.html_details() + } else { + match result_reaction { + ReactionType::Approval => format!("{} is configured as approval emoji.", term), + ReactionType::Section(section) => section.unwrap().html_details(), + ReactionType::Project(project) => project.unwrap().html_details(), + ReactionType::Image => format!("{} is configured as image emoji.", term), + ReactionType::Video => format!("{} is configured as video emoji.", term), + ReactionType::None => format!("❌ Unable to find details for ”{}”.", term), + } + }; + + self.0 + .send_message(&msg, BotMsgType::AdminRoomHtmlNotice) + .await; + } + async fn list_config_command(&self) { let mut config = self.0.config.clone(); diff --git a/src/project.rs b/src/project.rs index 9dd2fbc..1374177 100644 --- a/src/project.rs +++ b/src/project.rs @@ -9,3 +9,17 @@ pub struct Project { pub website: String, pub default_section: String, } + +impl Project { + pub fn html_details(&self) -> String { + format!( + "Project Details
\ + Emoji: {}
\ + Name: {} ({})
\ + Description: {}
\ + Website: {}
\ + Default Section: {}", + self.emoji, self.title, self.name, self.description, self.website, self.default_section + ) + } +} diff --git a/src/section.rs b/src/section.rs index 85c7fa7..1488233 100644 --- a/src/section.rs +++ b/src/section.rs @@ -9,6 +9,18 @@ pub struct Section { pub order: u32, } +impl Section { + pub fn html_details(&self) -> String { + format!( + "Section Details
\ + Emoji: {}
\ + Name: {} ({})
\ + Order: {}", + self.emoji, self.title, self.name, self.order + ) + } +} + impl PartialOrd for Section { fn partial_cmp(&self, other: &Self) -> Option { Some(self.order.cmp(&other.order))