Start migrating to Arrow Options
This commit is contained in:
parent
7eadb2d67d
commit
3d813384e2
|
@ -1,6 +1,9 @@
|
|||
package moe.kageru.kagebot
|
||||
|
||||
import arrow.core.Option
|
||||
import arrow.core.extensions.list.foldable.find
|
||||
import moe.kageru.kagebot.config.Config
|
||||
import moe.kageru.kagebot.extensions.*
|
||||
import moe.kageru.kagebot.config.Config.server
|
||||
import org.javacord.api.entity.channel.TextChannel
|
||||
import org.javacord.api.entity.message.MessageAuthor
|
||||
|
@ -26,9 +29,9 @@ object Util {
|
|||
if (this.isPresent) op(this.get()) else null
|
||||
|
||||
fun hasOneOf(messageAuthor: MessageAuthor, roles: Set<Role>): Boolean {
|
||||
return messageAuthor.asUser().ifNotEmpty { user ->
|
||||
user.getRoles(server).toSet().intersect(roles).isNotEmpty()
|
||||
} ?: false
|
||||
return messageAuthor.asUser().asOption().flatMap { user ->
|
||||
user.roles().find { it in roles }
|
||||
}.nonEmpty()
|
||||
}
|
||||
|
||||
private val channelIdRegex = Regex("\\d{18}")
|
||||
|
@ -101,6 +104,8 @@ object Util {
|
|||
}
|
||||
}
|
||||
|
||||
fun <T> Optional<T>.asOption(): Option<T> = if (this.isPresent) Option.just(this.get()) else Option.empty()
|
||||
|
||||
inline fun checked(op: (() -> Unit)) {
|
||||
try {
|
||||
op()
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package moe.kageru.kagebot.extensions
|
||||
|
||||
import arrow.core.ListK
|
||||
import arrow.core.Option
|
||||
import arrow.core.k
|
||||
import org.javacord.api.entity.channel.ServerTextChannel
|
||||
import moe.kageru.kagebot.Util.asOption
|
||||
import moe.kageru.kagebot.config.Config
|
||||
import org.javacord.api.entity.channel.ChannelCategory
|
||||
import org.javacord.api.entity.permission.Role
|
||||
import org.javacord.api.entity.server.Server
|
||||
import org.javacord.api.entity.user.User
|
||||
|
||||
fun Server.channelById(id: String): Option<ServerTextChannel> = getTextChannelById(id).asOption()
|
||||
fun Server.channelByName(name: String): ListK<ServerTextChannel> = getTextChannelsByName(name).k()
|
||||
fun Server.rolesByName(name: String): ListK<Role> = getRolesByNameIgnoreCase(name).k()
|
||||
fun Server.membersByName(name: String): ListK<User> = getMembersByName(name).toList().k()
|
||||
fun Server.categoriesByName(name: String): ListK<ChannelCategory> = getChannelCategoriesByNameIgnoreCase(name).k()
|
||||
|
||||
fun User.roles(): ListK<Role> = getRoles(Config.server).k()
|
|
@ -124,13 +124,6 @@ object TestUtil {
|
|||
Config.commandConfig = oldCmds
|
||||
}
|
||||
|
||||
fun withLocalization(config: String, test: (() -> Unit)) {
|
||||
val oldLoc = Config.localization
|
||||
Config.localization = Config.localeSpec.string(config)
|
||||
test()
|
||||
Config.localization = oldLoc
|
||||
}
|
||||
|
||||
fun withReplyContents(
|
||||
expected: List<String> = emptyList(),
|
||||
unexpected: List<String> = emptyList(),
|
||||
|
|
Loading…
Reference in New Issue
Block a user