From b46f0bac46a5709618c98f73453db3ae417ef6ac Mon Sep 17 00:00:00 2001 From: Evann Regnault Date: Mon, 3 Oct 2022 17:12:25 +0200 Subject: [PATCH] Parallel Message edit --- .idea/.gitignore | 8 ------ Cargo.toml | 3 ++- src/interactions/status_message.rs | 42 ++++++++++++++++++------------ src/utils/mongo.rs | 2 +- 4 files changed, 28 insertions(+), 27 deletions(-) delete mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/Cargo.toml b/Cargo.toml index ea7b29a..74ccefd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,4 +22,5 @@ mongodb = "2.1.0" futures = "0.3.19" rand = "0.8.4" dotenv = "0.15.0" -dotenv_codegen = "0.15.0" \ No newline at end of file +dotenv_codegen = "0.15.0" +rayon = "1.5.3" \ No newline at end of file diff --git a/src/interactions/status_message.rs b/src/interactions/status_message.rs index d49123b..ebd3fff 100644 --- a/src/interactions/status_message.rs +++ b/src/interactions/status_message.rs @@ -1,3 +1,6 @@ +use std::borrow::Borrow; +use std::ops::Add; +use std::sync::{Arc, Mutex}; use std::time::Duration; use rand::Rng; use serenity::{ @@ -7,6 +10,7 @@ use serenity::{ }; use serenity::builder::CreateEmbed; use serenity::model::id::{ChannelId, MessageId}; +use rayon::prelude::*; use serenity::model::interactions::application_command::ApplicationCommandInteraction; use serenity::model::interactions::InteractionApplicationCommandCallbackDataFlags; @@ -27,30 +31,34 @@ pub fn copy_embed(from: &Vec) -> Vec { pub async fn update_status_message(ctx: Context) { let forever = tokio::task::spawn(async move { let mut interval = tokio::time::interval(Duration::from_secs(60*60)); + let mut counter = Arc::new(Mutex::new(0)); loop { let mut x = get_all_status_messages().await; x.reverse(); - let embeds = create_status_embed().await; for sm in x { - if sm.channel_id == 0 {continue;} - let msg = ChannelId::from(sm.channel_id as u64).message(&ctx.http, sm.message_id as u64).await; - match msg { - Ok(mut m) => { - let copies = copy_embed(&embeds); - match m.edit(&ctx.http, |f| { - f.set_embeds(copies) - }).await { - Ok(_) => {}, - Err(e) => println!("Error while editing message {}", e) + let embeds : Vec = (&embeds).clone(); + let ctx = ctx.borrow().clone(); + tokio::spawn( async move { + if sm.channel_id == 0 { return } + let msg = ChannelId::from(sm.channel_id as u64).message(ctx.borrow().clone().http, sm.message_id as u64).await; + match msg { + Ok(mut m) => { + match m.edit(&ctx.http, |f| { + f.set_embeds((*embeds).to_owned()) + }).await { + Ok(_) => { + }, + Err(e) => println!("Error while editing message {}", e) + } } + Err(_) => { + println!("Cannot update guild : {}", sm.channel_id); + } + } + }); + }; - } - Err(_) => { - println!("Cannot update guild : {}", sm.channel_id); - } - } - } interval.tick().await; } }); diff --git a/src/utils/mongo.rs b/src/utils/mongo.rs index 679a0b8..69d5b2c 100644 --- a/src/utils/mongo.rs +++ b/src/utils/mongo.rs @@ -3,7 +3,7 @@ use mongodb::{Client, Collection, options::ClientOptions}; use futures::stream::{TryStreamExt}; use serde::{Serialize, Deserialize}; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct StatusMessage { pub(crate) guild_id: i64, pub(crate) message_id: i64,