feat: motivationnnn

This commit is contained in:
Max batleforc 2025-06-17 00:57:47 +02:00
parent 00ef0c09c5
commit a607856288
No known key found for this signature in database
GPG Key ID: 25D243AB4B6AC9E7
6 changed files with 224 additions and 106314 deletions

File diff suppressed because it is too large Load Diff

78
Cargo.lock generated
View File

@ -597,6 +597,28 @@ dependencies = [
"windows-link", "windows-link",
] ]
[[package]]
name = "chrono-tz"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efdce149c370f133a071ca8ef6ea340b7b88748ab0810097a9e2976eaa34b4f3"
dependencies = [
"chrono",
"chrono-tz-build",
"phf",
"serde",
]
[[package]]
name = "chrono-tz-build"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f10f8c9340e31fc120ff885fcdb54a0b48e474bbd77cab557f0c30a3e569402"
dependencies = [
"parse-zoneinfo",
"phf_codegen",
]
[[package]] [[package]]
name = "cityhash-rs" name = "cityhash-rs"
version = "1.0.1" version = "1.0.1"
@ -738,6 +760,9 @@ name = "cron_scheduler"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"chrono-tz",
"clickhouse",
"clickhouse_pool",
"config", "config",
"database", "database",
"poise", "poise",
@ -2027,12 +2052,59 @@ dependencies = [
"windows-targets 0.52.6", "windows-targets 0.52.6",
] ]
[[package]]
name = "parse-zoneinfo"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f2a05b18d44e2957b88f96ba460715e295bc1d7510468a2f3d3b44535d26c24"
dependencies = [
"regex",
]
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.3.1" version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "phf"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078"
dependencies = [
"phf_shared",
]
[[package]]
name = "phf_codegen"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aef8048c789fa5e851558d709946d6d79a8ff88c0440c587967f8e94bfb1216a"
dependencies = [
"phf_generator",
"phf_shared",
]
[[package]]
name = "phf_generator"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d"
dependencies = [
"phf_shared",
"rand 0.8.5",
]
[[package]]
name = "phf_shared"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5"
dependencies = [
"siphasher",
]
[[package]] [[package]]
name = "pin-project" name = "pin-project"
version = "1.1.10" version = "1.1.10"
@ -2779,6 +2851,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "siphasher"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d"
[[package]] [[package]]
name = "skeptic" name = "skeptic"
version = "0.13.7" version = "0.13.7"

View File

