Port mathbot
This commit is contained in:
parent
324959faad
commit
5d6b67baa8
|
@ -1,42 +0,0 @@
|
|||
use arrayvec::ArrayString;
|
||||
use meval::Context;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Write;
|
||||
use crate::bot::NormalCommand;
|
||||
use async_trait::async_trait;
|
||||
|
||||
pub struct Waifu;
|
||||
|
||||
#[async_trait]
|
||||
impl NormalCommand for Waifu {
|
||||
async fn execute(&mut self, _last_msg: &HashMap<String, String>, message: String) -> anyhow::Result<String> {
|
||||
let api_resp = reqwest::get(format!("https://api.waifu.pics/sfw/{}", message))
|
||||
.await?
|
||||
.text()
|
||||
.await?;
|
||||
let api_resp = api_resp.trim();
|
||||
let value: Value = serde_json::from_str(api_resp)?;
|
||||
let url = value["url"].as_str().unwrap_or("Invalid API Response.").to_string();
|
||||
Ok(url)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mathbot(
|
||||
author: String,
|
||||
expr: Option<&str>,
|
||||
last_evals: &mut HashMap<String, f64>,
|
||||
) -> anyhow::Result<ArrayString<256>> {
|
||||
if let Some(expr) = expr {
|
||||
let last_eval = last_evals.entry(author).or_insert(0.0);
|
||||
let mut meval_ctx = Context::new();
|
||||
let mut result = ArrayString::new();
|
||||
let value = meval::eval_str_with_context(expr, meval_ctx.var("x", *last_eval))?;
|
||||
*last_eval = value;
|
||||
tracing::debug!("{} = {}", expr, value);
|
||||
write!(result, "{} = {}", expr, value)?;
|
||||
Ok(result)
|
||||
} else {
|
||||
Ok(ArrayString::from("No expression to evaluate")?)
|
||||
}
|
||||
}
|
24
src/commands/eval.rs
Normal file
24
src/commands/eval.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use std::collections::HashMap;
|
||||
use async_trait::async_trait;
|
||||
use meval::Context;
|
||||
use crate::bot::{Command, Message};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Eval {
|
||||
last_eval: HashMap<String, f64>
|
||||
}
|
||||
|
||||
impl Command for Eval {
|
||||
//noinspection RsNeedlessLifetimes
|
||||
async fn execute<'a>(&mut self, msg: Message<'a>) -> anyhow::Result<String> {
|
||||
if let Some(expr) = msg.content {
|
||||
let last_eval = self.last_eval.entry(author).or_insert(0.0);
|
||||
let mut meval_ctx = Context::new();
|
||||
let value = meval::eval_str_with_context(expr, meval_ctx.var("x", *last_eval))?;
|
||||
*last_eval = value;
|
||||
Ok(format!("{} = {}", expr, value))
|
||||
} else {
|
||||
Ok("No expression to evaluate".into())
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,5 +2,6 @@ pub mod help;
|
|||
pub mod leek;
|
||||
pub mod waifu;
|
||||
pub mod sed;
|
||||
pub mod eval;
|
||||
#[cfg(feature = "debug")]
|
||||
pub mod debug;
|
|
@ -7,7 +7,7 @@ pub struct Sed;
|
|||
#[async_trait]
|
||||
impl Trigger for Sed {
|
||||
async fn execute<'a>(&mut self, msg: Message<'a>, matches: Captures<'a>) -> anyhow::Result<String> {
|
||||
let mut foreign_author;
|
||||
let foreign_author;
|
||||
let author = if let Some(author) = matches.name("u").map(|m| m.as_str()) {
|
||||
foreign_author = true;
|
||||
author
|
||||
|
|
Loading…
Reference in a new issue