Simplifications

This commit is contained in:
kageru 2019-11-13 23:18:28 +01:00
parent f742383f38
commit cd9ee0e881
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
6 changed files with 22 additions and 39 deletions

View File

@ -6,24 +6,17 @@ import org.javacord.api.entity.message.Message
import org.javacord.api.entity.message.MessageAuthor import org.javacord.api.entity.message.MessageAuthor
import org.javacord.api.entity.message.Messageable import org.javacord.api.entity.message.Messageable
import org.javacord.api.entity.message.embed.EmbedBuilder import org.javacord.api.entity.message.embed.EmbedBuilder
import org.javacord.api.entity.user.User
import java.util.concurrent.CompletableFuture import java.util.concurrent.CompletableFuture
object MessageUtil { object MessageUtil {
fun mention(user: MessageAuthor): String { fun MessageAuthor.mention() = "<@$id>"
return "<@${user.id}>"
}
fun mention(user: User): String {
return "<@${user.id}>"
}
fun withEmbed(op: EmbedBuilder.() -> Unit): EmbedBuilder { fun withEmbed(op: EmbedBuilder.() -> Unit): EmbedBuilder {
val builder = EmbedBuilder() return EmbedBuilder().apply {
Config.server.icon.ifPresent { builder.setThumbnail(it) } Config.server.icon.ifPresent { setThumbnail(it) }
builder.setColor(SystemSpec.color) setColor(SystemSpec.color)
builder.op() op()
return builder }
} }
fun Messageable.sendEmbed(op: EmbedBuilder.() -> Unit) { fun Messageable.sendEmbed(op: EmbedBuilder.() -> Unit) {

View File

@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty
import moe.kageru.kagebot.Globals import moe.kageru.kagebot.Globals
import moe.kageru.kagebot.Log import moe.kageru.kagebot.Log
import moe.kageru.kagebot.MessageUtil 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.config.Config
import moe.kageru.kagebot.features.MessageFeature import moe.kageru.kagebot.features.MessageFeature
import org.javacord.api.entity.message.MessageAuthor import org.javacord.api.entity.message.MessageAuthor
@ -49,22 +49,12 @@ class Command(
} }
private fun respond(author: MessageAuthor, response: String) = private fun respond(author: MessageAuthor, response: String) =
response.applyIf(response.contains(AUTHOR_PLACEHOLDER)) { response.replace(AUTHOR_PLACEHOLDER, author.mention())
it.replace(AUTHOR_PLACEHOLDER, MessageUtil.mention(author))
}
} }
@Suppress("unused") @Suppress("unused")
enum class MatchType { enum class MatchType(val matches: (String, Command) -> Boolean) {
PREFIX { PREFIX({ message, command -> message.startsWith(command.trigger, ignoreCase = true) }),
override fun matches(message: String, command: Command) = message.startsWith(command.trigger, ignoreCase = true) CONTAINS({ message, command -> message.contains(command.trigger, ignoreCase = true) }),
}, REGEX({ message, command -> command.regex!!.matches(message) });
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
} }

View File

@ -23,5 +23,5 @@ object CommandSpec : ConfigSpec(prefix = "") {
} }
object FeatureSpec : ConfigSpec(prefix = "") { object FeatureSpec : ConfigSpec(prefix = "") {
val features by optional(Features.DEFAULT, name = "feature") val features by optional(Features(), name = "feature")
} }

View File

@ -13,10 +13,10 @@ fun <L, R> Either<L, R>.on(op: (R) -> Unit): Either<L, R> {
fun <T> Either<*, T>.unwrap(): T = getOrElse { error("Attempted to unwrap Either.left") } fun <T> Either<*, T>.unwrap(): T = getOrElse { error("Attempted to unwrap Either.left") }
fun <A, B, C, A2, F> Tuple3<A, B, C>.mapFirst(AP: Functor<F>, op: (A) -> Kind<F, A2>) = let { (a, b, c) -> inline fun <A, B, C, A2, F> Tuple3<A, B, C>.mapFirst(AP: Functor<F>, op: (A) -> Kind<F, A2>) = let { (a, b, c) ->
AP.run { op(a).map { Tuple3(it, b, c) } } AP.run { op(a).map { Tuple3(it, b, c) } }
} }
fun <A, B, C, B2, F> Tuple3<A, B, C>.mapSecond(AP: Functor<F>, op: (B) -> Kind<F, B2>) = let { (a, b, c) -> inline fun <A, B, C, B2, F> Tuple3<A, B, C>.mapSecond(AP: Functor<F>, op: (B) -> Kind<F, B2>) = let { (a, b, c) ->
AP.run { op(b).map { Tuple3(a, it, c) } } AP.run { op(b).map { Tuple3(a, it, c) } }
} }

View File

@ -1,6 +1,10 @@
package moe.kageru.kagebot.features 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 debug = DebugFeature()
private val help = HelpFeature() private val help = HelpFeature()
private val getConfig = GetConfigFeature() 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 findByString(feature: String) = featureMap[feature]
fun eventFeatures() = all.filterIsInstance<EventFeature>() fun eventFeatures() = all.filterIsInstance<EventFeature>()
companion object {
val DEFAULT = Features(null, null)
}
} }

View File

@ -3,8 +3,8 @@ package moe.kageru.kagebot.features
import moe.kageru.kagebot.Log import moe.kageru.kagebot.Log
import moe.kageru.kagebot.MessageUtil import moe.kageru.kagebot.MessageUtil
import moe.kageru.kagebot.Util import moe.kageru.kagebot.Util
import moe.kageru.kagebot.Util.checked
import moe.kageru.kagebot.Util.asOption import moe.kageru.kagebot.Util.asOption
import moe.kageru.kagebot.Util.checked
import moe.kageru.kagebot.extensions.unwrap import moe.kageru.kagebot.extensions.unwrap
import org.javacord.api.DiscordApi import org.javacord.api.DiscordApi
import org.javacord.api.entity.channel.TextChannel 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 the user disabled direct messages, try the fallback (if defined)
if (message.asOption().isEmpty() && hasFallback()) { if (message.asOption().isEmpty() && hasFallback()) {
fallbackChannel!!.sendMessage( fallbackChannel!!.sendMessage(
fallbackMessage!!.replace("@@", MessageUtil.mention(event.user)) fallbackMessage!!.replace("@@", event.user.mentionTag)
) )
} }
} }