add status automation service, unify event constituent update calls

This commit is contained in:
2025-06-21 00:23:25 +02:00
parent 904ff5a213
commit 6ec3c8e4cb
4 changed files with 75 additions and 16 deletions

View File

@@ -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());
}
};

View File

@@ -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);
}

View File

@@ -0,0 +1,34 @@
use serenity::all::ActivityData;
use std::sync::LazyLock;
pub static LIST: LazyLock<Vec<ActivityData>> = 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"),
]
});

View File

@@ -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;
}
}