gui can receive packet from the bot

This commit is contained in:
WanderingPenwing 2024-03-09 16:59:26 +01:00
parent d11305b039
commit 886e29e72d
4 changed files with 35 additions and 13 deletions

View file

@ -44,7 +44,7 @@ impl EventHandler for Handler {
} }
if let Some(sender) = context.data.read().await.get::<postman::Sender>() { if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
let message = postman::Message::new(postman::MessageType::GuildName, guild_names[0].clone()); let message = postman::Packet::new(postman::PacketKind::GuildName, guild_names[0].clone());
sender.send(message).unwrap(); sender.send(message).unwrap();
println!("Message from bot to gui, sent"); println!("Message from bot to gui, sent");
} else { } else {
@ -53,7 +53,7 @@ impl EventHandler for Handler {
} }
} }
pub async fn start_discord_bot(sender: mpsc::Sender<postman::Message>) { pub async fn start_discord_bot(sender: mpsc::Sender<postman::Packet>) {
println!("Bot connection process started..."); println!("Bot connection process started...");
let maybe_client = Client::builder(token::TOKEN) let maybe_client = Client::builder(token::TOKEN)
.event_handler(Handler) .event_handler(Handler)

View file

@ -9,7 +9,7 @@ mod postman;
const MAX_FPS: f32 = 30.0; const MAX_FPS: f32 = 30.0;
fn main() { fn main() {
let (tx, rx) = mpsc::channel::<postman::Message>(); //tx transmiter let (tx, rx) = mpsc::channel::<postman::Packet>(); //tx transmiter
let _handle = thread::spawn(move || { let _handle = thread::spawn(move || {
println!("Bot thread spawned"); println!("Bot thread spawned");
@ -21,7 +21,7 @@ fn main() {
gui(rx); gui(rx);
} }
fn gui(receiver: mpsc::Receiver<postman::Message>) { fn gui(receiver: mpsc::Receiver<postman::Packet>) {
let icon_data = load_icon().unwrap_or_default(); let icon_data = load_icon().unwrap_or_default();
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
@ -36,11 +36,11 @@ fn gui(receiver: mpsc::Receiver<postman::Message>) {
struct Jiji { struct Jiji {
next_frame: time::Instant, next_frame: time::Instant,
receiver: mpsc::Receiver<postman::Message>, receiver: mpsc::Receiver<postman::Packet>,
} }
impl Jiji { impl Jiji {
fn new(receiver: mpsc::Receiver<postman::Message>) -> Self { fn new(receiver: mpsc::Receiver<postman::Packet>) -> Self {
Self { Self {
next_frame: time::Instant::now(), next_frame: time::Instant::now(),
receiver, receiver,
@ -54,6 +54,10 @@ impl eframe::App for Jiji {
((1.0 / MAX_FPS) - self.next_frame.elapsed().as_secs_f32()).max(0.0), ((1.0 / MAX_FPS) - self.next_frame.elapsed().as_secs_f32()).max(0.0),
)); ));
self.next_frame = time::Instant::now(); self.next_frame = time::Instant::now();
while let Ok(packet) = self.receiver.try_recv() {
println!("Message from bot to gui received : {}", packet.content);
}
self.draw_feed(ctx); self.draw_feed(ctx);
} }

View file

@ -1,13 +1,13 @@
use std::sync::mpsc; use std::sync::mpsc;
use serenity::prelude::TypeMapKey; use serenity::prelude::TypeMapKey;
pub struct Message { pub struct Packet {
kind: MessageType, pub kind: PacketKind,
content: String, pub content: String,
} }
impl Message { impl Packet {
pub fn new(kind : MessageType, content: String) -> Self { pub fn new(kind : PacketKind, content: String) -> Self {
Self { Self {
kind, kind,
content, content,
@ -16,12 +16,12 @@ impl Message {
} }
pub enum MessageType { pub enum PacketKind {
GuildName, GuildName,
} }
pub struct Sender; pub struct Sender;
impl TypeMapKey for Sender { impl TypeMapKey for Sender {
type Value = mpsc::Sender<Message>; type Value = mpsc::Sender<Packet>;
} }

18
src/tree.rs Normal file
View file

@ -0,0 +1,18 @@
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,
}