use chrono::TimeZone; use poise::serenity_prelude::*; use crate::{ManifoldContext, ManifoldData}; use chrono_tz::Tz; use crate::error::{ManifoldError, ManifoldResult}; use crate::models::user::UserInfo; #[poise::command(prefix_command, track_edits, slash_command)] async fn help( ctx: ManifoldContext<'_>, #[description = "Help about a specific command"] command: Option ) -> ManifoldResult<()> { let responses = &ctx.data().responses; let config = poise::builtins::HelpConfiguration { extra_text_at_bottom: &*format!("{}", responses.get_response(&"help footer".to_string()).unwrap_or(&"".to_string())), ..Default::default() }; poise::builtins::help(ctx, command.as_deref(), config).await?; Ok(()) } #[poise::command(slash_command, prefix_command)] async fn ping(ctx: ManifoldContext<'_>,) -> ManifoldResult<()> { let requestor = { let calling_user = ctx.author(); calling_user.mention().to_string() }; ctx.say(format!("{}: Ping? Pong!", requestor)).await?; Ok(()) } #[poise::command(slash_command, prefix_command)] async fn version(ctx: ManifoldContext<'_>) -> ManifoldResult<()> { let version_string = &ctx.data().version_string; ctx.send(|f| f .reply(true) .content(version_string)).await?; Ok(()) } #[poise::command(slash_command, prefix_command, aliases("st"))] async fn set_timezone(ctx: ManifoldContext<'_>, input_timezone: String) -> ManifoldResult<()> { let mut userinfo = ctx.data().user_info.lock().await; let validated_timezone: Tz = match input_timezone.parse() { Ok(r) => r, Err(e) => { error!("Problem parsing timezone input: {:?}", e); ctx.send(|f| f.content(format!("Are you having a laugh? What kind of timezone is that? {:?}", e)).reply(true)).await?; Err(e)? } }; if let Some(user) = userinfo.get_mut(ctx.author().id.as_u64()) { user.timezone = Some(validated_timezone.to_string()); ctx.send(|f| f.content(format!("I have set your timezone to {}, but we all know that UTC is the One True Timezone.", validated_timezone.to_string())).reply(true)).await?; } else { let new_user = UserInfo { user_id: ctx.author().id.as_u64().to_owned() as i64, username: ctx.author().name.to_owned(), weather_location: None, weather_units: None, timezone: Some(validated_timezone.to_string()), last_seen: Some(chrono::Utc::now().timestamp()) }; userinfo.insert(ctx.author().id.as_u64().clone(), new_user); ctx.send(|f| f.content(format!("I have set your timezone to {}, but we all know that UTC is the One True Timezone.", validated_timezone.to_string())).reply(true)).await?; } Ok(()) } #[poise::command(slash_command, prefix_command, aliases("t"))] pub async fn time(ctx: ManifoldContext<'_>, target: u64) -> ManifoldResult<()> { let userinfo = ctx.data().user_info.lock().await; let user = match userinfo.get(&target) { Some(u) => match u.timezone.to_owned() { Some(t) => u, None => {ctx.send(|f| f.content(format!("This user hasn't given me their timezone yet. Shame on them!")).reply(true)).await?; Err("NoTZ")?} }, None => { ctx.send(|f| f.content(format!("Who's that? I don't know them.")).reply(true)).await?; Err("User not found")?} }; let current_time_utc = chrono::Utc::now(); let user_timezone: Tz = user.timezone.as_ref().unwrap().parse::().unwrap(); let adjusted_time = user_timezone.from_utc_datetime(¤t_time_utc.naive_utc()); ctx.send(|f| f.content(format!("Time in {}'s current timezone of {} is {}", user.username, user_timezone.name().to_string(), adjusted_time.time().to_string())).reply(true)).await?; Ok(()) } pub fn commands() -> [poise::Command; 5] { [help(), ping(), version(), set_timezone(), time()] }