diff --git a/src/bot.rs b/src/bot.rs index 65a9cc0..76a027e 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -3,6 +3,8 @@ use serenity::{ model::{channel::Message, gateway::Ready}, prelude::*, }; +use std::sync::mpsc; +use crate::postman; mod token; @@ -29,21 +31,33 @@ impl EventHandler for Handler { println!("{} is connected!", ready.user.name); let guilds = context.cache.guilds().await; + let mut guild_names : Vec = vec![]; + for guild_id in guilds { let guild_name : String = if let Some(guild) = context.cache.guild(guild_id).await { - guild.name.clone() - } else { - "not found".to_string() - }; + guild.name.clone() + } else { + "not found".to_string() + }; + guild_names.push(guild_name.clone()); println!("Guild : '{}' ({})", guild_id, guild_name); } + + if let Some(sender) = context.data.read().await.get::() { + let message = postman::Message::new(postman::MessageType::GuildName, guild_names[0].clone()); + sender.send(message).unwrap(); + println!("Message from bot to gui, sent"); + } else { + println!("Failed to retrieve sender"); + } } } -pub async fn start_discord_bot() { +pub async fn start_discord_bot(sender: mpsc::Sender) { println!("Bot connection process started..."); let maybe_client = Client::builder(token::TOKEN) .event_handler(Handler) + .type_map_insert::(sender) .await .map_err(|why| format!("Client error: {:?}", why)); diff --git a/src/main.rs b/src/main.rs index 4cb4bf6..a5d938f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,27 @@ use eframe::egui; use image::GenericImageView; -use std::{error::Error, sync::Arc, thread, time}; +use std::{error::Error, sync::Arc, sync::mpsc, thread, time}; use tokio::runtime::Runtime; mod bot; +mod postman; const MAX_FPS: f32 = 30.0; fn main() { - let _handle = thread::spawn(|| { + let (tx, rx) = mpsc::channel::(); //tx transmiter + + let _handle = thread::spawn(move || { println!("Bot thread spawned"); let mut rt = Runtime::new().unwrap(); - rt.block_on(bot::start_discord_bot()); + rt.block_on(bot::start_discord_bot(tx)); }); // Run the GUI on the main thread - gui(); + gui(rx); } -fn gui() { +fn gui(receiver: mpsc::Receiver) { let icon_data = load_icon().unwrap_or_default(); let options = eframe::NativeOptions { @@ -28,17 +31,19 @@ fn gui() { ..Default::default() }; - let _ = eframe::run_native("Jiji", options, Box::new(move |_cc| Box::from(Jiji::new()))); + let _ = eframe::run_native("Jiji", options, Box::new(move |_cc| Box::from(Jiji::new(receiver)))); } struct Jiji { next_frame: time::Instant, + receiver: mpsc::Receiver, } impl Jiji { - fn new() -> Self { + fn new(receiver: mpsc::Receiver) -> Self { Self { next_frame: time::Instant::now(), + receiver, } } } @@ -80,4 +85,4 @@ pub fn load_icon() -> Result> { width: icon_width, height: icon_height, }) -} +} \ No newline at end of file diff --git a/src/postman.rs b/src/postman.rs new file mode 100644 index 0000000..d4bcc2e --- /dev/null +++ b/src/postman.rs @@ -0,0 +1,27 @@ +use std::sync::mpsc; +use serenity::prelude::TypeMapKey; + +pub struct Message { + kind: MessageType, + content: String, +} + +impl Message { + pub fn new(kind : MessageType, content: String) -> Self { + Self { + kind, + content, + } + } +} + + +pub enum MessageType { + GuildName, +} + +pub struct Sender; + +impl TypeMapKey for Sender { + type Value = mpsc::Sender; +} \ No newline at end of file