From 029d7a1d0695838faf1a0c6a90bd2845e7dd4aa0 Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Wed, 10 Jul 2024 19:58:41 +0200 Subject: [PATCH] added channel fetching --- shell.nix | 38 +++++++++++++++++++++ src/bot.rs | 95 +++++++++++++++++++++++++++++++++++++---------------- src/main.rs | 55 ++++++++++++++++++++++++------- 3 files changed, 149 insertions(+), 39 deletions(-) create mode 100644 shell.nix diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..ab6295d --- /dev/null +++ b/shell.nix @@ -0,0 +1,38 @@ +{ pkgs ? import { overlays = [ (import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz)) ]; }, + unstable ? import { config = { allowUnfree = true; }; } +}: +with pkgs; + +let + # Specify Pillow as a build input + pillow = python3Packages.pillow; +in + +mkShell { + nativeBuildInputs = with xorg; [ + libxcb + libXcursor + libXrandr + libXi + pkg-config + ] ++ [ + cargo + unstable.rustc + python3 + libGL + libGLU + libxkbcommon + gtk3-x11 + gnome.zenity + ]; + buildInputs = [ + latest.rustChannels.stable.rust + xorg.libX11 + wayland + libxkbcommon + ]; + + shellHook = '' + export LD_LIBRARY_PATH=/run/opengl-driver/lib/:${lib.makeLibraryPath ([libGL libGLU libxkbcommon])} + ''; +} diff --git a/src/bot.rs b/src/bot.rs index 7a24236..1c6deb2 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -3,7 +3,7 @@ use serenity::{ model::{channel::Message, gateway::Ready}, prelude::*, }; -use serenity::model::prelude::GuildId; +use serenity::model::prelude::ChannelType; use std::sync::mpsc; use std::sync::Mutex; use std::sync::atomic::{AtomicBool, Ordering}; @@ -17,7 +17,7 @@ use crate::discord_structure; mod token; const HELP_MESSAGE: &str = "Hello there, Human! I am a messenger for the wandering penwing."; -const HELP_COMMAND: &str = "!jiji"; +const HELP_COMMAND: &str = "!penwing"; const PACKET_REFRESH : u64 = 500; struct Handler { @@ -38,50 +38,45 @@ impl EventHandler for Handler { } } - async fn ready(&self, context: Context, ready: Ready) { + async fn ready(&self, _context: Context, ready: Ready) { println!("bot : {} is connected!", ready.user.name); } - async fn cache_ready(&self, context: Context, _guilds: Vec) { + 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; - }); + // 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; + }); let context2 = Arc::clone(&context); - tokio::spawn(async move { - loop { - check_packets(&context2).await; - thread::sleep(Duration::from_millis(PACKET_REFRESH)); - } - }); + 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); - } + // Now that the loop is running, we set the bool to true + self.is_loop_running.swap(true, Ordering::Relaxed); + } } } async fn check_packets(context: &Context) { + let mut packets_received : Vec = vec![]; + if let Some(receiver_mutex) = context.data.read().await.get::() { if let Ok(receiver) = receiver_mutex.lock() { while let Ok(packet) = receiver.try_recv() { - match packet { - postman::Packet::FetchChannels(guild_id) => { - println!("bot : fetch channels request received : '{}'", guild_id); - } - _ => { - println!("bot : unhandled packet"); - } - } + packets_received.push(packet); } } else { println!("bot : failed to lock receiver"); @@ -89,8 +84,52 @@ async fn check_packets(context: &Context) { } else { println!("bot : failed to retrieve receiver"); } + + for packet in packets_received { + match packet { + postman::Packet::FetchChannels(guild_id_str) => { + println!("bot : received FetchChannels packet, id '{}'", guild_id_str); + match guild_id_str.parse::() { + Ok(guild_id_u64) => { + if let Some(guild) = context.cache.guild(guild_id_u64).await { + match guild.channels(&context.http).await { + Ok(guild_channels) => { + if let Some(sender) = context.data.read().await.get::() { + for (channel_id, channel) in guild_channels { + if channel.kind != ChannelType::Text { + continue + } + let discord_channel = discord_structure::Channel::new(channel.name, format!("{}",channel_id), guild_id_str.to_string()); + sender.send(postman::Packet::Channel(discord_channel)).expect("Failed to send packet"); + } + println!("bot : sent channels"); + } else { + println!("bot : failed to retrieve sender"); + } + + } + Err(e) => { + eprintln!("bot : Failed to get channels : {}", e); + } + + } + } else { + println!("bot : guild not found"); + }; + } + Err(e) => { + eprintln!("bot : Failed to parse guild ID string to u64: {}", e); + } + } + } + _ => { + println!("bot : unhandled packet"); + } + } + } } + async fn get_guilds(context: &Context) { let guilds = context.cache.guilds().await; diff --git a/src/main.rs b/src/main.rs index f97918e..b7e5d8e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,7 +43,8 @@ struct Jiji { sender: mpsc::Sender, receiver: mpsc::Receiver, guilds: Vec, - selected_guild: Option, + selected_guild: Option, + selected_channel: Option, } impl Jiji { @@ -54,6 +55,7 @@ impl Jiji { receiver, guilds: vec![], selected_guild: None, + selected_channel: None, } } } @@ -73,6 +75,13 @@ impl eframe::App for Jiji { } postman::Packet::Channel(channel) => { println!("gui : channel received : '{}'", channel.name); + for i in 0..self.guilds.len() { + if self.guilds[i].id != channel.guild_id { + continue + } + self.guilds[i].channels.push(channel.clone()); + println!("gui : channel added to '{}'", self.guilds[i].name); + } } postman::Packet::Message(message) => { println!("gui : message received : '{}'", message.content); @@ -98,32 +107,56 @@ impl Jiji { .show(ctx, |ui| { ui.horizontal(|ui| { ui.label("Where do you want to look ? "); - let selected_text = if let Some(guild) = &self.selected_guild { - guild.name.clone() + let selected_guild_text = if let Some(selected_guild_index) = &self.selected_guild { + self.guilds[*selected_guild_index].name.clone() } else { "None".to_string() }; egui::ComboBox::from_label("") - .selected_text(format!("{}", selected_text)) + .selected_text(format!("{}", selected_guild_text)) .show_ui(ui, |ui| { ui.style_mut().wrap = Some(false); ui.set_min_width(60.0); if ui.add(egui::SelectableLabel::new(self.selected_guild == None, "None")).clicked() { - self.selected_guild = None; + self.selected_guild = None; } - for guild in self.guilds.clone() { - if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(guild.clone()), guild.name.clone())).clicked() { - self.selected_guild = Some(guild); + 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 let Some(guild) = &self.selected_guild { - if guild.channels.len() == 0 { + 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(guild.id.clone())); + 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 { + self.guilds[*selected_guild_index].channels[*selected_channel_index].name.clone() + } else { + "None".to_string() + }; + + egui::ComboBox::from_label(":") + .selected_text(format!("{}", selected_channel_text)) + .show_ui(ui, |ui| { + ui.style_mut().wrap = Some(false); + ui.set_min_width(60.0); + if ui.add(egui::SelectableLabel::new(self.selected_channel == None, "None")).clicked() { + self.selected_channel = None; + } + 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); + } + } + }); + } } });