diff --git a/jiji.project b/jiji.project index 2d8d1f0..4d0df87 100644 --- a/jiji.project +++ b/jiji.project @@ -1 +1 @@ -{"categories":[{"name":"to do","content":[{"name":"ability to change token","description":"use a config file to store token so that \n\n1- it is away from github\n\n2- it is configurable if need be","id":1},{"name":"clean up bot code","description":"try to remove unnecessary code\n\nunindent\n\ngive sender to function ?","id":2},{"name":"trayable ?","description":"// Hello there","id":5},{"name":"new message marker","description":"perma if too complicated to detect read","id":2},{"name":"proper links","description":"when there is a link, ability to click it","id":3}]},{"name":"in progress","content":[]},{"name":"done","content":[{"name":"run discord bot","description":"make it so the bot is running","id":1},{"name":"fixed token in github","description":"// Hello there","id":1},{"name":"fetch previous messages","description":"// Hello there","id":4},{"name":"ability to write messages","description":"// Hello there","id":5},{"name":"get incoming messages","description":"read","id":2},{"name":"unread system","description":"add a * when a channel just received a message","id":1},{"name":"remember channel id for dm","description":"and put in config file\n\nmaybe load message ? dm first ?","id":3},{"name":"bug : does not save notification state for dm","description":"// Hello there","id":1},{"name":"handle unknown channel better","description":"when receiving a message from a not yet scanned guild, create the channel and put the message\n\nallow scanning if guild selected\n\ndo not add duplicate channel","id":1},{"name":"guild unread ?","description":"// Hello there","id":2},{"name":"timestamps","description":"// Hello there","id":1},{"name":"better ui error display","description":"handle the error packet for better display","id":1},{"name":"notifications !!!","description":"// Hello there","id":4}]},{"name":"bugs","content":[]},{"name":"v1.0","content":[]},{"name":"+","content":[]}]} \ No newline at end of file +{"categories":[{"name":"to do","content":[{"name":"clean up bot code","description":"try to remove unnecessary code\n\nunindent\n\ngive sender to function ?","id":2},{"name":"trayable ?","description":"// Hello there","id":5},{"name":"new message marker","description":"perma if too complicated to detect read","id":2},{"name":"proper links","description":"when there is a link, ability to click it","id":3}]},{"name":"in progress","content":[]},{"name":"done","content":[{"name":"run discord bot","description":"make it so the bot is running","id":1},{"name":"fixed token in github","description":"// Hello there","id":1},{"name":"fetch previous messages","description":"// Hello there","id":4},{"name":"ability to write messages","description":"// Hello there","id":5},{"name":"get incoming messages","description":"read","id":2},{"name":"unread system","description":"add a * when a channel just received a message","id":1},{"name":"remember channel id for dm","description":"and put in config file\n\nmaybe load message ? dm first ?","id":3},{"name":"bug : does not save notification state for dm","description":"// Hello there","id":1},{"name":"handle unknown channel better","description":"when receiving a message from a not yet scanned guild, create the channel and put the message\n\nallow scanning if guild selected\n\ndo not add duplicate channel","id":1},{"name":"guild unread ?","description":"// Hello there","id":2},{"name":"timestamps","description":"// Hello there","id":1},{"name":"better ui error display","description":"handle the error packet for better display","id":1},{"name":"notifications !!!","description":"// Hello there","id":4}]},{"name":"bugs","content":[]},{"name":"v1.0","content":[{"name":"ability to change token","description":"use a config file to store token so that \n\n1- it is away from github\n\n2- it is configurable if need be","id":1}]},{"name":"+","content":[]}]} \ No newline at end of file diff --git a/src/bot.rs b/src/bot.rs index 569eafd..5bd3f9f 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -17,7 +17,7 @@ use tokio::time::interval; use crate::postman; use crate::discord_structure; -mod token; +pub mod token; const HELP_MESSAGE: &str = "Hello there, Human! I am a messenger for the wandering penwing."; const HELP_COMMAND: &str = "!penwing"; @@ -245,13 +245,13 @@ async fn get_guilds(context: &Context) { } } -pub async fn start_discord_bot(sender: mpsc::Sender, receiver: Mutex>) { +pub async fn start_discord_bot(token: &str, sender: mpsc::Sender, receiver: Mutex>) { println!("bot : connection process started..."); - let maybe_client = Client::builder(token::TOKEN) + let maybe_client = Client::builder(token) .event_handler(Handler { is_loop_running: AtomicBool::new(false), }) - .type_map_insert::(sender) + .type_map_insert::(sender.clone()) .type_map_insert::(receiver) .await .map_err(|why| format!("Client error: {:?}", why)); @@ -259,13 +259,25 @@ pub async fn start_discord_bot(sender: mpsc::Sender, receiver: //let mut rx = bot_rx.lock().unwrap(); // Lock the receiver //let msg = rx.recv().unwrap(); // Receive a message - if let Ok(mut client) = maybe_client { - if let Err(why) = client.start().await { - println!("bot : client error: {:?}", why); - return; +// if let Ok(mut client) = maybe_client { +// if let Err(why) = client.start().await { +// println!("bot : client error: {:?}", why); +// return; +// } +// } else { +// println!("bot : no client"); +// return; +// } + + match maybe_client { + Ok(mut client) => { + if let Err(why) = client.start().await { + sender.send(postman::Packet::Error(format!("Start error: {:?}", why))).expect("Failed to send packet"); + return; + } + } + Err(why) => { + sender.send(postman::Packet::Error(why)).expect("Failed to send packet"); } - } else { - println!("bot : no client"); - return; } } diff --git a/src/main.rs b/src/main.rs index 34a0349..1b35f6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,10 +22,12 @@ fn main() { let (gui_tx, bot_rx) = mpsc::channel::(); //tx transmiter let bot_rx = Mutex::new(bot_rx); + let token = bot::token::TOKEN; + let _handle = thread::spawn(move || { println!("main : bot thread spawned"); let mut rt = Runtime::new().unwrap(); - rt.block_on(bot::start_discord_bot(bot_tx, bot_rx)); + rt.block_on(bot::start_discord_bot(token, bot_tx, bot_rx)); }); // Run the GUI on the main thread