added structure for fetching messages

This commit is contained in:
WanderingPenwing 2024-07-11 02:39:30 +02:00
parent 029d7a1d06
commit a431f09c42
4 changed files with 26 additions and 14 deletions

View file

@ -88,7 +88,7 @@ async fn check_packets(context: &Context) {
for packet in packets_received { for packet in packets_received {
match packet { match packet {
postman::Packet::FetchChannels(guild_id_str) => { 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::<u64>() { match guild_id_str.parse::<u64>() {
Ok(guild_id_u64) => { Ok(guild_id_u64) => {
if let Some(guild) = context.cache.guild(guild_id_u64).await { 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"); println!("bot : unhandled packet");
} }
@ -135,15 +138,15 @@ async fn get_guilds(context: &Context) {
if let Some(sender) = context.data.read().await.get::<postman::Sender>() { if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
for guild_id in guilds { for guild_id in guilds {
let guild_name : String = if let Some(guild) = context.cache.guild(guild_id).await { if let Some(guild) = context.cache.guild(guild_id).await {
guild.name.clone() 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 { } 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 { } else {
println!("bot : failed to retrieve sender"); println!("bot : failed to retrieve sender");

View file

@ -32,6 +32,10 @@ impl Channel {
messages: vec![], 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)] #[derive(PartialEq, Clone)]

View file

@ -124,18 +124,17 @@ impl Jiji {
for i in 0..self.guilds.len() { for i in 0..self.guilds.len() {
if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(i), self.guilds[i].name.clone())).clicked() { if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(i), self.guilds[i].name.clone())).clicked() {
self.selected_guild = Some(i); 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 let Some(selected_guild_index) = &self.selected_guild {
if self.guilds[*selected_guild_index].channels.len() == 0 { 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 {
let selected_channel_text = if let Some(selected_channel_index) = &self.selected_channel { let selected_channel_text = if let Some(selected_channel_index) = &self.selected_channel {
self.guilds[*selected_guild_index].channels[*selected_channel_index].name.clone() self.guilds[*selected_guild_index].channels[*selected_channel_index].name.clone()
} else { } else {
@ -153,6 +152,11 @@ impl Jiji {
for i in 0..self.guilds[*selected_guild_index].channels.len() { 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() { 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); 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();
}
} }
} }
}); });

View file

@ -5,9 +5,10 @@ use std::sync::Mutex;
pub enum Packet { pub enum Packet {
Guild(discord_structure::Guild), Guild(discord_structure::Guild),
FetchChannels(String),
Channel(discord_structure::Channel), Channel(discord_structure::Channel),
Message(discord_structure::Message), Message(discord_structure::Message),
FetchChannels(String),
FetchMessages(String, String)
} }
pub struct Sender; pub struct Sender;