diff --git a/Cargo.toml b/Cargo.toml index f34380b7bb659f0c9d9e46a8fe300d1f1c421bff..d31667ac9de90c09f3d6cc722c804cf022c5f5a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ serenity = "0.8.0" simplelog = "^0.7.4" guard = "0.5.0" indexmap = { version = "1.3.1", features = ["serde-1"] } +rayon = "1.3.0" diff --git a/src/reaction_roles.rs b/src/reaction_roles.rs index faa2a238d81a517104548cb6818de392aa1be86d..3e1729b6adf57e92f1531cfdfa8f22e685163d28 100644 --- a/src/reaction_roles.rs +++ b/src/reaction_roles.rs @@ -1,5 +1,6 @@ use crate::config::{CONFIG, ReactRoleMap}; use crate::util::{get_react_from_string, get_string_from_react}; +use rayon::prelude::*; use serenity::{ client::Context, model::{channel::Message, channel::Reaction, id::RoleId, id::UserId}, @@ -174,25 +175,20 @@ pub fn sync_all_role_reactions(ctx: Context) { info!("Role reaction sync complete"); } -fn get_all_role_reaction_message( - ctx: &Context, -) -> Vec<( - Message, - &'static ReactRoleMap, -)> { +fn get_all_role_reaction_message(ctx: &Context) -> Vec<(Message, &'static ReactRoleMap)> { let guild = ctx.http.get_guild(CONFIG.server_id).unwrap(); info!(" Find role-react message: guild determined"); let channels = ctx.http.get_channels(*guild.id.as_u64()).unwrap(); info!(" Find role-react message: channels determined"); + let http = ctx.http.clone(); channels - .iter() + .par_iter() .flat_map(|channel| { - let ctxx = ctx.clone(); - // since we don't know which channels the messages are in, we check every combination of message and - // channel and ignore the bad matches using .ok() and .filter_map() - CONFIG.react_role_messages.iter().filter_map(move |rrm| { - ctxx.http - .get_message(*channel.id.as_u64(), *rrm.message.as_u64()) + // since we don't know which channels the messages are in, we check every combination + // of message and channel and ignore the bad matches using .ok() and .filter_map() + let h = http.clone(); // thread-local copy + CONFIG.react_role_messages.par_iter().filter_map(move |rrm| { + h.get_message(*channel.id.as_u64(), *rrm.message.as_u64()) .ok() .map(|m| (m, &rrm.mapping)) })