added a schedule system

This commit is contained in:
2025-02-19 16:14:31 +01:00
parent c24b0d5d5b
commit 1873a22e23
3 changed files with 58 additions and 7 deletions
+4 -4
View File
@@ -4,16 +4,16 @@
<!-- - [x] ~~Discord bot /bk_help command~~ -->
<!-- - [x] ~~Scrape the data~~ -->
<!-- - [x] ~~Put it in a JSON~~ -->
<!-- - [x] ~~Multithread so it can run both Discord and Reddit bot!!!~~ -->¨
<!-- - [x] ~~Multithread so it can run both Discord and Reddit bot!!!~~ -->
<!-- - [x] Security that only allows bk mods to run these commands. -->
<!-- - [x] Some kind of voting system. -->
- [ ] `/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
<!-- - [x] ~~Manually add posts~~ -->
<!-- - [x] ~~via `u/[bot] add`~~ -->
<!-- - [x] ~~via `/bk_week_add [url]`~~ -->
<!-- - [x] ~~2 minute schedule for responding to commands~~ -->
<!-- - [x] ~~Manually remove posts via `/bk_week_remove [url]`~~ -->
<!-- - [x] ~~Manually approve posts via `/bk_week_approve [url]`~~ -->
<!-- - [x] ~~Manually un-approve posts via `/bk_week_disapprove [url]`~~ -->
+52 -3
View File
@@ -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<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
struct Data {
ball_prompts: [Vec<String>; 2],
byte_dice_id: u64,
@@ -50,10 +61,7 @@ struct Data {
discord_data: Mutex<Option<Value>>,
bk_mods_json: Value,
args: Args
// TODO: schedules
}
type Error = Box<dyn std::error::Error + Send + Sync>;
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<Box<dyn Future<Output = ()> + 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<F: Fn() -> Pin<Box<dyn Future<Output = ()> + 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<Box<dyn Future<Output = ()> + Send>>)>) {
let mut handles: Vec<JoinHandle<()>> = 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");
}
+2
View File
@@ -18,6 +18,7 @@ type Receiver = Arc<Mutex<Option<SplitStream<WebSocketStream<TcpStream>>>>>;
static mut GLOBAL_SENDER: Option<Sender> = None;
static mut GLOBAL_RECEIVER: Option<Receiver> = 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;
}
}
}