From da571b425966b1b8abc99969509a56c7f28b004d Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Thu, 11 Jul 2024 17:27:29 +0200 Subject: [PATCH] message sorting --- Cargo.toml | 1 + src/bot.rs | 9 +++++---- src/discord_structure.rs | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2a90792..873c57f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +9,6 @@ edition = "2021" eframe = "0.26.2" futures = "0.3.30" image = "0.24.9" +chrono = "0.4" serenity = { version = "0.9.0-rc.2", default-features = false, features = ["client", "gateway", "model", "builder", "rustls_backend", "cache", "http"] } tokio = {features = ["macros"], version = "0.2"} diff --git a/src/bot.rs b/src/bot.rs index 35a182d..7d81a57 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -29,15 +29,16 @@ struct Handler { #[async_trait] impl EventHandler for Handler { - async fn message(&self, ctx: Context, msg: Message) { + async fn message(&self, context: Context, msg: Message) { println!("bot : message received : '{}' from {}", msg.content, msg.author); if msg.content == HELP_COMMAND { - if let Err(why) = msg.channel_id.say(&ctx.http, HELP_MESSAGE).await { + if let Err(why) = msg.channel_id.say(&context.http, HELP_MESSAGE).await { eprintln!("bot : Error sending message: {:?}", why); return } println!("bot : successfuly sent reply"); } + } async fn ready(&self, _context: Context, ready: Ready) { @@ -140,7 +141,7 @@ async fn get_channels(context: &Context, guild_id_str: String) -> Result<(), Str return Err("guild not found".to_string()) } } else { - return Err("failed to retriev sender".to_string()) + return Err("failed to retrieve sender".to_string()) } Ok(()) } @@ -163,7 +164,7 @@ async fn get_messages(context: &Context, guild_id_str: String, channel_id_str: S for message in &messages { let author_name = message.author.name.clone(); - let discord_message = discord_structure::Message::new(message.id.to_string(), channel_id_str.clone(), guild_id_str.clone(), author_name, message.content.clone(), message.timestamp.to_string()); + let discord_message = discord_structure::Message::new(message.id.to_string(), channel_id_str.clone(), guild_id_str.clone(), author_name, message.content.clone(), message.timestamp.to_rfc2822()); sender.send(postman::Packet::Message(discord_message)).map_err(|e| e.to_string())?; } diff --git a/src/discord_structure.rs b/src/discord_structure.rs index de2f4fc..b161961 100644 --- a/src/discord_structure.rs +++ b/src/discord_structure.rs @@ -1,3 +1,5 @@ +use chrono::{DateTime, ParseError}; + #[derive(PartialEq, Clone)] pub struct Guild { pub name: String, @@ -34,8 +36,35 @@ impl Channel { } pub fn insert(&mut self, message: Message) { - self.messages.insert(1, message); - println!("discord_structure : need to compare timestamp"); + match self.get_index_from_timestamp(&message.timestamp) { + Ok(index) => { + self.messages.insert(index, message); + } + Err(why) => { + eprintln!("discord_structure : timestamp error : {}", why); + self.messages.push(message); + } + } + + } + + pub fn get_index_from_timestamp(&self, message_timestamp: &str) -> Result { + let new_timestamp = DateTime::parse_from_rfc2822(message_timestamp)?; + + let mut index: usize = 0; + + for i in 0..self.messages.len() { + if self.messages[i].timestamp == "" { + index = i + 1; + continue + } + let current_timestamp = DateTime::parse_from_rfc2822(&self.messages[i].timestamp)?; + + if new_timestamp > current_timestamp { + index = i + 1; + } + } + Ok(index) } pub fn end(&mut self) {