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 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<String> = 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::<postman::Sender>() {
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");
}

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 postman;
mod discord_structure;
const MAX_FPS: f32 = 30.0;
@ -37,6 +38,7 @@ fn gui(receiver: mpsc::Receiver<postman::Packet>) {
struct Jiji {
next_frame: time::Instant,
receiver: mpsc::Receiver<postman::Packet>,
guilds: Vec<discord_structure::Guild>,
}
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);

View file

@ -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;

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,
}