add status automation service, unify event constituent update calls
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
use chrono::{Datelike, Duration, TimeZone, Utc};
|
use chrono::{Datelike, Duration, TimeZone, Utc};
|
||||||
use chrono_tz::Europe::Warsaw;
|
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 tracing::{error, info, warn};
|
||||||
|
|
||||||
use super::Event;
|
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> {
|
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 event = Event::get_current();
|
||||||
|
|
||||||
let icon = match CreateAttachment::path(event.icon()).await {
|
let icon = match CreateAttachment::path(event.icon()).await {
|
||||||
Ok(i) => i,
|
Ok(i) => i,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -33,17 +25,19 @@ async fn update_event_constituents(ctx: &Context, guild_id: GuildId) -> Result<(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match guild
|
match ctx
|
||||||
.edit(
|
.http
|
||||||
&ctx.http,
|
.edit_guild(
|
||||||
EditGuild::new().name(event.guild_name()).icon(Some(&icon)),
|
guild_id,
|
||||||
|
&EditGuild::new().name(&event.guild_name()).icon(Some(&icon)),
|
||||||
|
None,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => info!("Guild name/icon updated."),
|
Ok(_) => info!("Guild updated."),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Could not update guild name/icon: {e}");
|
error!("Could not update guild: {e}");
|
||||||
return Err("Could not update guild name/icon: {e}".to_string());
|
return Err("Could not update guild: {e}".to_string());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ struct Handler;
|
|||||||
|
|
||||||
mod commands;
|
mod commands;
|
||||||
mod events;
|
mod events;
|
||||||
|
mod status;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl EventHandler for Handler {
|
impl EventHandler for Handler {
|
||||||
@@ -34,6 +35,7 @@ impl EventHandler for Handler {
|
|||||||
Err(why) => info!("Failed to register commands on the guild: {why:?}"),
|
Err(why) => info!("Failed to register commands on the guild: {why:?}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
status::init_service(&ctx);
|
||||||
events::init_service(&ctx, &guild_id);
|
events::init_service(&ctx, &guild_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
34
src/discordbot/status/list.rs
Normal file
34
src/discordbot/status/list.rs
Normal 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"),
|
||||||
|
]
|
||||||
|
});
|
||||||
29
src/discordbot/status/mod.rs
Normal file
29
src/discordbot/status/mod.rs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user