Skip to content

Commit

Permalink
Implement new !details command
Browse files Browse the repository at this point in the history
  • Loading branch information
haecker-felix committed Sep 19, 2021
1 parent 029f2cd commit 9330fd2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -622,6 +623,7 @@ impl EventCallback {
async fn help_command(&self) {
let help = "Available commands: \n\n\
!clear \n\
!details <name> \n\
!list-config \n\
!list-projects \n\
!list-sections \n\
Expand Down Expand Up @@ -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();

Expand Down
14 changes: 14 additions & 0 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@ pub struct Project {
pub website: String,
pub default_section: String,
}

impl Project {
pub fn html_details(&self) -> String {
format!(
"<b>Project Details</b><br>\
<b>Emoji</b>: {} <br>\
<b>Name</b>: {} ({}) <br>\
<b>Description</b>: {} <br>\
<b>Website</b>: {} <br>\
<b>Default Section</b>: {}",
self.emoji, self.title, self.name, self.description, self.website, self.default_section
)
}
}
12 changes: 12 additions & 0 deletions src/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ pub struct Section {
pub order: u32,
}

impl Section {
pub fn html_details(&self) -> String {
format!(
"<b>Section Details</b><br>\
<b>Emoji</b>: {} <br>\
<b>Name</b>: {} ({}) <br>\
<b>Order</b>: {}",
self.emoji, self.title, self.name, self.order
)
}
}

impl PartialOrd for Section {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.order.cmp(&other.order))
Expand Down

0 comments on commit 9330fd2

Please sign in to comment.