diff --git a/src/main/kotlin/moe/kageru/kagebot/MessageUtil.kt b/src/main/kotlin/moe/kageru/kagebot/MessageUtil.kt index cb9ff76..943b1d6 100644 --- a/src/main/kotlin/moe/kageru/kagebot/MessageUtil.kt +++ b/src/main/kotlin/moe/kageru/kagebot/MessageUtil.kt @@ -6,24 +6,17 @@ import org.javacord.api.entity.message.Message import org.javacord.api.entity.message.MessageAuthor import org.javacord.api.entity.message.Messageable import org.javacord.api.entity.message.embed.EmbedBuilder -import org.javacord.api.entity.user.User import java.util.concurrent.CompletableFuture object MessageUtil { - fun mention(user: MessageAuthor): String { - return "<@${user.id}>" - } - - fun mention(user: User): String { - return "<@${user.id}>" - } + fun MessageAuthor.mention() = "<@$id>" fun withEmbed(op: EmbedBuilder.() -> Unit): EmbedBuilder { - val builder = EmbedBuilder() - Config.server.icon.ifPresent { builder.setThumbnail(it) } - builder.setColor(SystemSpec.color) - builder.op() - return builder + return EmbedBuilder().apply { + Config.server.icon.ifPresent { setThumbnail(it) } + setColor(SystemSpec.color) + op() + } } fun Messageable.sendEmbed(op: EmbedBuilder.() -> Unit) { diff --git a/src/main/kotlin/moe/kageru/kagebot/command/Command.kt b/src/main/kotlin/moe/kageru/kagebot/command/Command.kt index 179af74..4729812 100644 --- a/src/main/kotlin/moe/kageru/kagebot/command/Command.kt +++ b/src/main/kotlin/moe/kageru/kagebot/command/Command.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty import moe.kageru.kagebot.Globals import moe.kageru.kagebot.Log import moe.kageru.kagebot.MessageUtil -import moe.kageru.kagebot.Util.applyIf +import moe.kageru.kagebot.MessageUtil.mention import moe.kageru.kagebot.config.Config import moe.kageru.kagebot.features.MessageFeature import org.javacord.api.entity.message.MessageAuthor @@ -49,22 +49,12 @@ class Command( } private fun respond(author: MessageAuthor, response: String) = - response.applyIf(response.contains(AUTHOR_PLACEHOLDER)) { - it.replace(AUTHOR_PLACEHOLDER, MessageUtil.mention(author)) - } + response.replace(AUTHOR_PLACEHOLDER, author.mention()) } @Suppress("unused") -enum class MatchType { - PREFIX { - override fun matches(message: String, command: Command) = message.startsWith(command.trigger, ignoreCase = true) - }, - CONTAINS { - override fun matches(message: String, command: Command) = message.contains(command.trigger, ignoreCase = true) - }, - REGEX { - override fun matches(message: String, command: Command) = command.regex!!.matches(message) - }; - - abstract fun matches(message: String, command: Command): Boolean +enum class MatchType(val matches: (String, Command) -> Boolean) { + PREFIX({ message, command -> message.startsWith(command.trigger, ignoreCase = true) }), + CONTAINS({ message, command -> message.contains(command.trigger, ignoreCase = true) }), + REGEX({ message, command -> command.regex!!.matches(message) }); } diff --git a/src/main/kotlin/moe/kageru/kagebot/config/ConfigSpecs.kt b/src/main/kotlin/moe/kageru/kagebot/config/ConfigSpecs.kt index 27b0f23..676855b 100644 --- a/src/main/kotlin/moe/kageru/kagebot/config/ConfigSpecs.kt +++ b/src/main/kotlin/moe/kageru/kagebot/config/ConfigSpecs.kt @@ -23,5 +23,5 @@ object CommandSpec : ConfigSpec(prefix = "") { } object FeatureSpec : ConfigSpec(prefix = "") { - val features by optional(Features.DEFAULT, name = "feature") + val features by optional(Features(), name = "feature") } diff --git a/src/main/kotlin/moe/kageru/kagebot/extensions/ArrowExtensions.kt b/src/main/kotlin/moe/kageru/kagebot/extensions/ArrowExtensions.kt index fe3d43d..3e2ed52 100644 --- a/src/main/kotlin/moe/kageru/kagebot/extensions/ArrowExtensions.kt +++ b/src/main/kotlin/moe/kageru/kagebot/extensions/ArrowExtensions.kt @@ -13,10 +13,10 @@ fun Either.on(op: (R) -> Unit): Either { fun Either<*, T>.unwrap(): T = getOrElse { error("Attempted to unwrap Either.left") } -fun Tuple3.mapFirst(AP: Functor, op: (A) -> Kind) = let { (a, b, c) -> +inline fun Tuple3.mapFirst(AP: Functor, op: (A) -> Kind) = let { (a, b, c) -> AP.run { op(a).map { Tuple3(it, b, c) } } } -fun Tuple3.mapSecond(AP: Functor, op: (B) -> Kind) = let { (a, b, c) -> +inline fun Tuple3.mapSecond(AP: Functor, op: (B) -> Kind) = let { (a, b, c) -> AP.run { op(b).map { Tuple3(a, it, c) } } } diff --git a/src/main/kotlin/moe/kageru/kagebot/features/Features.kt b/src/main/kotlin/moe/kageru/kagebot/features/Features.kt index 9e38b7f..b11048d 100644 --- a/src/main/kotlin/moe/kageru/kagebot/features/Features.kt +++ b/src/main/kotlin/moe/kageru/kagebot/features/Features.kt @@ -1,6 +1,10 @@ package moe.kageru.kagebot.features -class Features(val welcome: WelcomeFeature?, val timeout: TimeoutFeature?, vc: TempVCFeature = TempVCFeature(null)) { +class Features( + val welcome: WelcomeFeature? = null, + val timeout: TimeoutFeature? = null, + vc: TempVCFeature = TempVCFeature(null) +) { private val debug = DebugFeature() private val help = HelpFeature() private val getConfig = GetConfigFeature() @@ -19,8 +23,4 @@ class Features(val welcome: WelcomeFeature?, val timeout: TimeoutFeature?, vc: T fun findByString(feature: String) = featureMap[feature] fun eventFeatures() = all.filterIsInstance() - - companion object { - val DEFAULT = Features(null, null) - } } diff --git a/src/main/kotlin/moe/kageru/kagebot/features/WelcomeFeature.kt b/src/main/kotlin/moe/kageru/kagebot/features/WelcomeFeature.kt index dc2eb7c..3adaaa7 100644 --- a/src/main/kotlin/moe/kageru/kagebot/features/WelcomeFeature.kt +++ b/src/main/kotlin/moe/kageru/kagebot/features/WelcomeFeature.kt @@ -3,8 +3,8 @@ package moe.kageru.kagebot.features import moe.kageru.kagebot.Log import moe.kageru.kagebot.MessageUtil import moe.kageru.kagebot.Util -import moe.kageru.kagebot.Util.checked import moe.kageru.kagebot.Util.asOption +import moe.kageru.kagebot.Util.checked import moe.kageru.kagebot.extensions.unwrap import org.javacord.api.DiscordApi import org.javacord.api.entity.channel.TextChannel @@ -31,7 +31,7 @@ class WelcomeFeature( // If the user disabled direct messages, try the fallback (if defined) if (message.asOption().isEmpty() && hasFallback()) { fallbackChannel!!.sendMessage( - fallbackMessage!!.replace("@@", MessageUtil.mention(event.user)) + fallbackMessage!!.replace("@@", event.user.mentionTag) ) } }