Skip to content

Commit

Permalink
Add /timestamp command
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinegb committed Aug 16, 2024
1 parent 7b4f360 commit 7fa879f
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Join the [Goober Bot Dev](https://discord.gg/7v2aY2YzJU) Discord server to try t

## Commands

*Last updated Aug 11, 2024*
*Last updated Aug 15, 2024*

### Silly

Expand Down Expand Up @@ -53,8 +53,10 @@ Join the [Goober Bot Dev](https://discord.gg/7v2aY2YzJU) Discord server to try t
### Other

- `/anon <message>`
- `/debug <error|delete_config>`
- `/debug delete_config`
- `/debug error <kind>`
- `/rock_paper_scissors [user]`
- `/sponsors` 💖
- `/timestamp [year] [month] [day] [hour] [minute] [second] [timezone] [style]`
- `/updates`
- `/vote` ❤️
2 changes: 2 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod rock_paper_scissors;
mod silly;
mod sponsors;
mod strike;
mod timestamp;
mod updates;
#[cfg(not(debug_assertions))]
mod vote;
Expand All @@ -30,6 +31,7 @@ pub(super) use rock_paper_scissors::*;
pub(super) use silly::*;
pub(super) use sponsors::*;
pub(super) use strike::*;
pub(super) use timestamp::*;
pub(super) use updates::*;
#[cfg(not(debug_assertions))]
pub(super) use vote::*;
118 changes: 118 additions & 0 deletions src/commands/timestamp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Goober Bot, Discord bot
// Copyright (C) 2024 Valentine Briese
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use anyhow::{anyhow, Context as _};
use chrono::{Datelike, FixedOffset, TimeZone, Timelike, Utc};
use poise::{
command,
serenity_prelude::{FormattedTimestamp, FormattedTimestampStyle},
ChoiceParameter,
};

use crate::{emoji::*, error::UserError, Context, Error};

const SECONDS_PER_HOUR: i32 = 3600;

#[derive(ChoiceParameter)]
enum FormattedTimestampStyleChoice {
#[name = "Short time"]
ShortTime,
#[name = "Long time"]
LongTime,
#[name = "Short date"]
ShortDate,
#[name = "Long date"]
LongDate,
#[name = "Short date time"]
ShortDateTime,
#[name = "Long date time"]
LongDateTime,
#[name = "Relative time"]
RelativeTime,
}

impl From<FormattedTimestampStyleChoice> for FormattedTimestampStyle {
fn from(value: FormattedTimestampStyleChoice) -> Self {
match value {
FormattedTimestampStyleChoice::ShortTime => FormattedTimestampStyle::ShortTime,
FormattedTimestampStyleChoice::LongTime => FormattedTimestampStyle::LongTime,
FormattedTimestampStyleChoice::ShortDate => FormattedTimestampStyle::ShortDate,
FormattedTimestampStyleChoice::LongDate => FormattedTimestampStyle::LongDate,
FormattedTimestampStyleChoice::ShortDateTime => FormattedTimestampStyle::ShortDateTime,
FormattedTimestampStyleChoice::LongDateTime => FormattedTimestampStyle::LongDateTime,
FormattedTimestampStyleChoice::RelativeTime => FormattedTimestampStyle::RelativeTime,
}
}
}

/// Generates a Unix timestamp for your use in Discord styled messages
#[command(
slash_command,
install_context = "Guild|User",
interaction_context = "Guild|BotDm|PrivateChannel",
ephemeral
)]
pub(crate) async fn timestamp(
ctx: Context<'_>,
#[description = "Default is current year"]
// Where the heck did these big ol' numbers come from?
#[min = -262142]
#[max = 262141]
year: Option<i32>,
#[description = "Default is current month"]
#[min = 1]
#[max = 12]
month: Option<u32>,
#[description = "Default is current day"]
#[min = 1]
#[max = 31]
day: Option<u32>,
#[description = "Default is current hour"]
#[max = 23]
hour: Option<u32>,
#[description = "Default is current minute"]
#[max = 59]
minute: Option<u32>,
#[description = "Default is current second"]
#[max = 59]
second: Option<u32>,
#[description = "Offset from UTC (default is 0)"]
#[min = -12]
#[max = 14]
timezone: Option<i32>,
#[description = "Default is short date time"] style: Option<FormattedTimestampStyleChoice>,
) -> Result<(), Error> {
let timezone = timezone.unwrap_or(0);
let fixed_offset = FixedOffset::east_opt(timezone * SECONDS_PER_HOUR)
.context(UserError(anyhow!("Entered timezone difference is invalid")))?;
let now = Utc::now().with_timezone(&fixed_offset);
let year = year.unwrap_or_else(|| now.year());
let month = month.unwrap_or_else(|| now.month());
let day = day.unwrap_or_else(|| now.day());
let hour = hour.unwrap_or_else(|| now.hour());
let minute = minute.unwrap_or_else(|| now.minute());
let second = second.unwrap_or_else(|| now.second());
let datetime = fixed_offset
.with_ymd_and_hms(year, month, day, hour, minute, second)
.earliest()
.context(UserError(anyhow!("Entered date/time is invalid")))?;
let formatted_timestamp =
FormattedTimestamp::new(datetime.into(), style.and_then(|s| Some(s.into())));

ctx.say(format!("Copy this and use it anywhere that supports Discord formatting {FLOOF_HAPPY}\n```\n{formatted_timestamp}\n```\nLooks like this btw: {formatted_timestamp}\n*If this isn't the timestamp you expected, make sure you set `timezone` to your timezone!*")).await?;

Ok(())
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ async fn main(
commands::slap(),
commands::sponsors(),
commands::strike(),
commands::timestamp(),
commands::updates(),
#[cfg(not(debug_assertions))]
commands::vote(),
Expand Down

0 comments on commit 7fa879f

Please sign in to comment.