Skip to content

Commit

Permalink
Refactor time command to new patterns, also add Discord timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
NovaFox161 committed Mar 4, 2024
1 parent 3a15a74 commit f6dee00
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import discord4j.core.`object`.command.ApplicationCommandInteractionOption
import discord4j.core.`object`.command.ApplicationCommandInteractionOptionValue
import discord4j.core.`object`.entity.Message
import kotlinx.coroutines.reactor.awaitSingle
import kotlinx.coroutines.reactor.awaitSingleOrNull
import org.dreamexposure.discal.client.commands.SlashCommand
import org.dreamexposure.discal.client.message.embed.CalendarEmbed
import org.dreamexposure.discal.core.extensions.discord4j.followupEphemeral
import org.dreamexposure.discal.core.business.EmbedService
import org.dreamexposure.discal.core.extensions.discord4j.followup
import org.dreamexposure.discal.core.extensions.discord4j.getCalendar
import org.dreamexposure.discal.core.`object`.GuildSettings
import org.dreamexposure.discal.core.utils.getCommonMsg
import org.springframework.stereotype.Component

@Component
class TimeCommand : SlashCommand {
class TimeCommand(
private val embedService: EmbedService,
) : SlashCommand {
override val name = "time"
override val ephemeral = true

Expand All @@ -24,10 +28,14 @@ class TimeCommand : SlashCommand {
.map(Long::toInt)
.orElse(1)

return event.interaction.guild.flatMap { guild ->
CalendarEmbed.time(guild, settings, calendarNumber).flatMap {
event.followupEphemeral(it)
}
}.switchIfEmpty(event.followupEphemeral(getCommonMsg("error.notFound.calendar", settings))).awaitSingle()

val calendar = event.interaction.guild.flatMap {
it.getCalendar(calendarNumber)
}.awaitSingleOrNull()
if (calendar == null) {
return event.followup(getCommonMsg("error.notFound.calendar", settings)).awaitSingle()
}

return event.followup(embedService.calendarTimeEmbed(calendar, settings)).awaitSingle()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package org.dreamexposure.discal.client.message.embed
import discord4j.core.`object`.entity.Guild
import discord4j.core.spec.EmbedCreateSpec
import org.dreamexposure.discal.core.entities.Calendar
import org.dreamexposure.discal.core.enums.time.TimeFormat
import org.dreamexposure.discal.core.extensions.discord4j.getCalendar
import org.dreamexposure.discal.core.extensions.embedDescriptionSafe
import org.dreamexposure.discal.core.extensions.embedFieldSafe
import org.dreamexposure.discal.core.extensions.embedTitleSafe
Expand All @@ -13,9 +11,6 @@ import org.dreamexposure.discal.core.`object`.GuildSettings
import org.dreamexposure.discal.core.`object`.calendar.PreCalendar
import org.dreamexposure.discal.core.utils.GlobalVal.discalColor
import org.dreamexposure.discal.core.utils.getCommonMsg
import reactor.core.publisher.Mono
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

object CalendarEmbed : EmbedMaker {

Expand All @@ -38,32 +33,6 @@ object CalendarEmbed : EmbedMaker {
.build()
}

fun time(guild: Guild, settings: GuildSettings, calNumber: Int): Mono<EmbedCreateSpec> {
return guild.getCalendar(calNumber).map { cal ->
val ldt = LocalDateTime.now(cal.timezone)

val fmt: DateTimeFormatter =
if (settings.timeFormat == TimeFormat.TWELVE_HOUR)
DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss a")
else
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")


val correctTime = fmt.format(ldt)
val builder = defaultBuilder(guild, settings)

builder.title(getMessage("time", "embed.title", settings))
builder.addField(getMessage("time", "embed.field.current", settings), correctTime, false)
builder.addField(getMessage("time", "embed.field.timezone", settings), cal.zoneName, false)
builder.footer(getMessage("time", "embed.footer", settings), null)
builder.url(cal.link)

builder.color(discalColor)

builder.build()
}
}

fun pre(guild: Guild, settings: GuildSettings, preCal: PreCalendar): EmbedCreateSpec {
val builder = defaultBuilder(guild, settings)
.title(getMessage("calendar", "wizard.title", settings))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ class EmbedService(
.build()
}

suspend fun calendarTimeEmbed(calendar: Calendar, settings: GuildSettings): EmbedCreateSpec {
val formattedTime = Instant.now().humanReadableFullSimple(calendar.timezone, settings.timeFormat)
val formattedLocal = Instant.now().asDiscordTimestamp(DiscordTimestampFormat.SHORT_DATETIME)

return defaultEmbedBuilder(settings)
.title(getEmbedMessage("time", "embed.title", settings))
.addField(getEmbedMessage("time", "embed.field.current", settings), formattedTime, true)
.addField(getEmbedMessage("time", "embed.field.timezone", settings), calendar.zoneName, true)
.addField(getEmbedMessage("time", "embed.field.local", settings), formattedLocal, false)
.footer(getEmbedMessage("time", "embed.footer", settings), null)
.url(calendar.link)
.color(GlobalVal.discalColor)
.build()
}

/////////////////////////
////// RSVP Embeds //////
/////////////////////////
Expand Down Expand Up @@ -313,7 +328,7 @@ class EmbedService(
////// Announcement Embeds //////
/////////////////////////////////
suspend fun determineAnnouncementEmbed(announcement: Announcement, event: Event, settings: GuildSettings): EmbedCreateSpec {
return when(settings.announcementStyle) {
return when (settings.announcementStyle) {
AnnouncementStyle.FULL -> fullAnnouncementEmbed(announcement, event, settings)
AnnouncementStyle.SIMPLE -> simpleAnnouncementEmbed(announcement, event, settings)
AnnouncementStyle.EVENT -> eventAnnouncementEmbed(announcement, event, settings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,33 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

@Serializable(with = TimeFormatAsIntSerializer::class)
enum class TimeFormat(val value: Int = 1, val full: String, val date: String, val longDate: String, val time: String, val dayOfWeek: String) {
TWENTY_FOUR_HOUR(1, "LLLL dd yyyy '@' HH:mm", "yyyy/MM/dd", "dd LLLL yyyy", "HH:mm", "EEEE '-' dd LLLL yyyy"),
TWELVE_HOUR(2, "LLLL dd yyyy '@' hh:mm a", "yyyy/MM/dd", "dd LLLL yyyy", "hh:mm a", "EEEE '-' dd LLLL yyyy");
enum class TimeFormat(
val value: Int,
val fullSimple: String,
val full: String,
val date: String,
val longDate: String,
val time: String,
val dayOfWeek: String
) {
TWENTY_FOUR_HOUR(
value = 1,
fullSimple = "yyyy/MM/dd hh:mm:ss",
full = "LLLL dd yyyy '@' HH:mm",
date = "yyyy/MM/dd",
longDate = "dd LLLL yyyy",
time = "HH:mm",
dayOfWeek = "EEEE '-' dd LLLL yyyy"),
TWELVE_HOUR(
value = 2,
fullSimple = "yyyy/MM/dd hh:mm:ss a",
full = "LLLL dd yyyy '@' hh:mm a",
date = "yyyy/MM/dd",
longDate = "dd LLLL yyyy",
time = "hh:mm a",
dayOfWeek = "EEEE '-' dd LLLL yyyy");

companion object {
fun isValid(i: Int): Boolean {
return i == 1 || i == 2
}

fun fromValue(i: Int): TimeFormat {
return when (i) {
1 -> TWENTY_FOUR_HOUR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ fun Instant.humanReadableFull(timezone: ZoneId, format: TimeFormat): String {
return DateTimeFormatter.ofPattern(format.full).withZone(timezone).format(this)
}

fun Instant.humanReadableFullSimple(timezone: ZoneId, format: TimeFormat): String {
return DateTimeFormatter.ofPattern(format.fullSimple).withZone(timezone).format(this)
}

fun Instant.humanReadableDate(timezone: ZoneId, format: TimeFormat, long: Boolean = true, longDay: Boolean = false): String {
return if (long && longDay) DateTimeFormatter.ofPattern(format.dayOfWeek).withZone(timezone).format(this)
else if (long) DateTimeFormatter.ofPattern(format.longDate).withZone(timezone).format(this)
Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/i18n/embed/time.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
embed.title=Current Calendar Time
embed.field.current=Current Time
embed.field.timezone=Time Zone
embed.field.local=Current Time (local)
embed.footer=This is the current time for this calendar

0 comments on commit f6dee00

Please sign in to comment.