From 3d813384e23dde8d30f7db281f799fecfb2d9a3e Mon Sep 17 00:00:00 2001 From: kageru Date: Mon, 11 Nov 2019 23:52:14 +0100 Subject: [PATCH] Start migrating to Arrow Options --- src/main/kotlin/moe/kageru/kagebot/Util.kt | 11 +++++++--- .../kageru/kagebot/extensions/Extensions.kt | 20 +++++++++++++++++++ .../kotlin/moe/kageru/kagebot/TestUtil.kt | 7 ------- 3 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 src/main/kotlin/moe/kageru/kagebot/extensions/Extensions.kt diff --git a/src/main/kotlin/moe/kageru/kagebot/Util.kt b/src/main/kotlin/moe/kageru/kagebot/Util.kt index 814d8b6..36ab9a8 100644 --- a/src/main/kotlin/moe/kageru/kagebot/Util.kt +++ b/src/main/kotlin/moe/kageru/kagebot/Util.kt @@ -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): 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 Optional.asOption(): Option = if (this.isPresent) Option.just(this.get()) else Option.empty() + inline fun checked(op: (() -> Unit)) { try { op() diff --git a/src/main/kotlin/moe/kageru/kagebot/extensions/Extensions.kt b/src/main/kotlin/moe/kageru/kagebot/extensions/Extensions.kt new file mode 100644 index 0000000..f9a9215 --- /dev/null +++ b/src/main/kotlin/moe/kageru/kagebot/extensions/Extensions.kt @@ -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 = getTextChannelById(id).asOption() +fun Server.channelByName(name: String): ListK = getTextChannelsByName(name).k() +fun Server.rolesByName(name: String): ListK = getRolesByNameIgnoreCase(name).k() +fun Server.membersByName(name: String): ListK = getMembersByName(name).toList().k() +fun Server.categoriesByName(name: String): ListK = getChannelCategoriesByNameIgnoreCase(name).k() + +fun User.roles(): ListK = getRoles(Config.server).k() diff --git a/src/test/kotlin/moe/kageru/kagebot/TestUtil.kt b/src/test/kotlin/moe/kageru/kagebot/TestUtil.kt index f001189..e7405fe 100644 --- a/src/test/kotlin/moe/kageru/kagebot/TestUtil.kt +++ b/src/test/kotlin/moe/kageru/kagebot/TestUtil.kt @@ -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 = emptyList(), unexpected: List = emptyList(),