Reduce complexity of redirect text handling

This commit is contained in:
kageru 2019-07-17 22:47:47 +02:00
parent 691bc408c0
commit c400ab7369
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 9 additions and 12 deletions

View File

@ -14,8 +14,8 @@ import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionException
object Util {
inline fun <T> T.doIf(condition: (T) -> Boolean, op: (T) -> T): T {
return if (condition(this)) op(this) else this
inline fun <T> T.applyIf(condition: Boolean, op: (T) -> T): T {
return if (condition) op(this) else this
}
/**

View File

@ -3,7 +3,7 @@ package moe.kageru.kagebot.command
import moe.kageru.kagebot.Globals
import moe.kageru.kagebot.Log
import moe.kageru.kagebot.MessageUtil
import moe.kageru.kagebot.Util.doIf
import moe.kageru.kagebot.Util.applyIf
import moe.kageru.kagebot.config.Config
import moe.kageru.kagebot.config.RawCommand
import moe.kageru.kagebot.features.MessageFeature
@ -61,7 +61,7 @@ class Command(cmd: RawCommand) {
fun matches(msg: String) = this.matchType.matches(msg, this)
private fun respond(author: MessageAuthor, response: String) = response.doIf({ it.contains(AUTHOR_PLACEHOLDER) }) {
private fun respond(author: MessageAuthor, response: String) = response.applyIf(response.contains(AUTHOR_PLACEHOLDER)) {
it.replace(AUTHOR_PLACEHOLDER, MessageUtil.mention(author))
}
}

View File

@ -3,6 +3,7 @@ package moe.kageru.kagebot.command
import moe.kageru.kagebot.Log
import moe.kageru.kagebot.MessageUtil
import moe.kageru.kagebot.Util
import moe.kageru.kagebot.Util.applyIf
import moe.kageru.kagebot.config.Config
import moe.kageru.kagebot.config.RawRedirect
import org.javacord.api.entity.channel.TextChannel
@ -15,15 +16,11 @@ internal class MessageRedirect(rawRedirect: RawRedirect) {
fun execute(message: MessageCreateEvent, command: Command) {
val embed = MessageUtil.withEmbed {
addField(
Config.localization.redirectedMessage,
message.readableMessageContent.let { content ->
when (command.matchType) {
MatchType.PREFIX -> content.removePrefix(command.trigger).trim()
else -> content
}
val redirectedText = message.readableMessageContent
.applyIf(command.matchType == MatchType.PREFIX) { content ->
content.removePrefix(command.trigger).trim()
}
)
addField(Config.localization.redirectedMessage, redirectedText)
}
// No inlined if/else because the types are different.
// Passing the full message author will also include the avatar in the embed.