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.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) {

View File

@ -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) });
}

View File

@ -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")
}

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 <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) } }
}
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) } }
}

View File

@ -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<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.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)
)
}
}