From ce2992f5248760a777b6a4a2b361695f1eca8df3 Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Mon, 11 Mar 2024 08:46:29 +0100 Subject: [PATCH] server selection and reverse communication prototype --- src/bot.rs | 24 +++++++------ src/discord_structure.rs | 6 ++-- src/main.rs | 78 +++++++++++++++++++++++++++++++--------- src/postman.rs | 14 ++++++-- 4 files changed, 90 insertions(+), 32 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index f32dfba..48a19de 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -4,6 +4,7 @@ use serenity::{ prelude::*, }; use std::sync::mpsc; +use std::sync::Mutex; use crate::postman; use crate::discord_structure; @@ -18,14 +19,13 @@ struct Handler; #[async_trait] impl EventHandler for Handler { async fn message(&self, ctx: Context, msg: Message) { - println!("Message received : '{}' from {}", msg.content, msg.author); + println!("bot : message received : '{}' from {}", msg.content, msg.author); if msg.content == HELP_COMMAND { - println!("Message is command"); if let Err(why) = msg.channel_id.say(&ctx.http, HELP_MESSAGE).await { - println!("Error sending message: {:?}", why); + println!("bot : Error sending message: {:?}", why); return } - println!("Successfuly sent message"); + println!("bot : successfuly sent reply"); } } @@ -40,32 +40,36 @@ impl EventHandler for Handler { } else { "not found".to_string() }; - println!("Guild : '{}' ({})", guild_name.clone(), 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!("Failed to retrieve sender"); + println!("bot : failed to retrieve sender"); } } } -pub async fn start_discord_bot(sender: mpsc::Sender) { - println!("Bot connection process started..."); +pub async fn start_discord_bot(sender: mpsc::Sender, receiver: Mutex>) { + println!("bot : connection process started..."); let maybe_client = Client::builder(token::TOKEN) .event_handler(Handler) .type_map_insert::(sender) + .type_map_insert::(receiver) .await .map_err(|why| format!("Client error: {:?}", why)); + + //let mut rx = bot_rx.lock().unwrap(); // Lock the receiver + //let msg = rx.recv().unwrap(); // Receive a message if let Ok(mut client) = maybe_client { if let Err(why) = client.start().await { - println!("Client error: {:?}", why); + println!("bot : client error: {:?}", why); return; } } else { - println!("No Client"); + println!("bot : no client"); return; } } diff --git a/src/discord_structure.rs b/src/discord_structure.rs index b2dc05d..710a9bd 100644 --- a/src/discord_structure.rs +++ b/src/discord_structure.rs @@ -1,4 +1,4 @@ - +#[derive(PartialEq, Clone)] pub struct Guild { pub name: String, pub id: String, @@ -15,7 +15,7 @@ impl Guild { } } - +#[derive(PartialEq, Clone)] pub struct Channel { pub name: String, pub id: String, @@ -34,7 +34,7 @@ impl Channel { } } - +#[derive(PartialEq, Clone)] pub struct Message { pub author_name: String, pub id: String, diff --git a/src/main.rs b/src/main.rs index 029a5db..f97918e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use eframe::egui; use image::GenericImageView; use std::{error::Error, sync::Arc, sync::mpsc, thread, time}; use tokio::runtime::Runtime; +use std::sync::Mutex; mod bot; mod postman; @@ -10,19 +11,21 @@ mod discord_structure; const MAX_FPS: f32 = 30.0; fn main() { - let (tx, rx) = mpsc::channel::(); //tx transmiter + let (bot_tx, gui_rx) = mpsc::channel::(); //tx transmiter + let (gui_tx, bot_rx) = mpsc::channel::(); //tx transmiter + let bot_rx = Mutex::new(bot_rx); let _handle = thread::spawn(move || { - println!("Bot thread spawned"); + println!("main : bot thread spawned"); let mut rt = Runtime::new().unwrap(); - rt.block_on(bot::start_discord_bot(tx)); + rt.block_on(bot::start_discord_bot(bot_tx, bot_rx)); }); // Run the GUI on the main thread - gui(rx); + gui(gui_tx, gui_rx); } -fn gui(receiver: mpsc::Receiver) { +fn gui(sender: mpsc::Sender, receiver: mpsc::Receiver) { let icon_data = load_icon().unwrap_or_default(); let options = eframe::NativeOptions { @@ -32,21 +35,25 @@ fn gui(receiver: mpsc::Receiver) { ..Default::default() }; - let _ = eframe::run_native("Jiji", options, Box::new(move |_cc| Box::from(Jiji::new(receiver)))); + let _ = eframe::run_native("Jiji", options, Box::new(move |_cc| Box::from(Jiji::new(sender, receiver)))); } struct Jiji { next_frame: time::Instant, + sender: mpsc::Sender, receiver: mpsc::Receiver, guilds: Vec, + selected_guild: Option, } impl Jiji { - fn new(receiver: mpsc::Receiver) -> Self { + fn new(sender: mpsc::Sender, receiver: mpsc::Receiver) -> Self { Self { next_frame: time::Instant::now(), + sender, receiver, guilds: vec![], + selected_guild: None, } } } @@ -61,14 +68,18 @@ impl eframe::App for Jiji { while let Ok(packet) = self.receiver.try_recv() { match packet { postman::Packet::Guild(guild) => { - println!("gui : guild received : '{}'", guild.name); - } - postman::Packet::Channel(channel) => { - println!("gui : channel received : '{}'", channel.name); - } - postman::Packet::Message(message) => { - println!("gui : message received : '{}'", message.content); - } + println!("gui : guild received : '{}'", guild.name); + self.guilds.push(guild); + } + postman::Packet::Channel(channel) => { + println!("gui : channel received : '{}'", channel.name); + } + postman::Packet::Message(message) => { + println!("gui : message received : '{}'", message.content); + } + _ => { + println!("unhandled packet"); + } } } @@ -82,8 +93,43 @@ impl eframe::App for Jiji { impl Jiji { pub fn draw_feed(&mut self, ctx: &egui::Context) { + egui::TopBottomPanel::top("server_selection") + .resizable(false) + .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() + } else { + "None".to_string() + }; + + egui::ComboBox::from_label("") + .selected_text(format!("{}", selected_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; + } + 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); + } + } + }); + + if let Some(guild) = &self.selected_guild { + if guild.channels.len() == 0 { + if ui.add(egui::Button::new("get channels")).clicked() { + let _ = self.sender.send(postman::Packet::FetchChannels(guild.id.clone())); + } + } + } + }); + }); egui::CentralPanel::default().show(ctx, |ui| { - ui.label("Hello there"); + ui.label("General Kenobi"); }); } } diff --git a/src/postman.rs b/src/postman.rs index ee9353c..22b6b9d 100644 --- a/src/postman.rs +++ b/src/postman.rs @@ -1,15 +1,23 @@ use std::sync::mpsc; use serenity::prelude::TypeMapKey; use crate::discord_structure; +use std::sync::Mutex; pub enum Packet { - Guild(discord_structure::Guild), - Channel(discord_structure::Channel), - Message(discord_structure::Message), + Guild(discord_structure::Guild), + FetchChannels(String), + Channel(discord_structure::Channel), + Message(discord_structure::Message), } pub struct Sender; impl TypeMapKey for Sender { type Value = mpsc::Sender; +} + +pub struct Receiver; + +impl TypeMapKey for Receiver { + type Value = Mutex>; } \ No newline at end of file