OttoBot/src/main/kotlin/nl/voidcorp/dbot/Util.kt

29 lines
797 B
Kotlin
Raw Normal View History

2018-10-10 23:54:01 +02:00
package nl.voidcorp.dbot
2018-10-15 22:25:51 +02:00
import com.google.gson.Gson
2018-10-10 23:54:01 +02:00
import com.google.gson.GsonBuilder
2018-10-16 13:33:15 +02:00
import com.jagrosh.jdautilities.command.Command
2018-10-10 23:54:01 +02:00
import java.util.*
2018-10-15 22:25:51 +02:00
val gson: Gson = GsonBuilder().setPrettyPrinting().create()
2018-10-10 23:54:01 +02:00
2018-10-15 22:25:51 +02:00
val random = Random()
fun <E> MutableList<E>.addAll(vararg e: E) = this.addAll(listOf(*e))
2018-10-16 13:33:15 +02:00
fun <E> List<E>.random(): E = this[random.nextInt(this.size)]
fun List<Command>.catMap(): Map<String, List<Command>> {
val m = mutableMapOf<String, MutableList<Command>>()
for (c in this) {
2018-10-25 13:47:54 +02:00
val cname = if (c.category == null) "Unknown" else c.category.name
2018-10-16 13:33:15 +02:00
if (!m.containsKey(cname)) {
m[cname] = mutableListOf()
}
2018-10-25 13:47:54 +02:00
m[cname]!! += c
2018-10-16 13:33:15 +02:00
}
return m
2018-10-25 13:47:54 +02:00
}
fun String.emptyOr(other: String): String = if (this.isEmpty()) other else this