discord-kagebot/src/main/kotlin/moe/kageru/kagebot/Util.kt

23 lines
669 B
Kotlin
Raw Normal View History

2019-06-08 21:14:57 +02:00
package moe.kageru.kagebot
2019-06-10 09:19:03 +02:00
import org.javacord.api.entity.message.MessageAuthor
2019-06-09 18:41:51 +02:00
import java.util.*
2019-06-08 23:50:05 +02:00
2019-06-08 21:14:57 +02:00
object Util {
inline fun <T> T.doIf(condition: (T) -> Boolean, op: (T) -> T): T {
return if (condition(this)) op(this) else this
}
2019-06-08 23:50:05 +02:00
2019-06-09 18:41:51 +02:00
inline fun <T, R> Optional<T>.ifNotEmpty(op: (T) -> R): R? {
if (this.isPresent) {
return op(this.get())
}
return null
}
2019-06-10 09:19:03 +02:00
fun hasOneOf(messageAuthor: MessageAuthor, roles: Set<Long>): Boolean {
return messageAuthor.asUser().ifNotEmpty { user ->
user.getRoles(Config.server).map { it.id }.toSet().intersect(roles).isNotEmpty()
} ?: false
}
2019-06-08 21:14:57 +02:00
}