server selection and reverse communication prototype
This commit is contained in:
parent
17d7c6095f
commit
ce2992f524
24
src/bot.rs
24
src/bot.rs
|
@ -4,6 +4,7 @@ use serenity::{
|
|||
prelude::*,
|
||||
};
|
||||
use std::sync::mpsc;
|
||||
use std::sync::Mutex;
|
||||
use crate::postman;
|
||||
use crate::discord_structure;
|
||||
|
||||
|
@ -18,14 +19,13 @@ struct Handler;
|
|||
#[async_trait]
|
||||
impl EventHandler for Handler {
|
||||
async fn message(&self, ctx: Context, msg: Message) {
|
||||
println!("Message received : '{}' from {}", msg.content, msg.author);
|
||||
println!("bot : message received : '{}' from {}", msg.content, msg.author);
|
||||
if msg.content == HELP_COMMAND {
|
||||
println!("Message is command");
|
||||
if let Err(why) = msg.channel_id.say(&ctx.http, HELP_MESSAGE).await {
|
||||
println!("Error sending message: {:?}", why);
|
||||
println!("bot : Error sending message: {:?}", why);
|
||||
return
|
||||
}
|
||||
println!("Successfuly sent message");
|
||||
println!("bot : successfuly sent reply");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -40,32 +40,36 @@ impl EventHandler for Handler {
|
|||
} else {
|
||||
"not found".to_string()
|
||||
};
|
||||
println!("Guild : '{}' ({})", guild_name.clone(), guild_id.clone());
|
||||
println!("bot : found 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");
|
||||
println!("bot : failed to retrieve sender");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start_discord_bot(sender: mpsc::Sender<postman::Packet>) {
|
||||
println!("Bot connection process started...");
|
||||
pub async fn start_discord_bot(sender: mpsc::Sender<postman::Packet>, receiver: Mutex<mpsc::Receiver<postman::Packet>>) {
|
||||
println!("bot : connection process started...");
|
||||
let maybe_client = Client::builder(token::TOKEN)
|
||||
.event_handler(Handler)
|
||||
.type_map_insert::<postman::Sender>(sender)
|
||||
.type_map_insert::<postman::Receiver>(receiver)
|
||||
.await
|
||||
.map_err(|why| format!("Client error: {:?}", why));
|
||||
|
||||
//let mut rx = bot_rx.lock().unwrap(); // Lock the receiver
|
||||
//let msg = rx.recv().unwrap(); // Receive a message
|
||||
|
||||
if let Ok(mut client) = maybe_client {
|
||||
if let Err(why) = client.start().await {
|
||||
println!("Client error: {:?}", why);
|
||||
println!("bot : client error: {:?}", why);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
println!("No Client");
|
||||
println!("bot : no client");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
|
||||
#[derive(PartialEq, Clone)]
|
||||
pub struct Guild {
|
||||
pub name: String,
|
||||
pub id: String,
|
||||
|
@ -15,7 +15,7 @@ impl Guild {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Clone)]
|
||||
pub struct Channel {
|
||||
pub name: String,
|
||||
pub id: String,
|
||||
|
@ -34,7 +34,7 @@ impl Channel {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Clone)]
|
||||
pub struct Message {
|
||||
pub author_name: String,
|
||||
pub id: String,
|
||||
|
|
62
src/main.rs
62
src/main.rs
|
@ -2,6 +2,7 @@ use eframe::egui;
|
|||
use image::GenericImageView;
|
||||
use std::{error::Error, sync::Arc, sync::mpsc, thread, time};
|
||||
use tokio::runtime::Runtime;
|
||||
use std::sync::Mutex;
|
||||
|
||||
mod bot;
|
||||
mod postman;
|
||||
|
@ -10,19 +11,21 @@ mod discord_structure;
|
|||
const MAX_FPS: f32 = 30.0;
|
||||
|
||||
fn main() {
|
||||
let (tx, rx) = mpsc::channel::<postman::Packet>(); //tx transmiter
|
||||
let (bot_tx, gui_rx) = mpsc::channel::<postman::Packet>(); //tx transmiter
|
||||
let (gui_tx, bot_rx) = mpsc::channel::<postman::Packet>(); //tx transmiter
|
||||
let bot_rx = Mutex::new(bot_rx);
|
||||
|
||||
let _handle = thread::spawn(move || {
|
||||
println!("Bot thread spawned");
|
||||
println!("main : bot thread spawned");
|
||||
let mut rt = Runtime::new().unwrap();
|
||||
rt.block_on(bot::start_discord_bot(tx));
|
||||
rt.block_on(bot::start_discord_bot(bot_tx, bot_rx));
|
||||
});
|
||||
|
||||
// Run the GUI on the main thread
|
||||
gui(rx);
|
||||
gui(gui_tx, gui_rx);
|
||||
}
|
||||
|
||||
fn gui(receiver: mpsc::Receiver<postman::Packet>) {
|
||||
fn gui(sender: mpsc::Sender<postman::Packet>, receiver: mpsc::Receiver<postman::Packet>) {
|
||||
let icon_data = load_icon().unwrap_or_default();
|
||||
|
||||
let options = eframe::NativeOptions {
|
||||
|
@ -32,21 +35,25 @@ fn gui(receiver: mpsc::Receiver<postman::Packet>) {
|
|||
..Default::default()
|
||||
};
|
||||
|
||||
let _ = eframe::run_native("Jiji", options, Box::new(move |_cc| Box::from(Jiji::new(receiver))));
|
||||
let _ = eframe::run_native("Jiji", options, Box::new(move |_cc| Box::from(Jiji::new(sender, receiver))));
|
||||
}
|
||||
|
||||
struct Jiji {
|
||||
next_frame: time::Instant,
|
||||
sender: mpsc::Sender<postman::Packet>,
|
||||
receiver: mpsc::Receiver<postman::Packet>,
|
||||
guilds: Vec<discord_structure::Guild>,
|
||||
selected_guild: Option<discord_structure::Guild>,
|
||||
}
|
||||
|
||||
impl Jiji {
|
||||
fn new(receiver: mpsc::Receiver<postman::Packet>) -> Self {
|
||||
fn new(sender: mpsc::Sender<postman::Packet>, receiver: mpsc::Receiver<postman::Packet>) -> Self {
|
||||
Self {
|
||||
next_frame: time::Instant::now(),
|
||||
sender,
|
||||
receiver,
|
||||
guilds: vec![],
|
||||
selected_guild: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -62,6 +69,7 @@ impl eframe::App for Jiji {
|
|||
match packet {
|
||||
postman::Packet::Guild(guild) => {
|
||||
println!("gui : guild received : '{}'", guild.name);
|
||||
self.guilds.push(guild);
|
||||
}
|
||||
postman::Packet::Channel(channel) => {
|
||||
println!("gui : channel received : '{}'", channel.name);
|
||||
|
@ -69,6 +77,9 @@ impl eframe::App for Jiji {
|
|||
postman::Packet::Message(message) => {
|
||||
println!("gui : message received : '{}'", message.content);
|
||||
}
|
||||
_ => {
|
||||
println!("unhandled packet");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,8 +93,43 @@ impl eframe::App for Jiji {
|
|||
|
||||
impl Jiji {
|
||||
pub fn draw_feed(&mut self, ctx: &egui::Context) {
|
||||
egui::TopBottomPanel::top("server_selection")
|
||||
.resizable(false)
|
||||
.show(ctx, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Where do you want to look ? ");
|
||||
let selected_text = if let Some(guild) = &self.selected_guild {
|
||||
guild.name.clone()
|
||||
} else {
|
||||
"None".to_string()
|
||||
};
|
||||
|
||||
egui::ComboBox::from_label("")
|
||||
.selected_text(format!("{}", selected_text))
|
||||
.show_ui(ui, |ui| {
|
||||
ui.style_mut().wrap = Some(false);
|
||||
ui.set_min_width(60.0);
|
||||
if ui.add(egui::SelectableLabel::new(self.selected_guild == None, "None")).clicked() {
|
||||
self.selected_guild = None;
|
||||
}
|
||||
for guild in self.guilds.clone() {
|
||||
if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(guild.clone()), guild.name.clone())).clicked() {
|
||||
self.selected_guild = Some(guild);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if let Some(guild) = &self.selected_guild {
|
||||
if guild.channels.len() == 0 {
|
||||
if ui.add(egui::Button::new("get channels")).clicked() {
|
||||
let _ = self.sender.send(postman::Packet::FetchChannels(guild.id.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.label("Hello there");
|
||||
ui.label("General Kenobi");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
use std::sync::mpsc;
|
||||
use serenity::prelude::TypeMapKey;
|
||||
use crate::discord_structure;
|
||||
use std::sync::Mutex;
|
||||
|
||||
pub enum Packet {
|
||||
Guild(discord_structure::Guild),
|
||||
FetchChannels(String),
|
||||
Channel(discord_structure::Channel),
|
||||
Message(discord_structure::Message),
|
||||
}
|
||||
|
@ -13,3 +15,9 @@ pub struct Sender;
|
|||
impl TypeMapKey for Sender {
|
||||
type Value = mpsc::Sender<Packet>;
|
||||
}
|
||||
|
||||
pub struct Receiver;
|
||||
|
||||
impl TypeMapKey for Receiver {
|
||||
type Value = Mutex<mpsc::Receiver<Packet>>;
|
||||
}
|
Loading…
Reference in a new issue