tenebrous-dicebot/src/commands/mod.rs

154 lines
4.5 KiB
Rust
Raw Normal View History

use crate::context::Context;
use crate::error::BotError;
2020-10-17 15:47:17 +00:00
use async_trait::async_trait;
use thiserror::Error;
use BotError::DataError;
2020-04-21 06:09:43 +00:00
2020-10-31 12:40:44 +00:00
pub mod basic_rolling;
pub mod cofd;
pub mod cthulhu;
pub mod management;
2020-10-31 12:40:44 +00:00
pub mod misc;
pub mod parser;
2020-10-31 12:40:44 +00:00
pub mod variables;
/// A custom error type specifically related to parsing command text.
/// Does not wrap an execution failure.
#[derive(Error, Debug)]
pub enum CommandError {
#[error("invalid command: {0}")]
InvalidCommand(String),
#[error("ignored command")]
IgnoredCommand,
}
/// A successfully executed command returns a message to be sent back
/// to the user in HTML (plain text used as a fallback by message
/// formatter).
#[derive(Debug)]
2020-04-21 06:07:03 +00:00
pub struct Execution {
html: String,
}
2020-04-21 06:07:03 +00:00
impl Execution {
pub fn success(html: String) -> ExecutionResult {
Ok(Execution { html })
}
/// Response message in HTML.
pub fn html(&self) -> String {
self.html.clone()
2020-04-21 06:07:03 +00:00
}
}
/// Wraps a command execution failure. Provides HTML formatting for
/// any error message from the BotError type, similar to how Execution
/// provides formatting for successfully executed commands.
#[derive(Error, Debug)]
#[error("{0}")]
pub struct ExecutionError(#[from] pub BotError);
impl From<crate::db::errors::DataError> for ExecutionError {
fn from(error: crate::db::errors::DataError) -> Self {
Self(DataError(error))
}
}
impl ExecutionError {
/// Error message in bolded HTML.
pub fn html(&self) -> String {
format!("<p><strong>{}</strong></p>", self.0)
}
}
/// Wraps either a successful command execution response, or an error
/// that occurred.
pub type ExecutionResult = Result<Execution, ExecutionError>;
/// Extract response messages out of a type, whether it is success or
/// failure.
pub trait ResponseExtractor {
/// HTML representation of the message, directly mentioning the
/// username.
fn message_html(&self, username: &str) -> String;
}
impl ResponseExtractor for ExecutionResult {
/// Error message in bolded HTML.
fn message_html(&self, username: &str) -> String {
// TODO use user display name too (element seems to render this
// without display name)
let username = format!(
"<a href=\"https://matrix.to/#/{}\">{}</a>",
username, username
);
match self {
Ok(resp) => format!("<p>{}</p><p>{}</p>", username, resp.html).replace("\n", "<br/>"),
Err(e) => format!("<p>{}</p><p>{}</p>", username, e.html()).replace("\n", "<br/>"),
}
}
}
/// The trait that any command that can be executed must implement.
2020-10-17 15:47:17 +00:00
#[async_trait]
pub trait Command: Send + Sync {
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult;
fn name(&self) -> &'static str;
fn is_secure(&self) -> bool;
2020-04-21 06:07:03 +00:00
}
/// Attempt to execute a command, and return the content that should
/// go back to Matrix, if the command was executed (successfully or
/// not). If a command is determined to be ignored, this function will
/// return None, signifying that we should not send a response.
pub async fn execute_command(ctx: &Context<'_>) -> ExecutionResult {
let cmd = parser::parse_command(&ctx.message_body)?;
cmd.execute(ctx).await
}
#[cfg(test)]
mod tests {
use super::*;
2021-05-13 19:48:29 +00:00
use url::Url;
macro_rules! dummy_room {
() => {
crate::context::RoomContext {
id: &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"),
display_name: "displayname",
}
};
}
#[test]
fn command_result_extractor_creates_bubble() {
let result = Execution::success("test".to_string());
let message = result.message_html("@myuser:example.com");
assert!(message.contains(
"<a href=\"https://matrix.to/#/@myuser:example.com\">@myuser:example.com</a>"
));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
2020-11-12 21:05:14 +00:00
async fn unrecognized_command() {
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
let db = crate::db::sqlite::Database::new(db_path.path().to_str().unwrap())
.await
.unwrap();
2021-05-13 19:48:29 +00:00
let homeserver = Url::parse("http://example.com").unwrap();
2020-11-22 20:52:44 +00:00
let ctx = Context {
db: db,
2021-05-13 19:48:29 +00:00
matrix_client: &matrix_sdk::Client::new(homeserver).unwrap(),
room: dummy_room!(),
2020-11-22 20:52:44 +00:00
username: "myusername",
message_body: "!notacommand",
};
2020-11-12 21:05:14 +00:00
let result = execute_command(&ctx).await;
assert!(result.is_err());
}
}