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

View file

@ -1,75 +1,88 @@
use eframe::egui;
use image::GenericImageView;
use std::{error::Error, sync::Arc, thread, time};
use tokio::runtime::Runtime;
mod bot;
const MAX_FPS: f32 = 30.0;
fn main() -> Result<(), eframe::Error> {
let icon_data = load_icon().unwrap_or_default();
fn main() {
println!("hello there");
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([1200.0, 800.0])
.with_icon(Arc::new(icon_data)),
..Default::default()
};
// Create a separate Tokio runtime for the bot
let bot_runtime = Runtime::new().unwrap();
let bot_handle = bot_runtime.handle().clone();
eframe::run_native(
"Jiji",
options,
Box::new(move |_cc| Box::from(Jiji::default())),
)
// 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 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 {
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);
}
next_frame: time::Instant,
}
impl Jiji {
pub fn draw_feed(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.label("Hello there");
});
}
fn new() -> Self {
Self {
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>> {
let (icon_rgba, icon_width, icon_height) = {
let icon = include_bytes!("../assets/icon.png");
let image = image::load_from_memory(icon)?;
let rgba = image.clone().into_rgba8().to_vec();
let (width, height) = image.dimensions();
(rgba, width, height)
};
let (icon_rgba, icon_width, icon_height) = {
let icon = include_bytes!("../assets/icon.png");
let image = image::load_from_memory(icon)?;
let rgba = image.clone().into_rgba8().to_vec();
let (width, height) = image.dimensions();
(rgba, width, height)
};
Ok(egui::IconData {
rgba: icon_rgba,
width: icon_width,
height: icon_height,
})
Ok(egui::IconData {
rgba: icon_rgba,
width: icon_width,
height: icon_height,
})
}