Trying out #35
19
src/io.rs
19
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<dyn std::error::Error + Send + Sync>;
|
||||
/// 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();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,6 +46,9 @@ pub struct EventHolder {
|
|||
// pub closed_events: Mutex<ClosedEvents>,
|
||||
/// A reference-counted pointer to a mutex-protected `NaiveDate` instance, representing the last date the watchdog was updated.
|
||||
pub old_date: Arc<Mutex<NaiveDate>>,
|
||||
|
||||
/// The newly created discord context
|
||||
pub discord_ctx: Arc<Mutex<Option<poise::serenity_prelude::Context>>>,
|
||||
}
|
||||
|
||||
/// Adds a parsed vote to the list of planned events.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue