Get all variables command.

This commit is contained in:
projectmoon 2020-10-22 20:29:37 +00:00
parent 314b0520d9
commit 0c394d0f79
3 changed files with 35 additions and 3 deletions

View File

@ -144,7 +144,7 @@ impl DiceBot {
error!("Error sending message: {}", e.to_string()); error!("Error sending message: {}", e.to_string());
}; };
} else { } else {
let message = format!("Executed {} commands", results.len()); let message = format!("{}: Executed {} commands", sender_username, results.len());
let response = AnyMessageEventContent::RoomMessage(MessageEventContent::Notice( let response = AnyMessageEventContent::RoomMessage(MessageEventContent::Notice(
NoticeMessageEventContent::html(&message, &message), NoticeMessageEventContent::html(&message, &message),
)); ));

View File

@ -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::<Vec<_>>()
.join("\n"),
Err(e) => format!("error getting variables: {}", e),
};
let plain = format!("Variables:\n{}", value);
let html = format!(
"<p><strong>Variables:</strong><br/>{}",
value.replace("\n", "<br/>")
);
Execution { plain, html }
}
}
pub struct GetVariableCommand(String); pub struct GetVariableCommand(String);
#[async_trait] #[async_trait]

View File

@ -1,7 +1,7 @@
use crate::cofd::parser::{create_chance_die, parse_dice_pool}; use crate::cofd::parser::{create_chance_die, parse_dice_pool};
use crate::commands::{ use crate::commands::{
Command, DeleteVariableCommand, GetVariableCommand, HelpCommand, PoolRollCommand, RollCommand, Command, DeleteVariableCommand, GetAllVariablesCommand, GetVariableCommand, HelpCommand,
SetVariableCommand, PoolRollCommand, RollCommand, SetVariableCommand,
}; };
use crate::dice::parser::parse_element_expression; use crate::dice::parser::parse_element_expression;
use crate::error::BotError; use crate::error::BotError;
@ -47,6 +47,10 @@ fn chance_die() -> Result<Box<dyn Command>, BotError> {
Ok(Box::new(PoolRollCommand(pool))) Ok(Box::new(PoolRollCommand(pool)))
} }
fn get_all_variables() -> Result<Box<dyn Command>, BotError> {
Ok(Box::new(GetAllVariablesCommand))
}
fn help(topic: &str) -> Result<Box<dyn Command>, BotError> { fn help(topic: &str) -> Result<Box<dyn Command>, BotError> {
let topic = parse_help_topic(topic); let topic = parse_help_topic(topic);
Ok(Box::new(HelpCommand(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<Option<Box<dyn Command>>, BotError> { pub fn parse_command(input: &str) -> Result<Option<Box<dyn Command>>, BotError> {
match split_command(input) { match split_command(input) {
Ok((cmd, cmd_input)) => match cmd.as_ref() { 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)), "get" => parse_get_variable_command(&cmd_input).map(|command| Some(command)),
"set" => parse_set_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)), "del" => parse_delete_variable_command(&cmd_input).map(|command| Some(command)),