tenebrous-dicebot/dicebot/src/commands/variables.rs

162 lines
4.1 KiB
Rust
Raw Normal View History

use super::{Command, Execution, ExecutionResult};
2020-10-31 12:40:44 +00:00
use crate::context::Context;
use crate::db::errors::DataError;
use crate::db::Variables;
use crate::error::BotError;
2020-10-31 12:40:44 +00:00
use async_trait::async_trait;
use std::convert::TryFrom;
2020-10-31 12:40:44 +00:00
pub struct GetAllVariablesCommand;
impl TryFrom<String> for GetAllVariablesCommand {
type Error = BotError;
fn try_from(_: String) -> Result<Self, Self::Error> {
Ok(GetAllVariablesCommand)
}
}
2020-10-31 12:40:44 +00:00
#[async_trait]
impl Command for GetAllVariablesCommand {
fn name(&self) -> &'static str {
"get all variables"
}
fn is_secure(&self) -> bool {
false
}
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
let variables = ctx
.db
2021-05-30 14:17:33 +00:00
.get_user_variables(&ctx.username, ctx.active_room_id().as_str())
.await?;
2020-10-31 12:40:44 +00:00
let mut variable_list: Vec<String> = variables
.into_iter()
.map(|(name, value)| format!(" - {} = {}", name, value))
.collect();
2020-10-31 12:40:44 +00:00
variable_list.sort();
let value = variable_list.join("\n");
2020-10-31 12:40:44 +00:00
let html = format!(
"<strong>Variables:</strong><br/>{}",
2020-10-31 12:40:44 +00:00
value.replace("\n", "<br/>")
);
Execution::success(html)
2020-10-31 12:40:44 +00:00
}
}
pub struct GetVariableCommand(pub String);
impl TryFrom<String> for GetVariableCommand {
type Error = BotError;
fn try_from(input: String) -> Result<Self, Self::Error> {
Ok(GetVariableCommand(input))
}
}
2020-10-31 12:40:44 +00:00
#[async_trait]
impl Command for GetVariableCommand {
fn name(&self) -> &'static str {
"retrieve variable value"
}
fn is_secure(&self) -> bool {
false
}
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
2020-10-31 12:40:44 +00:00
let name = &self.0;
let result = ctx
.db
2021-05-30 14:17:33 +00:00
.get_user_variable(&ctx.username, ctx.active_room_id().as_str(), name)
.await;
2020-10-31 12:40:44 +00:00
let value = match result {
Ok(num) => format!("{} = {}", name, num),
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not set", name),
Err(e) => return Err(e.into()),
2020-10-31 12:40:44 +00:00
};
let html = format!("<strong>Variable:</strong> {}", value);
Execution::success(html)
2020-10-31 12:40:44 +00:00
}
}
pub struct SetVariableCommand(pub String, pub i32);
impl TryFrom<String> for SetVariableCommand {
type Error = BotError;
fn try_from(input: String) -> Result<Self, Self::Error> {
let (variable_name, value) = crate::parser::variables::parse_set_variable(&input)?;
Ok(SetVariableCommand(variable_name, value))
}
}
2020-10-31 12:40:44 +00:00
#[async_trait]
impl Command for SetVariableCommand {
fn name(&self) -> &'static str {
"set variable value"
}
fn is_secure(&self) -> bool {
false
}
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
2020-10-31 12:40:44 +00:00
let name = &self.0;
let value = self.1;
ctx.db
2021-05-30 14:17:33 +00:00
.set_user_variable(&ctx.username, ctx.active_room_id().as_str(), name, value)
.await?;
2020-10-31 12:40:44 +00:00
let content = format!("{} = {}", name, value);
let html = format!("<strong>Set Variable:</strong> {}", content);
Execution::success(html)
2020-10-31 12:40:44 +00:00
}
}
pub struct DeleteVariableCommand(pub String);
impl TryFrom<String> for DeleteVariableCommand {
type Error = BotError;
fn try_from(input: String) -> Result<Self, Self::Error> {
Ok(DeleteVariableCommand(input))
}
}
2020-10-31 12:40:44 +00:00
#[async_trait]
impl Command for DeleteVariableCommand {
fn name(&self) -> &'static str {
"delete variable"
}
fn is_secure(&self) -> bool {
false
}
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
2020-10-31 12:40:44 +00:00
let name = &self.0;
let result = ctx
.db
2021-05-30 14:17:33 +00:00
.delete_user_variable(&ctx.username, ctx.active_room_id().as_str(), name)
.await;
2020-10-31 12:40:44 +00:00
let value = match result {
Ok(()) => format!("{} now unset", name),
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not currently set", name),
Err(e) => return Err(e.into()),
2020-10-31 12:40:44 +00:00
};
let html = format!("<strong>Remove Variable:</strong> {}", value);
Execution::success(html)
2020-10-31 12:40:44 +00:00
}
}