@ -9,6 +9,9 @@ poise = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
uuid = { workspace = true } uuid = { workspace = true }
chrono = { workspace = true } chrono = { workspace = true }
chrono-tz = { version = "0.10", features = ["serde"] }
clickhouse_pool = { path = "../../libs/clickhouse_pool" }
clickhouse = { workspace = true }
database = { path = "../../libs/database" } database = { path = "../../libs/database" }
config = { path = "../../libs/config" } config = { path = "../../libs/config" }
tokio-cron-scheduler = { version = "0.14", features = [ tokio-cron-scheduler = { version = "0.14", features = [

View File

@ -1,3 +1,9 @@
use chrono_tz::Europe;
use clickhouse_pool::{pool_manager::PoolManager, traits::Model};
use database::{
trivial::{Trivial, TrivialStatus},
trivial_round::TrivialRound,
};
use poise::serenity_prelude::Http; use poise::serenity_prelude::Http;
use std::{ use std::{
collections::HashMap, collections::HashMap,
@ -5,8 +11,8 @@ use std::{
sync::Arc, sync::Arc,
}; };
use tokio::sync::RwLock; use tokio::sync::RwLock;
use tokio_cron_scheduler::JobScheduler; use tokio_cron_scheduler::{job::JobLocked, Job, JobScheduler};
use tracing::{error, info, instrument}; use tracing::{debug, error, info, info_span, instrument, Instrument};
use uuid::Uuid; use uuid::Uuid;
#[derive(Debug)] #[derive(Debug)]
@ -105,18 +111,148 @@ impl ScheduleJob {
} }
} }
#[instrument(skip(self, http), level = "info")] #[instrument(skip(self, http, manager), level = "info")]
pub async fn add_trivial_cron_job( pub async fn add_day_end_trivial_cron_job(
&mut self, &mut self,
server_id: u64, guild_id: u64,
channel_id: u64, channel_id: u64,
cron_expression: String, cron_expression: String,
manager: Arc<PoolManager>,
http: &Http, http: &Http,
) -> Result<Uuid, ()> { ) -> Result<Uuid, ()> {
let _http = Arc::new(Http::new(http.token())); let http = Arc::new(Http::new(http.token()));
// Create a new job with the provided cron expression // Create a new job with the provided cron expression
Err(()) let job = match Job::new_cron_job_async_tz(
cron_expression.as_str(),
Europe::Paris,
move |id, _l| {
Box::pin(
{
let http = http.clone();
let manager = manager.clone();
Box::pin(async move {
info!(
id = id.to_string(),
guild_id = guild_id,
channel_id = channel_id,
"Cron job triggered for guild: {}, channel: {}",
guild_id,
channel_id
);
// Here you can add the logic to be executed when the cron job is triggered
// For example, sending a message to the channel
use poise::serenity_prelude::{ChannelId, CreateMessage};
let search_query = Trivial::build_select_query(
Some(&format!(
"channel_id = {} and guild_id = {}",
channel_id, guild_id
)),
None,
None,
);
let trivial_exists: Trivial = match manager
.execute_select_with_retry::<Trivial>(&search_query).await{
Ok(trivia) => {
if trivia.is_empty() {
info!("No trivia game found in this channel.");
return;
}
if trivia.len() > 1 {
error!("Multiple trivia games found in this channel, using the first one.");
}
trivia[0].clone()
}
Err(e) => {
error!("Error fetching trivia game: {:?}", e);
return;
}
};
debug!("Trivial game found: {:?}", trivial_exists);
if trivial_exists.status != TrivialStatus::Started {
info!("Trivia game is not started, skipping message.");
return;
}
let trivia_round_query = TrivialRound::build_select_query(
Some(&format!(
"trivial_id = '{}' and finished = false",
trivial_exists.id
)),
None,
None,
);
let trivia_round: TrivialRound = match manager
.execute_select_with_retry::<TrivialRound>(&trivia_round_query)
.await{
Ok(rounds) => {
if rounds.is_empty() {
info!("No trivia rounds found for the current game.");
return;
}
if rounds.len() > 1 {
error!("Multiple trivia rounds found, using the first one.");
}
rounds[0].clone()
},
Err(e) => {
error!("Error fetching trivia rounds: {:?}", e);
return;
}
};
debug!("Trivial round found: {:?}", trivia_round.id);
// TODO : Send a message to end the trivia round and that the bot is processing the results
// TODO : Add score to the user
// TODO : Update the trivial round to finished
// TODO : Congratulate the winner and delete te processing message
// TODO : Send message see you soon
if let Err(e) = ChannelId::new(channel_id)
.send_message(
&http,
CreateMessage::new().content("Cron job executed"),
)
.await
{
error!("Failed to send message: {:?}", e);
}
})
}
.instrument(info_span!(
"trivial_cron_job",
id = id.to_string(),
guild_id = guild_id,
channel_id = channel_id,
)),
)
},
) {
Ok(job) => job,
Err(e) => {
error!("Error creating cron job: {:?}", e);
return Err(());
}
};
self.inject_job(job, (guild_id, channel_id)).await
} }
#[instrument(skip(self, job), level = "info")]
pub async fn inject_job(&mut self, job: JobLocked, id: (u64, u64)) -> Result<Uuid, ()> {
match self.scheduler.add(job.clone()).await {
Ok(job_uuid) => {
info!(id = job_uuid.to_string(), "Cron job added successfully");
{
let mut job_id = self.job_id.write().await;
job_id.insert(id, job_uuid);
}
Ok(job_uuid)
}
Err(e) => {
error!("Error adding cron job: {:?}", e);
return Err(());
}
}
}
#[instrument(skip(self), level = "info")] #[instrument(skip(self), level = "info")]
pub async fn stop_scheduled_job( pub async fn stop_scheduled_job(
&mut self, &mut self,