send a start bot request

This commit is contained in:
WanderingPenwing 2024-03-07 18:55:06 +01:00
parent 5fb197390d
commit 5b5f4fb958
2 changed files with 70 additions and 71 deletions

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,27 +13,27 @@ 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);
} }
} }
async fn start_discord_bot() -> Result<Client, String> { pub async fn start_discord_bot() -> Result<Client, String> {
let mut client = Client::new(token::TOKEN) let mut 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))?;
if let Err(why) = client.start().await { if let Err(why) = client.start().await {
return Err(format!("Client error: {:?}", why)); return Err(format!("Client error: {:?}", why));
} }
Ok(client) Ok(client)
} }

View file

@ -1,78 +1,77 @@
use eframe::egui; use eframe::egui;
use image::GenericImageView; use image::GenericImageView;
use serenity::prelude::*; use serenity::prelude::*;
use std::{error::Error, sync::Arc, thread, time}; use std::{error::Error, sync::Arc, thread, time, future::Future};
mod bot; mod bot;
const MAX_FPS: f32 = 30.0; const MAX_FPS: f32 = 30.0;
fn main() -> Result<(), eframe::Error> { fn main() -> Result<(), eframe::Error> {
let icon_data = load_icon().unwrap_or_default(); let icon_data = load_icon().unwrap_or_default();
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default() viewport: egui::ViewportBuilder::default()
.with_inner_size([1200.0, 800.0]) .with_inner_size([1200.0, 800.0])
.with_icon(Arc::new(icon_data)), .with_icon(Arc::new(icon_data)),
..Default::default() ..Default::default()
}; };
eframe::run_native( eframe::run_native(
"Jiji", "Jiji",
options, options,
Box::new(move |_cc| Box::from(Jiji::default())), Box::new(move |_cc| Box::from(Jiji::default())),
) )
} }
struct Jiji { struct Jiji {
next_frame: time::Instant, next_frame: time::Instant,
bot: Option<Client>, bot: Option<Client>,
bot_future: Option<Box<dyn Future<Output = Result<Client, String>>>>,
} }
impl Default for Jiji { impl Default for Jiji {
fn default() -> Self { fn default() -> Self {
// should start the bot Self {
Self { next_frame: time::Instant::now(),
next_frame: time::Instant::now(), bot: None,
bot: None, bot_future: Some(Box::new(bot::start_discord_bot())),
//bot_process_reference }
} }
}
} }
impl eframe::App for Jiji { impl eframe::App for Jiji {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
thread::sleep(time::Duration::from_secs_f32( thread::sleep(time::Duration::from_secs_f32(
((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();
//here if bot started put its reference in self.bot self.draw_feed(ctx);
}
self.draw_feed(ctx);
}
} }
impl Jiji { impl Jiji {
pub fn draw_feed(&mut self, ctx: &egui::Context) { pub fn draw_feed(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
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>> {
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,
}) })
} }