improved packet struct

This commit is contained in:
WanderingPenwing 2024-03-09 17:33:11 +01:00
parent 886e29e72d
commit 17d7c6095f
5 changed files with 88 additions and 51 deletions

View file

@ -5,6 +5,7 @@ use serenity::{
}; };
use std::sync::mpsc; use std::sync::mpsc;
use crate::postman; use crate::postman;
use crate::discord_structure;
mod token; mod token;
@ -13,6 +14,7 @@ const HELP_COMMAND: &str = "!jiji";
struct Handler; struct Handler;
#[async_trait] #[async_trait]
impl EventHandler for Handler { impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) { async fn message(&self, ctx: Context, msg: Message) {
@ -31,22 +33,18 @@ impl EventHandler for Handler {
println!("{} is connected!", ready.user.name); println!("{} is connected!", ready.user.name);
let guilds = context.cache.guilds().await; let guilds = context.cache.guilds().await;
let mut guild_names : Vec<String> = vec![]; if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
for guild_id in guilds { for guild_id in guilds {
let guild_name : String = if let Some(guild) = context.cache.guild(guild_id).await { let guild_name : String = if let Some(guild) = context.cache.guild(guild_id).await {
guild.name.clone() guild.name.clone()
} else { } else {
"not found".to_string() "not found".to_string()
}; };
guild_names.push(guild_name.clone()); println!("Guild : '{}' ({})", guild_name.clone(), guild_id.clone());
println!("Guild : '{}' ({})", guild_id, guild_name);
}
if let Some(sender) = context.data.read().await.get::<postman::Sender>() { let guild = discord_structure::Guild::new(guild_name, guild_id.to_string());
let message = postman::Packet::new(postman::PacketKind::GuildName, guild_names[0].clone()); sender.send(postman::Packet::Guild(guild)).expect("Failed to send packet");
sender.send(message).unwrap(); }
println!("Message from bot to gui, sent");
} else { } else {
println!("Failed to retrieve sender"); println!("Failed to retrieve sender");
} }

56
src/discord_structure.rs Normal file
View file

@ -0,0 +1,56 @@
pub struct Guild {
pub name: String,
pub id: String,
pub channels: Vec<Channel>,
}
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<Message>,
}
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,
}
}
}

View file

@ -5,6 +5,7 @@ use tokio::runtime::Runtime;
mod bot; mod bot;
mod postman; mod postman;
mod discord_structure;
const MAX_FPS: f32 = 30.0; const MAX_FPS: f32 = 30.0;
@ -37,6 +38,7 @@ fn gui(receiver: mpsc::Receiver<postman::Packet>) {
struct Jiji { struct Jiji {
next_frame: time::Instant, next_frame: time::Instant,
receiver: mpsc::Receiver<postman::Packet>, receiver: mpsc::Receiver<postman::Packet>,
guilds: Vec<discord_structure::Guild>,
} }
impl Jiji { impl Jiji {
@ -44,6 +46,7 @@ impl Jiji {
Self { Self {
next_frame: time::Instant::now(), next_frame: time::Instant::now(),
receiver, receiver,
guilds: vec![],
} }
} }
} }
@ -56,7 +59,17 @@ impl eframe::App for Jiji {
self.next_frame = time::Instant::now(); self.next_frame = time::Instant::now();
while let Ok(packet) = self.receiver.try_recv() { 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); self.draw_feed(ctx);

View file

@ -1,23 +1,11 @@
use std::sync::mpsc; use std::sync::mpsc;
use serenity::prelude::TypeMapKey; use serenity::prelude::TypeMapKey;
use crate::discord_structure;
pub struct Packet { pub enum Packet {
pub kind: PacketKind, Guild(discord_structure::Guild),
pub content: String, Channel(discord_structure::Channel),
} Message(discord_structure::Message),
impl Packet {
pub fn new(kind : PacketKind, content: String) -> Self {
Self {
kind,
content,
}
}
}
pub enum PacketKind {
GuildName,
} }
pub struct Sender; pub struct Sender;

View file

@ -1,18 +0,0 @@
struct Guild {
name: String,
id: String,
channels: Vec<Channel>,
}
struct Channel {
name: String,
id: String,
messages: Vec<Message>,
}
struct Message {
author: String,
id: String,
content: String,
}