From d2a0603bbf2bf41d88ef3a589de25f921771de9e Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Thu, 11 Jul 2024 18:49:31 +0200 Subject: [PATCH] ability to receive msg --- src/bot.rs | 66 ++++++++++++++++++++++++++--------------------------- src/main.rs | 10 +++++++- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 10b12c3..7f0f711 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -10,8 +10,9 @@ use std::sync::mpsc; use std::sync::Mutex; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; -use std::thread; use std::sync::Arc; +use tokio::time::interval; + use crate::postman; use crate::discord_structure; @@ -41,13 +42,11 @@ impl EventHandler for Handler { if let Some(sender) = context.data.read().await.get::() { let author_name = msg.author.name.clone(); - let mut guild_id = "dm".to_string(); - - if let Some(channel) = msg.channel(&context.cache).await { - if let Some(guild) = channel.guild() { - guild_id = guild.id.to_string(); - } - } + let guild_id = if let Some(id) = msg.guild_id { + id.to_string() + } else { + "dm".to_string() + }; let discord_message = discord_structure::Message::new(msg.id.to_string(), msg.channel_id.to_string(), guild_id, author_name, msg.content.clone(), msg.timestamp.to_rfc2822()); sender.send(postman::Packet::Message(discord_message)).expect("failed to send packet"); @@ -61,31 +60,32 @@ impl EventHandler for Handler { } async fn cache_ready(&self, context: Context, _guilds: Vec) { - println!("bot : cache built successfully!"); - - let context = Arc::new(context); - - if !self.is_loop_running.load(Ordering::Relaxed) { - // We have to clone the Arc, as it gets moved into the new thread. - let context1 = Arc::clone(&context); - // tokio::spawn creates a new green thread that can run in parallel with the rest of - // the application. - tokio::spawn(async move { - get_guilds(&context1).await; - }); - - //comment this to get the listening to messages working - let context2 = Arc::clone(&context); - tokio::spawn(async move { - loop { - check_packets(&context2).await; - thread::sleep(Duration::from_millis(PACKET_REFRESH)); - } - }); - - // Now that the loop is running, we set the bool to true - self.is_loop_running.swap(true, Ordering::Relaxed); - } + println!("bot : cache built successfully!"); + + let context = Arc::new(context); + + if !self.is_loop_running.load(Ordering::Relaxed) { + // We have to clone the Arc, as it gets moved into the new thread. + let context1 = Arc::clone(&context); + // tokio::spawn creates a new green thread that can run in parallel with the rest of + // the application. + tokio::spawn(async move { + get_guilds(&context1).await; + }); + + // Use tokio interval instead of spawning a loop + let context2 = Arc::clone(&context); + let mut interval = interval(Duration::from_millis(PACKET_REFRESH)); + tokio::spawn(async move { + loop { + interval.tick().await; + check_packets(&context2).await; + } + }); + + // Now that the loop is running, we set the bool to true + self.is_loop_running.swap(true, Ordering::Relaxed); + } } } diff --git a/src/main.rs b/src/main.rs index 3312062..ca0ad56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,6 +102,7 @@ impl eframe::App for Jiji { guild = Some(i); } + if let Some(guild_index) = guild { let mut unkown_channel = true; for i in 0..self.guilds[guild_index].channels.len() { @@ -110,12 +111,17 @@ impl eframe::App for Jiji { } self.guilds[guild_index].channels[i].insert(message.clone()); unkown_channel = false; + println!("gui : message put in : '{}'", self.guilds[guild_index].channels[i].name); } if unkown_channel { println!("gui : unkown channel"); self.guilds[guild_index].channels.push(discord_structure::Channel::new(message.channel_id.clone(), message.channel_id, message.guild_id)); } + } else { + println!("gui : message guild issue : '{}'", message.guild_id); + + println!("gui : guilds {:?}", self.guilds.clone().into_iter().map(|guild| { guild.id.clone()}).collect::>()); } } postman::Packet::ChannelEnd(guild_id, channel_id) => { @@ -193,12 +199,14 @@ 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); + self.selected_channel = None; + if self.guilds[i].channels.len() == 0 && self.guilds[i].id != "dm" { let _ = self.sender.send(postman::Packet::FetchChannels(self.guilds[i].id.clone())); - self.selected_channel = None; self.pending_bot_requests += 1; } + } } });