Skip to content

Commit

Permalink
refactor: increase usage of iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Jan 25, 2024
1 parent cabeea1 commit 1dac27a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 66 deletions.
12 changes: 5 additions & 7 deletions src/commands/fun/pomelo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ pub async fn pomelo(ctx: Context<'_>) -> Result<()> {
.filter(|m| !m.user.bot)
.collect();

let mut nonmigrated_users: Vec<&serenity::UserId> = Vec::new();

for member in &members {
if member.user.discriminator.is_some() {
nonmigrated_users.push(&member.user.id);
};
}
let nonmigrated_users: Vec<serenity::UserId> = members
.iter()
.filter(|m| m.user.discriminator.is_some())
.map(|m| m.user.id)
.collect();

let embed = serenity::CreateEmbed::default()
.title("Username migration / Pomelo")
Expand Down
21 changes: 9 additions & 12 deletions src/starboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,21 @@ fn is_significant_reaction(reaction: &serenity::MessageReaction) -> Result<bool>
)
}

fn get_significant_reactions(
message: &serenity::Message,
) -> Result<Vec<(serenity::ReactionType, u64)>> {
let mut collected_reactions: Vec<(serenity::ReactionType, u64)> = Vec::new();

for reaction in &message.reactions {
if is_significant_reaction(reaction)? {
collected_reactions.push((reaction.reaction_type.clone(), reaction.count));
};
}
fn get_significant_reactions(message: &serenity::Message) -> Vec<(serenity::ReactionType, u64)> {
let mut collected_reactions: Vec<(serenity::ReactionType, u64)> = message
.reactions
.iter()
.filter(|r| is_significant_reaction(r).is_ok_and(|b| b))
.map(|r| (r.reaction_type.clone(), r.count))
.collect();

collected_reactions.sort_by_key(|i| match &i.0 {
serenity::ReactionType::Custom { id, .. } => id.get().to_string(),
serenity::ReactionType::Unicode(str) => str.to_string(),
_ => "unknown".to_owned(),
});

Ok(collected_reactions)
collected_reactions
}

fn serialize_reactions(
Expand Down Expand Up @@ -146,7 +143,7 @@ pub async fn handle(
.ok_or_eyre("no storage available for starboard features")?;

if let Some(starboard) = get_starboard_channel(&ctx, message.channel_id).await? {
let significant_reactions = get_significant_reactions(message)?;
let significant_reactions = get_significant_reactions(message);

if let Some(existing_starboard_message) = storage
.get_starboard(&message.id.to_string())
Expand Down
93 changes: 46 additions & 47 deletions src/template_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,33 +76,32 @@ impl Config {
self.components
.iter()
.map(|component| match component {
Component::Embed(data) => {
let mut message = CreateMessage::default();

for embed_data in &data.embeds {
let mut embed = CreateEmbed::default();

if let Some(title) = &embed_data.title {
embed = embed.title(title);
}
if let Some(description) = &embed_data.description {
embed = embed.description(description);
}
if let Some(color) = &embed_data.color {
embed = embed.color(*color);
}
if let Some(fields) = &embed_data.fields {
embed =
embed.fields(fields.iter().map(|f| (&f.name, &f.value, f.inline)));
}

message = message.embed(embed);
}

message
}

Component::Links(data) => {
Component::Embed(data) => CreateMessage::default().embeds(
data.embeds
.iter()
.map(|data| {
let mut embed = CreateEmbed::default();

if let Some(title) = &data.title {
embed = embed.title(title);
}
if let Some(description) = &data.description {
embed = embed.description(description);
}
if let Some(color) = &data.color {
embed = embed.color(*color);
}
if let Some(fields) = &data.fields {
embed = embed
.fields(fields.iter().map(|f| (&f.name, &f.value, f.inline)));
}

embed
})
.collect(),
),

Component::Links(data) => CreateMessage::default().embed({
let mut embed = CreateEmbed::default().title(&data.title).description(
data.links
.iter()
Expand All @@ -115,26 +114,26 @@ impl Config {
embed = embed.color(color);
}

CreateMessage::default().embed(embed)
}

Component::Rules(data) => {
let mut message = CreateMessage::default();

for (idx, (title, desc)) in data.rules.iter().enumerate() {
let mut embed = CreateEmbed::default()
.title(format!("{}. {}", idx + 1, title))
.description(desc);

if let Some(color) = data.colors.get(idx % data.colors.len()) {
embed = embed.color(*color);
}

message = message.add_embed(embed);
}

message
}
embed
}),

Component::Rules(data) => CreateMessage::default().embeds(
data.rules
.iter()
.enumerate()
.map(|(idx, (title, desc))| {
let mut embed = CreateEmbed::default()
.title(format!("{}. {}", idx + 1, title))
.description(desc);

if let Some(color) = data.colors.get(idx % data.colors.len()) {
embed = embed.color(*color);
}

embed
})
.collect(),
),

Component::Text(data) => CreateMessage::default().content(&data.text),
})
Expand Down

0 comments on commit 1dac27a

Please sign in to comment.