uberbot/src/commands/sed.rs
2022-07-17 20:10:34 +02:00

47 lines
1.4 KiB
Rust

use crate::bot::{Context, Trigger};
use async_trait::async_trait;
use fancy_regex::Captures;
pub struct Sed;
#[async_trait]
impl Trigger for Sed {
async fn execute<'a>(
&mut self,
msg: Context<'a>,
captures: Captures<'a>,
) -> anyhow::Result<String> {
let foreign_author;
let author = if let Some(author) = captures.name("u").map(|m| m.as_str()) {
foreign_author = true;
author
} else {
foreign_author = false;
msg.author
};
let message = if let Some(msg) = msg.history.last_msg(author).await {
msg
} else {
return Ok("No previous messages found.".into());
};
if let (Some(find), Some(replace)) = (captures.name("r"), captures.name("w")) {
// TODO: karx plz add flags
//let flags = matches.name("f").map(|m| m.as_str());
let result = message.replace(find.as_str(), replace.as_str());
if foreign_author {
Ok(format!(
"(edited by {}) <{}> {}",
msg.author, author, result
))
} else {
msg.history
.edit_message(author, 0, result.to_string())
.await;
Ok(format!("<{}> {}", author, result))
}
} else {
Ok("Invalid usage.".into())
}
}
}