diff --git a/src/callbacks.rs b/src/callbacks.rs index c3c4f70..d58ced2 100644 --- a/src/callbacks.rs +++ b/src/callbacks.rs @@ -28,23 +28,31 @@ impl FuukaBotCallbacks { if room.state() != RoomState::Joined { return Ok(()); } - let client = room.client(); - let user_id = client.user_id().unwrap(); - if ev.sender == user_id { - return Ok(()); - } - let body = remove_plain_reply_fallback(ev.content.body()).trim(); - if let Some(commands) = body.strip_prefix(&ctx.config.command_prefix) { - if let Err(e) = - fuuka_bot_dispatch_command(ev.clone(), room.clone(), commands, client.homeserver()) - .await - { - send_error_message(ev, room, e).await?; + tokio::spawn(async move { + let client = room.client(); + let user_id = client.user_id().unwrap(); + if ev.sender == user_id { + return Ok(()); + } + + let body = remove_plain_reply_fallback(ev.content.body()).trim(); + if let Some(commands) = body.strip_prefix(&ctx.config.command_prefix) { + if let Err(e) = fuuka_bot_dispatch_command( + ev.clone(), + room.clone(), + commands, + client.homeserver(), + ) + .await + { + send_error_message(ev, room, e).await?; + } } - } - Ok(()) + Ok(()) + }) + .await? } pub async fn on_stripped_member(ev: StrippedRoomMemberEvent, room: Room) { diff --git a/src/dicer.rs b/src/dicer.rs index 120321b..e82ba02 100644 --- a/src/dicer.rs +++ b/src/dicer.rs @@ -94,7 +94,8 @@ impl Expr { Self::DiceOrInt(result) => match result { DiceOrInt::Dice(dice) => { let Dice { count, sides } = dice; - Ok((fastrand::u32(1..=sides) * count) as i32) + //Ok((fastrand::u32(1..=sides) * count) as i32) + Ok((0..count).fold(0, |acc, _| acc + fastrand::u32(1..=sides)) as i32) } DiceOrInt::Int(num) => Ok(num), }, diff --git a/src/message_responses.rs b/src/message_responses.rs index 96008c5..366fabe 100644 --- a/src/message_responses.rs +++ b/src/message_responses.rs @@ -25,32 +25,36 @@ impl FuukaBotMessages { return Ok(()); } - let body = remove_plain_reply_fallback(ev.content.body()).trim(); - let mut splited = body.split_whitespace(); - // If the first part of the message is pure ASCII, skip it - if splited.next().unwrap().is_ascii() { - return Ok(()); - }; - - let from_sender = &ev.sender; - let Some(to_sender) = get_reply_target(&ev, &room).await? else { - return Ok(()); - }; - - let Some(content) = _dispatch_jerryxiao(&room, body, from_sender, &to_sender).await? else { - return Ok(()); - }; - - let content = content - .make_reply_to( - &ev.into_full_event(room.room_id().into()), - ForwardThread::Yes, - AddMentions::Yes, - ) - .add_mentions(Mentions::with_user_ids([to_sender])); - room.send(content).await?; - - Ok(()) + tokio::spawn(async move { + let body = remove_plain_reply_fallback(ev.content.body()).trim(); + let mut splited = body.split_whitespace(); + // If the first part of the message is pure ASCII, skip it + if splited.next().unwrap().is_ascii() { + return Ok(()); + }; + + let from_sender = &ev.sender; + let Some(to_sender) = get_reply_target(&ev, &room).await? else { + return Ok(()); + }; + + let Some(content) = _dispatch_jerryxiao(&room, body, from_sender, &to_sender).await? + else { + return Ok(()); + }; + + let content = content + .make_reply_to( + &ev.into_full_event(room.room_id().into()), + ForwardThread::Yes, + AddMentions::Yes, + ) + .add_mentions(Mentions::with_user_ids([to_sender])); + room.send(content).await?; + + Ok(()) + }) + .await? } /// The callback handler for randomdraw. @@ -60,18 +64,21 @@ impl FuukaBotMessages { return Ok(()); } - let body = remove_plain_reply_fallback(ev.content.body()).trim(); - let Some(content) = _dispatch_randomdraw(&ev, &room, body).await? else { - return Ok(()); - }; + tokio::spawn(async move { + let body = remove_plain_reply_fallback(ev.content.body()).trim(); + let Some(content) = _dispatch_randomdraw(&ev, &room, body).await? else { + return Ok(()); + }; - let content = content.make_reply_to( - &ev.into_full_event(room.room_id().into()), - ForwardThread::Yes, - AddMentions::Yes, - ); - room.send(content).await?; - Ok(()) + let content = content.make_reply_to( + &ev.into_full_event(room.room_id().into()), + ForwardThread::Yes, + AddMentions::Yes, + ); + room.send(content).await?; + Ok(()) + }) + .await? } /// The callback handler for dicer. @@ -80,25 +87,23 @@ impl FuukaBotMessages { if room.state() != RoomState::Joined { return Ok(()); } - let client = room.client(); - let user_id = client.user_id().unwrap(); - if ev.sender == user_id { - return Ok(()); - } - let body = remove_plain_reply_fallback(ev.content.body()).trim(); - let Some(content) = _dispatch_dicer(body).await? else { - return Ok(()); - }; + tokio::spawn(async move { + let body = remove_plain_reply_fallback(ev.content.body()).trim(); + let Some(content) = _dispatch_dicer(body).await? else { + return Ok(()); + }; - let content = content.make_reply_to( - &ev.into_full_event(room.room_id().into()), - ForwardThread::Yes, - AddMentions::Yes, - ); - room.send(content).await?; + let content = content.make_reply_to( + &ev.into_full_event(room.room_id().into()), + ForwardThread::Yes, + AddMentions::Yes, + ); + room.send(content).await?; - Ok(()) + Ok(()) + }) + .await? } }