From 17d7c6095fb9767679e638128a6a333d6c98ca3d Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Sat, 9 Mar 2024 17:33:11 +0100 Subject: [PATCH] improved packet struct --- src/bot.rs | 28 ++++++++++---------- src/discord_structure.rs | 56 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 15 ++++++++++- src/postman.rs | 22 ++++------------ src/tree.rs | 18 ------------- 5 files changed, 88 insertions(+), 51 deletions(-) create mode 100644 src/discord_structure.rs delete mode 100644 src/tree.rs diff --git a/src/bot.rs b/src/bot.rs index 88bc957..f32dfba 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -5,6 +5,7 @@ use serenity::{ }; use std::sync::mpsc; use crate::postman; +use crate::discord_structure; mod token; @@ -13,6 +14,7 @@ const HELP_COMMAND: &str = "!jiji"; struct Handler; + #[async_trait] impl EventHandler for Handler { async fn message(&self, ctx: Context, msg: Message) { @@ -31,22 +33,18 @@ 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_names.push(guild_name.clone()); - println!("Guild : '{}' ({})", guild_id, guild_name); - } - if let Some(sender) = context.data.read().await.get::() { - let message = postman::Packet::new(postman::PacketKind::GuildName, guild_names[0].clone()); - sender.send(message).unwrap(); - println!("Message from bot to gui, sent"); + 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() + }; + println!("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"); } diff --git a/src/discord_structure.rs b/src/discord_structure.rs new file mode 100644 index 0000000..b2dc05d --- /dev/null +++ b/src/discord_structure.rs @@ -0,0 +1,56 @@ + +pub struct Guild { + pub name: String, + pub id: String, + pub channels: Vec, +} + +impl Guild { + pub fn new(name: String, id: String) -> Self { + Self { + name, + id, + channels: vec![], + } + } +} + + +pub struct Channel { + pub name: String, + pub id: String, + pub guild_id: String, + pub messages: Vec, +} + +impl Channel { + pub fn new(name: String, id: String, guild_id: String) -> Self { + Self { + name, + id, + guild_id, + messages: vec![], + } + } +} + + +pub struct Message { + pub author_name: String, + pub id: String, + pub channel_id: String, + pub guild_id: String, + pub content: String, +} + +impl Message { + pub fn new(author_name: String, id: String, channel_id: String, guild_id: String, content: String) -> Self { + Self { + author_name, + id, + channel_id, + guild_id, + content, + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index af5734c..029a5db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use tokio::runtime::Runtime; mod bot; mod postman; +mod discord_structure; const MAX_FPS: f32 = 30.0; @@ -37,6 +38,7 @@ fn gui(receiver: mpsc::Receiver) { struct Jiji { next_frame: time::Instant, receiver: mpsc::Receiver, + guilds: Vec, } impl Jiji { @@ -44,6 +46,7 @@ impl Jiji { Self { next_frame: time::Instant::now(), receiver, + guilds: vec![], } } } @@ -56,7 +59,17 @@ impl eframe::App for Jiji { self.next_frame = time::Instant::now(); while let Ok(packet) = self.receiver.try_recv() { - println!("Message from bot to gui received : {}", packet.content); + 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); + } + } } self.draw_feed(ctx); diff --git a/src/postman.rs b/src/postman.rs index bffcb14..ee9353c 100644 --- a/src/postman.rs +++ b/src/postman.rs @@ -1,23 +1,11 @@ use std::sync::mpsc; use serenity::prelude::TypeMapKey; +use crate::discord_structure; -pub struct Packet { - pub kind: PacketKind, - pub content: String, -} - -impl Packet { - pub fn new(kind : PacketKind, content: String) -> Self { - Self { - kind, - content, - } - } -} - - -pub enum PacketKind { - GuildName, +pub enum Packet { + Guild(discord_structure::Guild), + Channel(discord_structure::Channel), + Message(discord_structure::Message), } pub struct Sender; diff --git a/src/tree.rs b/src/tree.rs deleted file mode 100644 index ead6c31..0000000 --- a/src/tree.rs +++ /dev/null @@ -1,18 +0,0 @@ - -struct Guild { - name: String, - id: String, - channels: Vec, -} - -struct Channel { - name: String, - id: String, - messages: Vec, -} - -struct Message { - author: String, - id: String, - content: String, -} \ No newline at end of file