message sorting

This commit is contained in:
WanderingPenwing 2024-07-11 17:27:29 +02:00
parent be807fd54a
commit da571b4259
3 changed files with 37 additions and 6 deletions

View file

@ -9,5 +9,6 @@ edition = "2021"
eframe = "0.26.2" eframe = "0.26.2"
futures = "0.3.30" futures = "0.3.30"
image = "0.24.9" 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"] } 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"} tokio = {features = ["macros"], version = "0.2"}

View file

@ -29,15 +29,16 @@ struct Handler {
#[async_trait] #[async_trait]
impl EventHandler for Handler { 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); println!("bot : message received : '{}' from {}", msg.content, msg.author);
if msg.content == HELP_COMMAND { 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); eprintln!("bot : Error sending message: {:?}", why);
return return
} }
println!("bot : successfuly sent reply"); println!("bot : successfuly sent reply");
} }
} }
async fn ready(&self, _context: Context, ready: Ready) { 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()) return Err("guild not found".to_string())
} }
} else { } else {
return Err("failed to retriev sender".to_string()) return Err("failed to retrieve sender".to_string())
} }
Ok(()) Ok(())
} }
@ -163,7 +164,7 @@ async fn get_messages(context: &Context, guild_id_str: String, channel_id_str: S
for message in &messages { for message in &messages {
let author_name = message.author.name.clone(); 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())?; sender.send(postman::Packet::Message(discord_message)).map_err(|e| e.to_string())?;
} }

View file

@ -1,3 +1,5 @@
use chrono::{DateTime, ParseError};
#[derive(PartialEq, Clone)] #[derive(PartialEq, Clone)]
pub struct Guild { pub struct Guild {
pub name: String, pub name: String,
@ -34,8 +36,35 @@ impl Channel {
} }
pub fn insert(&mut self, message: Message) { pub fn insert(&mut self, message: Message) {
self.messages.insert(1, message); match self.get_index_from_timestamp(&message.timestamp) {
println!("discord_structure : need to compare 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<usize, ParseError> {
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) { pub fn end(&mut self) {