mirror of
https://github.com/mii443/fendbot.git
synced 2025-08-22 23:15:33 +00:00
support async command execution
This commit is contained in:
Binary file not shown.
@ -1,3 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::{context::save_context, Context, Error};
|
||||
|
||||
#[poise::command(prefix_command)]
|
||||
@ -8,22 +12,44 @@ pub async fn register(ctx: Context<'_>) -> Result<(), Error> {
|
||||
|
||||
#[poise::command(prefix_command, slash_command)]
|
||||
pub async fn calc(ctx: Context<'_>, expr: String) -> Result<(), Error> {
|
||||
let mut data = ctx.data().context.lock().await;
|
||||
ctx.defer().await.unwrap();
|
||||
|
||||
let author = ctx.author().id.get();
|
||||
|
||||
if !data.contains_key(&author) {
|
||||
let mut context = fend_core::Context::new();
|
||||
context.set_random_u32_fn(rand::random);
|
||||
data.insert(author, fend_core::Context::new());
|
||||
let context = {
|
||||
let mut data = ctx.data().context.lock().await;
|
||||
|
||||
if !data.contains_key(&author) {
|
||||
let mut context = fend_core::Context::new();
|
||||
context.set_random_u32_fn(rand::random);
|
||||
data.insert(author, fend_core::Context::new());
|
||||
}
|
||||
data.get(&ctx.author().id.get()).unwrap().clone()
|
||||
};
|
||||
let context = Arc::new(Mutex::new(context));
|
||||
|
||||
let result = tokio::task::spawn_blocking({
|
||||
let context = context.clone();
|
||||
let expr = expr.clone();
|
||||
move || async move {
|
||||
let mut context = context.lock().await;
|
||||
let eval_result = fend_core::evaluate(&expr, &mut context).unwrap();
|
||||
let main_result = eval_result.get_main_result().to_string();
|
||||
main_result
|
||||
}
|
||||
})
|
||||
.await?
|
||||
.await;
|
||||
|
||||
ctx.reply(format!("> {}\n{}", expr, result)).await.unwrap();
|
||||
|
||||
{
|
||||
let mut data = ctx.data().context.lock().await;
|
||||
let context = context.lock().await;
|
||||
data.insert(author, context.clone());
|
||||
|
||||
save_context(&context, author);
|
||||
}
|
||||
let mut context = data.get_mut(&ctx.author().id.get()).unwrap();
|
||||
|
||||
let result = fend_core::evaluate(&expr, &mut context).unwrap();
|
||||
let result = result.get_main_result();
|
||||
|
||||
ctx.reply(result).await.unwrap();
|
||||
|
||||
save_context(context, author);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user