From 2317a41bba896b202f83254c91ab597700d0ddb1 Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Thu, 7 Mar 2024 19:30:10 +0100 Subject: [PATCH] tried to spawn a thread for the bot to run freely --- src/bot.rs | 19 ++++++++++++------- src/main.rs | 12 +++++------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 7b99cb3..79bfc42 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -26,14 +26,19 @@ impl EventHandler for Handler { } } -pub async fn start_discord_bot() -> Result { - let mut client = Client::builder(token::TOKEN) +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 Err(why) = client.start().await { - return Err(format!("Client error: {:?}", why)); + .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 } - Ok(client) } diff --git a/src/main.rs b/src/main.rs index c9251bb..09778c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,6 @@ use eframe::egui; use image::GenericImageView; -use serenity::prelude::*; -use std::{error::Error, sync::Arc, thread, time, future::Future}; - +use std::{error::Error, sync::Arc, thread, time}; mod bot; const MAX_FPS: f32 = 30.0; @@ -26,16 +24,16 @@ fn main() -> Result<(), eframe::Error> { struct Jiji { next_frame: time::Instant, - bot: Option, - bot_future: Option>>>, + bot: thread::JoinHandle<()>, } impl Default for Jiji { fn default() -> Self { Self { next_frame: time::Instant::now(), - bot: None, - bot_future: Some(Box::new(bot::start_discord_bot())), + bot: thread::spawn(|| { + bot::start_discord_bot(); + }), } } }