From 035aa514bcf878604b466f62b39868a44f9d4ae8 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 9 Nov 2024 16:49:33 +0100 Subject: [PATCH] Trying out --- src/io.rs | 19 +++++++++++++++---- src/main.rs | 4 ++-- src/votes.rs | 3 +++ src/watchdog.rs | 12 ++++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/io.rs b/src/io.rs index cdcd4ca..59091a3 100644 --- a/src/io.rs +++ b/src/io.rs @@ -12,15 +12,18 @@ //! //! ```no_run //! use idlefox::io::discord_main_loop; +//! use idlefox::votes::EventHolder; //! //! #[tokio::main] //! async fn main() { -//! discord_main_loop().await; +//! let ctx = EventHolder::default(); +//! discord_main_loop(ctx).await; //! } //! ``` //! #![allow(dead_code)] use crate::modules::handler; +use crate::votes::EventHolder; use distance::levenshtein; use poise::serenity_prelude::{self as serenity}; @@ -29,7 +32,10 @@ use poise::Modal as _; use crate::error_handling::IdleError; /// User data, which is stored and accessible in all command invocations -pub struct Data {} +pub struct Data { + /// The global event holder + event_holder: EventHolder, +} /// A generic temporary serenity error type GenericError = Box; /// The context (in a discord sence) of a message @@ -363,7 +369,7 @@ pub async fn long_vote( /// Registers all functions and starts a bot client // As an async loop it is untestable by nature, however this loop aims to stay as simple as possible to be "self testing" #[cfg(not(tarpaulin_include))] -pub async fn discord_main_loop() { +pub async fn discord_main_loop(event_holder: EventHolder) { let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"); let intents = serenity::GatewayIntents::non_privileged(); let framework = poise::Framework::builder() @@ -372,9 +378,14 @@ pub async fn discord_main_loop() { ..Default::default() }) .setup(|ctx, _ready, framework| { + event_holder + .discord_ctx + .lock() + .unwrap() + .replace(ctx.clone()); Box::pin(async move { poise::builtins::register_globally(ctx, &framework.options().commands).await?; - Ok(Data {}) + Ok(Data { event_holder }) }) }) .build(); diff --git a/src/main.rs b/src/main.rs index 5eaa90e..5d6f8de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ async fn main() { // TODO: Check that this is actually what we want task::spawn(main_scheduler_loop( time::interval(time::Duration::from_secs(1)), - event_context, + event_context.clone(), )); - discord_main_loop().await; + discord_main_loop(event_context).await; } diff --git a/src/votes.rs b/src/votes.rs index 650907d..3a94efd 100644 --- a/src/votes.rs +++ b/src/votes.rs @@ -46,6 +46,9 @@ pub struct EventHolder { // pub closed_events: Mutex, /// A reference-counted pointer to a mutex-protected `NaiveDate` instance, representing the last date the watchdog was updated. pub old_date: Arc>, + + /// The newly created discord context + pub discord_ctx: Arc>>, } /// Adds a parsed vote to the list of planned events. diff --git a/src/watchdog.rs b/src/watchdog.rs index 1cbfc15..25414c6 100644 --- a/src/watchdog.rs +++ b/src/watchdog.rs @@ -26,6 +26,7 @@ use chrono::{DateTime, Local, NaiveDate}; #[cfg(not(tarpaulin_include))] // This loop is untestable instead the logic is kept to the minimal pub async fn main_scheduler_loop(mut interval: tokio::time::Interval, ev_ctx: EventHolder) { loop { + fk_around(&ev_ctx); interval.tick().await; // tokio::spawn(async {watchdog_update().await;}); // For async task let ev = ev_ctx.clone(); @@ -33,6 +34,17 @@ pub async fn main_scheduler_loop(mut interval: tokio::time::Interval, ev_ctx: Ev } } +/// Close to finding out +fn fk_around(ev_ctx: &EventHolder) { + println!( + "In the fk_around function ({}):", + Local::now() // &ev_ctx + ); + if let Some(discord_ctx) = ev_ctx.discord_ctx.lock().unwrap().as_mut() { + println!("Fking around : {:#?}", discord_ctx); + } +} + /// The `watchdog_update` function updates the state of the watchdog and executes the necessary tasks. /// /// This function takes the current time as an argument, which is used to determine whether a new day has passed -- 2.45.1