Skip to content

Commit

Permalink
Change ,history command to use Discord timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickster258 committed Oct 12, 2023
1 parent 9a821ba commit e039c54
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/main/kotlin/org/openredstone/chad/commands/Command.kt
Original file line number Diff line number Diff line change
Expand Up @@ -410,37 +410,30 @@ fun piklCommand(authorizedRoles: List<String>, discordServer: Server, discordApi
}

fun historyCommand(authorizedRoles: List<String>, sql: Sql) = command(authorizedRoles) {
fun timestampToString(timestamp: Int): String {
val instant = Instant.ofEpochSecond(timestamp.toLong())
val dateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC)
return dateTime.format(DateTimeFormatter.ISO_DATE_TIME)
}

data class HistoryContainer(val key: String, var count: Int, var lastRan: String)
data class HistoryContainer(val key: String, var count: Int, var lastRan: Int)

val time by optional()
reply(isPrivate = true) {
val historyMap = mutableMapOf<String, HistoryContainer>()
sql.getHistory().forEach {
val timestamp = timestampToString(it.time)
if (it.key in historyMap) {
historyMap[it.key]!!.apply {
this.count += 1
this.lastRan = timestamp
this.lastRan = it.time
}
// The sort order is time ASC,
// so newer runs will be later
// in the search result
} else {
historyMap[it.key] = HistoryContainer(it.key, 1, timestamp)
historyMap[it.key] = HistoryContainer(it.key, 1, it.time)
}
}
historyMap.values
.sortedWith(compareByDescending { it.count })
.take(10).joinToString(
prefix = "Query results less than $time seconds:\n", separator = "\n"
) {
"${it.count}: ${it.key}, last ran ${it.lastRan}"
"${it.count}: ${it.key}, last ran <t:${it.lastRan}:F>"
}
}
}
Expand Down

0 comments on commit e039c54

Please sign in to comment.