diff --git a/src/bot.rs b/src/bot.rs index 729ca00..43fe33a 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -127,40 +127,8 @@ async fn check_packets(context: &Context) { } postman::Packet::FetchMessages(guild_id_str, channel_id_str, first_message_id_str) => { println!("bot : received FetchMessages packet, channel : '{}', first message : {}", channel_id_str, first_message_id_str); - if let Some(sender) = context.data.read().await.get::() { - match channel_id_str.parse::() { - Ok(channel_id_u64) => { - let channel_id = ChannelId::from(channel_id_u64); - - let maybe_messages = channel_id.messages(&context.http, |retriever| { - retriever.after(MessageId::from(158339864557912064)).limit(25) - }).await; - - match maybe_messages { - Ok(messages) => { - println!("bot : got messages"); - - for message in messages { - let discord_message = discord_structure::Message::new(message.id.to_string(), channel_id_str.clone(), guild_id_str.clone(), message.author.to_string(), message.content, message.timestamp.to_string()); - sender.send(postman::Packet::Message(discord_message)).expect("Failed to send packet"); - } - } - Err(why) => { - eprintln!("bot : Failed to fetch messages : {}", why); - } - - } - } - Err(why) => { - eprintln!("bot : Failed to parse channel ID string to u64: {}", why); - } - } - - - sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet"); - } else { - println!("bot : failed to retrieve sender"); - } + + let _result = get_messages(context, guild_id_str, channel_id_str, first_message_id_str).await; } _ => { println!("bot : unhandled packet"); @@ -169,6 +137,40 @@ async fn check_packets(context: &Context) { } } +async fn get_messages(context: &Context, guild_id_str: String, channel_id_str: String, first_message_id_str: String) -> Result<(), String> { + if let Some(sender) = context.data.read().await.get::() { + let channel_id_u64 = channel_id_str.parse::().map_err(|e| e.to_string())?; + let channel_id = ChannelId::from(channel_id_u64); + + let messages = if first_message_id_str == "" { + channel_id.messages(&context.http, |retriever| { + retriever.limit(25) + }).await.map_err(|e| e.to_string())? + } else { + let first_message_id_u64 = first_message_id_str.parse::().map_err(|e| e.to_string())?; + channel_id.messages(&context.http, |retriever| { + retriever.before(MessageId::from(first_message_id_u64)).limit(25) + }).await.map_err(|e| e.to_string())? + }; + + println!("bot : got messages"); + + 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()); + sender.send(postman::Packet::Message(discord_message)).map_err(|e| e.to_string())?; + } + + if messages.len() == 25 { + let discord_fetch_message = discord_structure::Message::new("".to_string(), channel_id_str.clone(), guild_id_str.clone(), "+".to_string(), "".to_string(), "".to_string()); + sender.send(postman::Packet::Message(discord_fetch_message)).map_err(|e| e.to_string())?; + } + + sender.send(postman::Packet::FinishedRequest).map_err(|e| e.to_string())?; + } + Ok(()) +} + async fn get_guilds(context: &Context) { let guilds = context.cache.guilds().await; diff --git a/src/main.rs b/src/main.rs index f15f9f3..9353005 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,7 @@ struct Jiji { selected_channel: Option, time_watch: f32, pending_bot_requests: usize, + current_message: String, } impl Jiji { @@ -61,6 +62,7 @@ impl Jiji { selected_channel: None, time_watch: 0.0, pending_bot_requests: 0, + current_message: "".into(), } } } @@ -105,6 +107,10 @@ impl eframe::App for Jiji { if self.guilds[guild_index].channels[i].id != message.channel_id { continue } + if self.guilds[guild_index].channels[i].messages[0].author_name == "+" { + self.guilds[guild_index].channels[i].messages[0] = message.clone(); + continue + } self.guilds[guild_index].channels[i].messages.insert(0, message.clone()); } } @@ -157,6 +163,7 @@ impl Jiji { ui.set_min_width(60.0); if ui.add(egui::SelectableLabel::new(self.selected_guild == None, "None")).clicked() { self.selected_guild = None; + self.selected_channel = None; } for i in 0..self.guilds.len() { if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(i), self.guilds[i].name.clone())).clicked() { @@ -164,6 +171,7 @@ impl Jiji { if self.guilds[i].channels.len() == 0 { let _ = self.sender.send(postman::Packet::FetchChannels(self.guilds[i].id.clone())); + self.selected_channel = None; self.pending_bot_requests += 1; } } @@ -213,6 +221,20 @@ impl Jiji { egui::TopBottomPanel::bottom("infobar") .resizable(false) .show(ctx, |ui| { + if self.selected_channel != None { + ui.label(""); + ui.horizontal(|ui| { + if ui.button(">").clicked() { + println!("gui : sent message"); + } + egui::ScrollArea::vertical() + .show(ui, |ui| { + let _response = ui.add(egui::TextEdit::multiline(&mut self.current_message) + .desired_width(f32::INFINITY) + .lock_focus(true)); + }); + }); + } ui.horizontal(|ui| { ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| { ui.label(&format!("time per frame : {:.1} ms", self.time_watch)); @@ -234,13 +256,35 @@ impl Jiji { .show(ui, |ui| { if let Some(selected_guild_index) = &self.selected_guild { if let Some(selected_channel_index) = &self.selected_channel { + let mut last_author = "".to_string(); for message in &self.guilds[*selected_guild_index].channels[*selected_channel_index].messages { if message.author_name == "-" { continue } - ui.separator(); + + if message.author_name == "+" { + if ui.button("+").clicked() { + if let Some(selected_guild_index) = &self.selected_guild { + if let Some(selected_channel_index) = &self.selected_channel { + let _ = self.sender.send(postman::Packet::FetchMessages( + self.guilds[*selected_guild_index].id.clone(), + self.guilds[*selected_guild_index].channels[*selected_channel_index].id.clone(), + self.guilds[*selected_guild_index].channels[*selected_channel_index].messages[1].id.clone(), + )); + self.pending_bot_requests += 1; + } + } + } + continue + } + if message.author_name != last_author { + ui.separator(); + ui.colored_label(hex_str_to_color("#3399ff"), &message.author_name); + } else { + ui.label(""); + } ui.label(&message.content); - ui.label(&message.author_name); + last_author = message.author_name.clone(); } } } @@ -263,4 +307,8 @@ pub fn load_icon() -> Result> { width: icon_width, height: icon_height, }) +} + +pub fn hex_str_to_color(hex_str: &str) -> egui::Color32 { + egui::Color32::from_hex(hex_str).unwrap_or_else(|_| egui::Color32::WHITE) } \ No newline at end of file