From 6ec3c8e4cb19de0730302833250d62a87a3c195a Mon Sep 17 00:00:00 2001 From: jakubmanczak Date: Sat, 21 Jun 2025 00:23:25 +0200 Subject: [PATCH] add status automation service, unify event constituent update calls --- src/discordbot/events/service.rs | 26 ++++++++++-------------- src/discordbot/mod.rs | 2 ++ src/discordbot/status/list.rs | 34 ++++++++++++++++++++++++++++++++ src/discordbot/status/mod.rs | 29 +++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 src/discordbot/status/list.rs create mode 100644 src/discordbot/status/mod.rs diff --git a/src/discordbot/events/service.rs b/src/discordbot/events/service.rs index 1e7fd2d..30bc0fa 100644 --- a/src/discordbot/events/service.rs +++ b/src/discordbot/events/service.rs @@ -1,6 +1,6 @@ use chrono::{Datelike, Duration, TimeZone, Utc}; use chrono_tz::Europe::Warsaw; -use serenity::all::{Context, CreateAttachment, EditGuild, EditProfile, Guild, GuildId}; +use serenity::all::{Context, CreateAttachment, EditGuild, EditProfile, GuildId}; use tracing::{error, info, warn}; use super::Event; @@ -16,15 +16,7 @@ pub async fn run_event_service(ctx: Context, guild_id: GuildId) { } async fn update_event_constituents(ctx: &Context, guild_id: GuildId) -> Result<(), String> { - let mut guild = match Guild::get(&ctx.http, guild_id).await { - Ok(g) => g, - Err(e) => { - error!("Could not get guild info: {e}"); - return Err("Could not get guild info: {e}".to_string()); - } - }; let event = Event::get_current(); - let icon = match CreateAttachment::path(event.icon()).await { Ok(i) => i, Err(e) => { @@ -33,17 +25,19 @@ async fn update_event_constituents(ctx: &Context, guild_id: GuildId) -> Result<( } }; - match guild - .edit( - &ctx.http, - EditGuild::new().name(event.guild_name()).icon(Some(&icon)), + match ctx + .http + .edit_guild( + guild_id, + &EditGuild::new().name(&event.guild_name()).icon(Some(&icon)), + None, ) .await { - Ok(_) => info!("Guild name/icon updated."), + Ok(_) => info!("Guild updated."), Err(e) => { - error!("Could not update guild name/icon: {e}"); - return Err("Could not update guild name/icon: {e}".to_string()); + error!("Could not update guild: {e}"); + return Err("Could not update guild: {e}".to_string()); } }; diff --git a/src/discordbot/mod.rs b/src/discordbot/mod.rs index b2160b3..f31129f 100644 --- a/src/discordbot/mod.rs +++ b/src/discordbot/mod.rs @@ -12,6 +12,7 @@ struct Handler; mod commands; mod events; +mod status; #[async_trait] impl EventHandler for Handler { @@ -34,6 +35,7 @@ impl EventHandler for Handler { Err(why) => info!("Failed to register commands on the guild: {why:?}"), }; + status::init_service(&ctx); events::init_service(&ctx, &guild_id); } diff --git a/src/discordbot/status/list.rs b/src/discordbot/status/list.rs new file mode 100644 index 0000000..1c2b812 --- /dev/null +++ b/src/discordbot/status/list.rs @@ -0,0 +1,34 @@ +use serenity::all::ActivityData; +use std::sync::LazyLock; + +pub static LIST: LazyLock> = LazyLock::new(|| { + use ActivityData as ACT; + vec![ + ACT::playing("Team Fortress 2"), + ACT::playing("Minecraft"), + ACT::playing("PGTF Dating Sim"), + ACT::playing("Hades"), + ACT::playing("Bloons TD 6"), + // // // // // // // // // // // + ACT::listening("Lin-Manuel Miranda"), + ACT::listening("Kendrick Lamar"), + ACT::listening("Pięć Dwa Dębiec"), + ACT::listening("Gimpson"), + // // // // // // // // // // // + ACT::watching("Scooby Doo"), + ACT::watching("Horimiya"), + ACT::watching("My Deer Friend Nokotan"), + ACT::watching("Lycoris Recoil"), + ACT::watching("Yuru Camp"), + ACT::watching("DARLING in the FRANXX"), + // // // // // // // // // // // + ACT::custom("Formalizuje stowarzyszenie"), + ACT::custom("Shipuje członków"), + ACT::custom("Spisuje cytaty"), + ACT::custom("Krzyczy na rozmówcę"), + ACT::custom("Montuje konwent"), + ACT::custom("Rozmontowuje konwent"), + ACT::custom("Głosi prelekcję"), + ACT::custom("Zmienia nicki innym"), + ] +}); diff --git a/src/discordbot/status/mod.rs b/src/discordbot/status/mod.rs new file mode 100644 index 0000000..1626c1f --- /dev/null +++ b/src/discordbot/status/mod.rs @@ -0,0 +1,29 @@ +use rand::{Rng, SeedableRng, seq::IndexedRandom}; +use serenity::all::Context; +use std::time::Duration; +use tokio::time::sleep; +use tracing::info; + +use crate::discordbot::status::list::LIST; + +mod list; + +pub fn init_service(ctx: &Context) { + info!("Initialising status automation service..."); + + let ctx = ctx.clone(); + tokio::spawn(async move { + run_service(ctx).await; + }); +} + +pub async fn run_service(ctx: Context) { + loop { + let mut rng = rand::rngs::SmallRng::from_rng(&mut rand::rng()); + let activity = (*LIST).choose(&mut rng).map(|a| a.to_owned()); + ctx.set_activity(activity); + + // sleep for randomly 15, 30 or 45 minutes between status changes + sleep(Duration::from_secs(60 * 15 * rng.random_range(1..=3))).await; + } +}