From 1873a22e23cba1ccd9a3b2557c1295c6fe73db9f Mon Sep 17 00:00:00 2001 From: ByteDice Date: Wed, 19 Feb 2025 16:14:31 +0100 Subject: [PATCH] added a schedule system --- TODO.md | 8 +++---- src/main.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++--- src/websocket.rs | 2 ++ 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/TODO.md b/TODO.md index c370be9..86de13b 100644 --- a/TODO.md +++ b/TODO.md @@ -4,16 +4,16 @@ - ยจ + - [ ] `/bk_week_top [category] [amount]` to get the top N posts in a category (e.g upvotes) - [ ] Allow updating the data autonomously and via manual commands. - [ ] 10-minute schedule for updating Discord channel - - [ ] Manually add posts - - [ ] via `u/[bot] add` - - [ ] 2 minute schedule for responding to commands + + + diff --git a/src/main.rs b/src/main.rs index 823229d..5862358 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,11 @@ mod macros; mod websocket; mod data; +use std::future::Future; +use std::pin::Pin; use std::process; use std::thread; +use std::time::Duration; use clap::Parser; use poise::serenity_prelude as serenity; @@ -20,6 +23,9 @@ use serde_json::Value; use tokio::runtime::Runtime; use tokio::sync::Mutex; use serde_json; +use tokio::task::JoinHandle; +use tokio::time; +use websocket::send_cmd_json; // TODO: bot command permissions @@ -43,6 +49,11 @@ struct Args { noping: bool } + +type Error = Box; +type Context<'a> = poise::Context<'a, Data, Error>; + + struct Data { ball_prompts: [Vec; 2], byte_dice_id: u64, @@ -50,10 +61,7 @@ struct Data { discord_data: Mutex>, bk_mods_json: Value, args: Args - // TODO: schedules } -type Error = Box; -type Context<'a> = poise::Context<'a, Data, Error>; static BK_WEEK: &str = "bk_weekly_art_posts"; @@ -100,6 +108,13 @@ async fn main() { let _ = python::start(python_args); }); + let schedules: Vec<(Duration, fn() -> Pin + Send>>)> = vec![ + (Duration::from_secs(/* 2 * 60 */ 30), || Box::pin(read_reddit_inbox())), + (Duration::from_secs(/* 10 * 60 */ 60), || Box::pin(update_post_channels())) + ]; + + run_schedules(schedules).await; + rust.join().unwrap(); python.join().unwrap(); } @@ -191,4 +206,38 @@ async fn gen_bot(data: Data, args: Args) -> Client { .framework(framework) .await .unwrap(); +} + + +async fn run_schedule Pin + Send>>>(d: Duration, f: F) { + let mut ticker = time::interval(d); + loop { + ticker.tick().await; + f().await; + } +} + + +async fn run_schedules(schedules: Vec<(Duration, fn() -> Pin + Send>>)>) { + let mut handles: Vec> = vec![]; + + rs_println!("Starting schedules..."); + for (d, f) in schedules { + let handle = tokio::spawn(run_schedule(d, f)); + handles.push(handle); + } + + for handle in handles { + let _ = handle.await; + } +} + + +async fn read_reddit_inbox() { + unsafe { if !websocket::HAS_CONNECTED { return; } } + send_cmd_json("respond_mentions", None).await; +} + +async fn update_post_channels() { + println!("simulated update post channels"); } \ No newline at end of file diff --git a/src/websocket.rs b/src/websocket.rs index 05392c0..2cecd4f 100644 --- a/src/websocket.rs +++ b/src/websocket.rs @@ -18,6 +18,7 @@ type Receiver = Arc>>>>; static mut GLOBAL_SENDER: Option = None; static mut GLOBAL_RECEIVER: Option = None; static mut REPLY_HELLO: bool = false; +pub static mut HAS_CONNECTED: bool = false; async fn set_sender(sender: Sender) { @@ -139,6 +140,7 @@ async fn handle_message(msg: tungstenite::protocol::Message, args: Args) { if !REPLY_HELLO { send_msg("[Connection test] Hello from Rust!").await; REPLY_HELLO = true; + HAS_CONNECTED = true; } } }