Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor chunks using iterators #152

Merged
merged 5 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions src/interactions/commands/chat/leaderboard.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt::Write;

use twilight_interactions::command::{CommandModel, CreateCommand};

use crate::{
Expand Down Expand Up @@ -33,31 +31,22 @@ impl Leaderboard {
} else {
Member::list_by_xp_exclude_deleted(&ctx.bot.pool, guild_id, 99, &ctx.bot.cache).await?
};
let mut pages = Vec::new();
let mut current_page = String::new();

for (idx, member) in lb.into_iter().enumerate() {
if idx % 9 == 0 && idx != 0 {
pages.push(current_page);
current_page = String::new();
}

writeln!(
current_page,
"`#{}` <@{}> - {} XP",
idx + 1,
member.user_id,
member.xp
)
.unwrap();
}
pages.push(current_page);
let mut idx = 0;
let pages = lb.chunks(9).map(|chunk| {
chunk
.iter()
.map(|Member { user_id, xp, .. }| {
idx += 1;
format!("`#{idx}` <@{user_id}> - {xp} XP\n")
})
.collect::<String>()
});

let author_id = ctx.interaction.author_id().unwrap();
paginator::simple(
&mut ctx,
pages
.into_iter()
.map(|p| {
(
None,
Expand Down
32 changes: 12 additions & 20 deletions src/interactions/commands/chat/utils/trashcan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,22 @@ impl TrashCan {
return Ok(());
}

let mut pages = Vec::new();
let mut curr_page = String::new();

for (idx, item) in trashed.into_iter().enumerate() {
if idx % 50 == 0 && idx != 0 {
pages.push(curr_page);
curr_page = String::new();
}

let link = fmt_message_link(guild_id, item.channel_id, item.message_id);
curr_page.push_str(&format!(
"[{}]({})\n",
item.trash_reason
.unwrap_or_else(|| "No reason given.".to_string()),
link,
));
}

pages.push(curr_page);
let pages = trashed.chunks(50).map(|chunk| {
chunk
.iter()
.map(|msg| {
let reason = msg.trash_reason.as_deref().unwrap_or("No reason given.");
let link = fmt_message_link(guild_id, msg.channel_id, msg.message_id);

format!("[{reason}]({link})\n")
})
.collect::<String>()
});

let author_id = ctx.interaction.author_id().unwrap();
paginator::simple(
&mut ctx,
pages.into_iter().map(|page| (Some(page), None)).collect(),
pages.map(|page| (Some(page), None)).collect(),
author_id,
true,
)
Expand Down