mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-18 16:28:55 +00:00
Tune Pagination to be less buggy and more friendly
This commit is contained in:
parent
22668fe654
commit
33c6aae64d
2 changed files with 34 additions and 8 deletions
|
@ -8,6 +8,9 @@ use serenity::{
|
|||
},
|
||||
};
|
||||
|
||||
const ARROW_RIGHT: &'static str = "➡️";
|
||||
const ARROW_LEFT: &'static str = "⬅️";
|
||||
|
||||
impl ReactionWatcher {
|
||||
/// Start a pagination.
|
||||
///
|
||||
|
@ -24,6 +27,22 @@ impl ReactionWatcher {
|
|||
let handler = PaginationHandler::new(pager, ctx, channel)?;
|
||||
self.handle_reactions(handler, duration)
|
||||
}
|
||||
|
||||
/// A version of `paginate` that compiles for closures.
|
||||
///
|
||||
/// A workaround until https://github.com/rust-lang/rust/issues/36582 is solved.
|
||||
pub fn paginate_fn<T>(
|
||||
&self,
|
||||
ctx: Context,
|
||||
channel: ChannelId,
|
||||
pager: T,
|
||||
duration: std::time::Duration,
|
||||
) -> CommandResult
|
||||
where
|
||||
T: for<'a> Fn(u8, &'a mut EditMessage) -> (&'a mut EditMessage, CommandResult),
|
||||
{
|
||||
self.paginate(ctx, channel, pager, duration)
|
||||
}
|
||||
}
|
||||
|
||||
/// Pagination allows the bot to display content in multiple pages.
|
||||
|
@ -70,8 +89,8 @@ impl<T: Pagination> PaginationHandler<T> {
|
|||
e.content("Youmu is loading the first page...")
|
||||
})?;
|
||||
// React to the message
|
||||
message.react(&mut ctx, "⬅️")?;
|
||||
message.react(&mut ctx, "➡️️")?;
|
||||
message.react(&mut ctx, ARROW_LEFT)?;
|
||||
message.react(&mut ctx, ARROW_RIGHT)?;
|
||||
let mut p = Self {
|
||||
pager,
|
||||
message: message.clone(),
|
||||
|
@ -93,6 +112,7 @@ impl<T: Pagination> PaginationHandler<T> {
|
|||
res = r;
|
||||
e
|
||||
})?;
|
||||
self.message = msg;
|
||||
res
|
||||
}
|
||||
}
|
||||
|
@ -101,14 +121,20 @@ impl<T: Pagination> ReactionHandler for PaginationHandler<T> {
|
|||
fn handle_reaction(&mut self, reaction: &Reaction, _is_add: bool) -> CommandResult {
|
||||
match &reaction.emoji {
|
||||
ReactionType::Unicode(ref s) => match s.as_str() {
|
||||
"⬅" if self.page == 0 => return Ok(()),
|
||||
"⬅" => {
|
||||
ARROW_LEFT if self.page == 0 => return Ok(()),
|
||||
ARROW_LEFT => {
|
||||
self.page -= 1;
|
||||
self.call_pager()?;
|
||||
if let Err(e) = self.call_pager() {
|
||||
self.page += 1;
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
"➡" => {
|
||||
ARROW_RIGHT => {
|
||||
self.page += 1;
|
||||
self.call_pager()?;
|
||||
if let Err(e) = self.call_pager() {
|
||||
self.page -= 1;
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
|
|
|
@ -67,7 +67,7 @@ impl ReactionWatcher {
|
|||
recv(timeout) -> _ => break,
|
||||
};
|
||||
if let Err(v) = r {
|
||||
return Err(v);
|
||||
dbg!(v);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
Loading…
Add table
Reference in a new issue