not much progress, but refactored

This commit is contained in:
WanderingPenwing 2024-03-07 20:54:10 +01:00
parent 2317a41bba
commit 8988bbd180
3 changed files with 101 additions and 84 deletions

View file

@ -7,6 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
eframe = "0.26.2" eframe = "0.26.2"
futures = "0.3.30"
image = "0.24.9" image = "0.24.9"
tokio = { version = "0.2", features = ["macros"] }
serenity = { default-features = false, features = ["client", "gateway", "model", "rustls_backend"], version = "0.9.0-rc.2"} serenity = { default-features = false, features = ["client", "gateway", "model", "rustls_backend"], version = "0.9.0-rc.2"}
tokio = {features = ["macros"], version = "0.2"}

View file

@ -27,18 +27,21 @@ impl EventHandler for Handler {
} }
pub async fn start_discord_bot() { pub async fn start_discord_bot() {
println!("Creation process started");
let maybe_client = Client::builder(token::TOKEN) let maybe_client = Client::builder(token::TOKEN)
.event_handler(Handler) .event_handler(Handler)
.await .await
.map_err(|why| format!("Client error: {:?}", why)); .map_err(|why| format!("Client error: {:?}", why));
println!("Got a result");
if let Ok(mut client) = maybe_client { if let Ok(mut client) = maybe_client {
if let Err(why) = client.start().await { if let Err(why) = client.start().await {
eprintln!("Client error: {:?}", why); println!("Client error: {:?}", why);
return return;
} }
} else { } else {
eprintln!("No Client"); println!("No Client");
return return;
} }
} }

View file

@ -1,11 +1,29 @@
use eframe::egui; use eframe::egui;
use image::GenericImageView; use image::GenericImageView;
use std::{error::Error, sync::Arc, thread, time}; use std::{error::Error, sync::Arc, thread, time};
use tokio::runtime::Runtime;
mod bot; mod bot;
const MAX_FPS: f32 = 30.0; const MAX_FPS: f32 = 30.0;
fn main() -> Result<(), eframe::Error> { fn main() {
println!("hello there");
// Create a separate Tokio runtime for the bot
let bot_runtime = Runtime::new().unwrap();
let bot_handle = bot_runtime.handle().clone();
// Spawn the bot task onto the separate runtime
bot_handle.spawn(async move {
bot::start_discord_bot().await;
});
// Run the GUI on the main thread
gui();
}
fn gui() {
let icon_data = load_icon().unwrap_or_default(); let icon_data = load_icon().unwrap_or_default();
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
@ -15,25 +33,17 @@ fn main() -> Result<(), eframe::Error> {
..Default::default() ..Default::default()
}; };
eframe::run_native( let _ = eframe::run_native("Jiji", options, Box::new(move |_cc| Box::from(Jiji::new())));
"Jiji",
options,
Box::new(move |_cc| Box::from(Jiji::default())),
)
} }
struct Jiji { struct Jiji {
next_frame: time::Instant, next_frame: time::Instant,
bot: thread::JoinHandle<()>,
} }
impl Default for Jiji { impl Jiji {
fn default() -> Self { fn new() -> Self {
Self { Self {
next_frame: time::Instant::now(), next_frame: time::Instant::now(),
bot: thread::spawn(|| {
bot::start_discord_bot();
}),
} }
} }
} }
@ -47,6 +57,10 @@ impl eframe::App for Jiji {
self.draw_feed(ctx); self.draw_feed(ctx);
} }
fn on_exit(&mut self, _gl: std::option::Option<&eframe::glow::Context>) {
//self.runtime.shutdown_background();
}
} }
impl Jiji { impl Jiji {
@ -55,7 +69,6 @@ impl Jiji {
ui.label("Hello there"); ui.label("Hello there");
}); });
} }
} }
pub fn load_icon() -> Result<egui::IconData, Box<dyn Error>> { pub fn load_icon() -> Result<egui::IconData, Box<dyn Error>> {