From 0c394d0f7932d3e1bec54aa6ac782a105a28f73c Mon Sep 17 00:00:00 2001 From: projectmoon Date: Thu, 22 Oct 2020 20:29:37 +0000 Subject: [PATCH] Get all variables command. --- src/bot.rs | 2 +- src/commands.rs | 27 +++++++++++++++++++++++++++ src/commands/parser.rs | 9 +++++++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 863de9c..18510fe 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -144,7 +144,7 @@ impl DiceBot { error!("Error sending message: {}", e.to_string()); }; } else { - let message = format!("Executed {} commands", results.len()); + let message = format!("{}: Executed {} commands", sender_username, results.len()); let response = AnyMessageEventContent::RoomMessage(MessageEventContent::Notice( NoticeMessageEventContent::html(&message, &message), )); diff --git a/src/commands.rs b/src/commands.rs index ffe6d2c..fc34bf1 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -111,6 +111,33 @@ impl Command for HelpCommand { } } +pub struct GetAllVariablesCommand; + +#[async_trait] +impl Command for GetAllVariablesCommand { + fn name(&self) -> &'static str { + "get all variables" + } + + async fn execute(&self, ctx: &Context) -> Execution { + let value = match ctx.db.get_user_variables(&ctx.room_id, &ctx.username).await { + Ok(variables) => variables + .into_iter() + .map(|(name, value)| format!(" - {} = {}", name, value)) + .collect::>() + .join("\n"), + Err(e) => format!("error getting variables: {}", e), + }; + + let plain = format!("Variables:\n{}", value); + let html = format!( + "

Variables:
{}", + value.replace("\n", "
") + ); + Execution { plain, html } + } +} + pub struct GetVariableCommand(String); #[async_trait] diff --git a/src/commands/parser.rs b/src/commands/parser.rs index c339f00..04e623e 100644 --- a/src/commands/parser.rs +++ b/src/commands/parser.rs @@ -1,7 +1,7 @@ use crate::cofd::parser::{create_chance_die, parse_dice_pool}; use crate::commands::{ - Command, DeleteVariableCommand, GetVariableCommand, HelpCommand, PoolRollCommand, RollCommand, - SetVariableCommand, + Command, DeleteVariableCommand, GetAllVariablesCommand, GetVariableCommand, HelpCommand, + PoolRollCommand, RollCommand, SetVariableCommand, }; use crate::dice::parser::parse_element_expression; use crate::error::BotError; @@ -47,6 +47,10 @@ fn chance_die() -> Result, BotError> { Ok(Box::new(PoolRollCommand(pool))) } +fn get_all_variables() -> Result, BotError> { + Ok(Box::new(GetAllVariablesCommand)) +} + fn help(topic: &str) -> Result, BotError> { let topic = parse_help_topic(topic); Ok(Box::new(HelpCommand(topic))) @@ -89,6 +93,7 @@ fn split_command(input: &str) -> Result<(String, String), BotError> { pub fn parse_command(input: &str) -> Result>, BotError> { match split_command(input) { Ok((cmd, cmd_input)) => match cmd.as_ref() { + "variables" => get_all_variables().map(|command| Some(command)), "get" => parse_get_variable_command(&cmd_input).map(|command| Some(command)), "set" => parse_set_variable_command(&cmd_input).map(|command| Some(command)), "del" => parse_delete_variable_command(&cmd_input).map(|command| Some(command)),