Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowRZ committed Nov 16, 2023
1 parent bcaf942 commit aaf3d9b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 68 deletions.
36 changes: 22 additions & 14 deletions src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/dicer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down
111 changes: 58 additions & 53 deletions src/message_responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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?
}
}

Expand Down

0 comments on commit aaf3d9b

Please sign in to comment.