diff --git a/src/bot.rs b/src/bot.rs index 1c6deb2..3714dd4 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -88,7 +88,7 @@ async fn check_packets(context: &Context) { for packet in packets_received { match packet { postman::Packet::FetchChannels(guild_id_str) => { - println!("bot : received FetchChannels packet, id '{}'", guild_id_str); + println!("bot : received FetchChannels packet, for guild '{}'", guild_id_str); match guild_id_str.parse::() { Ok(guild_id_u64) => { if let Some(guild) = context.cache.guild(guild_id_u64).await { @@ -122,6 +122,9 @@ async fn check_packets(context: &Context) { } } } + postman::Packet::FetchMessages(guild_id_str, channel_id_str) => { + println!("bot : received FetchMessages packet, for guild '{}', channel : '{}'", guild_id_str, channel_id_str); + } _ => { println!("bot : unhandled packet"); } @@ -135,15 +138,15 @@ async fn get_guilds(context: &Context) { if let Some(sender) = context.data.read().await.get::() { for guild_id in guilds { - let guild_name : String = if let Some(guild) = context.cache.guild(guild_id).await { - guild.name.clone() + if let Some(guild) = context.cache.guild(guild_id).await { + println!("bot : found guild '{}'", guild.name.clone()); + let discord_guild = discord_structure::Guild::new(guild.name.clone(), guild_id.to_string()); + sender.send(postman::Packet::Guild(discord_guild)).expect("Failed to send packet"); + } else { - "not found".to_string() + println!("bot : error retrieving guild '{}'", guild_id.clone()); }; - println!("bot : found guild : '{}' ({})", guild_name.clone(), guild_id.clone()); - let guild = discord_structure::Guild::new(guild_name, guild_id.to_string()); - sender.send(postman::Packet::Guild(guild)).expect("Failed to send packet"); } } else { println!("bot : failed to retrieve sender"); diff --git a/src/discord_structure.rs b/src/discord_structure.rs index 710a9bd..ed4c755 100644 --- a/src/discord_structure.rs +++ b/src/discord_structure.rs @@ -32,6 +32,10 @@ impl Channel { messages: vec![], } } + + pub fn greetings(&mut self) { + self.messages.push(Message::new("-".into(), "0".into(), self.id.clone(), self.guild_id.clone(), "start of the conversation".into())); + } } #[derive(PartialEq, Clone)] diff --git a/src/main.rs b/src/main.rs index b7e5d8e..c5a369a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -124,18 +124,17 @@ impl Jiji { for i in 0..self.guilds.len() { if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(i), self.guilds[i].name.clone())).clicked() { self.selected_guild = Some(i); + if self.guilds[i].channels.len() == 0 { + let _ = self.sender.send(postman::Packet::FetchChannels(self.guilds[i].id.clone())); + } } } }); + //let _ = self.sender.send(postman::Packet::FetchChannels(self.guilds[*selected_guild_index].id.clone())); if let Some(selected_guild_index) = &self.selected_guild { - if self.guilds[*selected_guild_index].channels.len() == 0 { - if ui.add(egui::Button::new("get channels")).clicked() { - let _ = self.sender.send(postman::Packet::FetchChannels(self.guilds[*selected_guild_index].id.clone())); - } - } else { - + if self.guilds[*selected_guild_index].channels.len() != 0 { let selected_channel_text = if let Some(selected_channel_index) = &self.selected_channel { self.guilds[*selected_guild_index].channels[*selected_channel_index].name.clone() } else { @@ -153,6 +152,11 @@ impl Jiji { for i in 0..self.guilds[*selected_guild_index].channels.len() { if ui.add(egui::SelectableLabel::new(self.selected_channel == Some(i), self.guilds[*selected_guild_index].channels[i].name.clone())).clicked() { self.selected_channel = Some(i); + if self.guilds[*selected_guild_index].channels[i].messages.len() == 0 { + let _ = self.sender.send(postman::Packet::FetchMessages(self.guilds[*selected_guild_index].id.clone(), self.guilds[*selected_guild_index].channels[i].id.clone())); + + self.guilds[*selected_guild_index].channels[i].greetings(); + } } } }); diff --git a/src/postman.rs b/src/postman.rs index 22b6b9d..f1f94d5 100644 --- a/src/postman.rs +++ b/src/postman.rs @@ -5,9 +5,10 @@ use std::sync::Mutex; pub enum Packet { Guild(discord_structure::Guild), - FetchChannels(String), Channel(discord_structure::Channel), Message(discord_structure::Message), + FetchChannels(String), + FetchMessages(String, String) } pub struct Sender;