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

@ -1,7 +1,7 @@
use serenity::{ use serenity::{
async_trait, async_trait,
model::{channel::Message, gateway::Ready}, model::{channel::Message, gateway::Ready},
prelude::*, prelude::*,
}; };
mod token; mod token;
@ -13,32 +13,35 @@ 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) {
if msg.content == HELP_COMMAND { if msg.content == HELP_COMMAND {
if let Err(why) = msg.channel_id.say(&ctx.http, HELP_MESSAGE).await { if let Err(why) = msg.channel_id.say(&ctx.http, HELP_MESSAGE).await {
println!("Error sending message: {:?}", why); println!("Error sending message: {:?}", why);
} }
} }
} }
async fn ready(&self, _: Context, ready: Ready) { async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name); println!("{} is connected!", ready.user.name);
} }
} }
pub async fn start_discord_bot() { pub async fn start_discord_bot() {
let maybe_client = Client::builder(token::TOKEN) println!("Creation process started");
.event_handler(Handler) let maybe_client = Client::builder(token::TOKEN)
.await .event_handler(Handler)
.map_err(|why| format!("Client error: {:?}", why)); .await
.map_err(|why| format!("Client error: {:?}", why));
if let Ok(mut client) = maybe_client {
if let Err(why) = client.start().await { println!("Got a result");
eprintln!("Client error: {:?}", why);
return if let Ok(mut client) = maybe_client {
} if let Err(why) = client.start().await {
} else { println!("Client error: {:?}", why);
eprintln!("No Client"); return;
return }
} } else {
println!("No Client");
return;
}
} }

View file

@ -1,75 +1,88 @@
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() {
let icon_data = load_icon().unwrap_or_default(); println!("hello there");
let options = eframe::NativeOptions { // Create a separate Tokio runtime for the bot
viewport: egui::ViewportBuilder::default() let bot_runtime = Runtime::new().unwrap();
.with_inner_size([1200.0, 800.0]) let bot_handle = bot_runtime.handle().clone();
.with_icon(Arc::new(icon_data)),
..Default::default()
};
eframe::run_native( // Spawn the bot task onto the separate runtime
"Jiji", bot_handle.spawn(async move {
options, bot::start_discord_bot().await;
Box::new(move |_cc| Box::from(Jiji::default())), });
)
// Run the GUI on the main thread
gui();
}
fn gui() {
let icon_data = load_icon().unwrap_or_default();
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([1200.0, 800.0])
.with_icon(Arc::new(icon_data)),
..Default::default()
};
let _ = eframe::run_native("Jiji", options, Box::new(move |_cc| Box::from(Jiji::new())));
} }
struct Jiji { struct Jiji {
next_frame: time::Instant, next_frame: time::Instant,
bot: thread::JoinHandle<()>,
}
impl Default for Jiji {
fn default() -> Self {
Self {
next_frame: time::Instant::now(),
bot: thread::spawn(|| {
bot::start_discord_bot();
}),
}
}
}
impl eframe::App for Jiji {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
thread::sleep(time::Duration::from_secs_f32(
((1.0 / MAX_FPS) - self.next_frame.elapsed().as_secs_f32()).max(0.0),
));
self.next_frame = time::Instant::now();
self.draw_feed(ctx);
}
} }
impl Jiji { impl Jiji {
pub fn draw_feed(&mut self, ctx: &egui::Context) { fn new() -> Self {
egui::CentralPanel::default().show(ctx, |ui| { Self {
ui.label("Hello there"); next_frame: time::Instant::now(),
}); }
} }
}
impl eframe::App for Jiji {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
thread::sleep(time::Duration::from_secs_f32(
((1.0 / MAX_FPS) - self.next_frame.elapsed().as_secs_f32()).max(0.0),
));
self.next_frame = time::Instant::now();
self.draw_feed(ctx);
}
fn on_exit(&mut self, _gl: std::option::Option<&eframe::glow::Context>) {
//self.runtime.shutdown_background();
}
}
impl Jiji {
pub fn draw_feed(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.label("Hello there");
});
}
} }
pub fn load_icon() -> Result<egui::IconData, Box<dyn Error>> { pub fn load_icon() -> Result<egui::IconData, Box<dyn Error>> {
let (icon_rgba, icon_width, icon_height) = { let (icon_rgba, icon_width, icon_height) = {
let icon = include_bytes!("../assets/icon.png"); let icon = include_bytes!("../assets/icon.png");
let image = image::load_from_memory(icon)?; let image = image::load_from_memory(icon)?;
let rgba = image.clone().into_rgba8().to_vec(); let rgba = image.clone().into_rgba8().to_vec();
let (width, height) = image.dimensions(); let (width, height) = image.dimensions();
(rgba, width, height) (rgba, width, height)
}; };
Ok(egui::IconData { Ok(egui::IconData {
rgba: icon_rgba, rgba: icon_rgba,
width: icon_width, width: icon_width,
height: icon_height, height: icon_height,
}) })
} }