discord-kagebot/src/main/kotlin/moe/kageru/kagebot/command/MessageRedirect.kt

41 lines
1.5 KiB
Kotlin
Raw Normal View History

package moe.kageru.kagebot.command
2019-07-17 21:16:17 +02:00
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.Util.asOption
import moe.kageru.kagebot.config.Config
import moe.kageru.kagebot.config.LocalizationSpec
2019-11-12 22:10:26 +01:00
import moe.kageru.kagebot.extensions.unwrap
import org.javacord.api.entity.channel.TextChannel
import org.javacord.api.event.message.MessageCreateEvent
class MessageRedirect(target: String, private val anonymous: Boolean = false) {
2019-11-14 15:10:30 +01:00
private val targetChannel: TextChannel = Util.findChannel(target).unwrap()
2019-11-14 15:10:30 +01:00
fun execute(message: MessageCreateEvent, command: Command) {
val embed = MessageUtil.withEmbed {
val redirectedText = message.readableMessageContent
.applyIf(command.matchType == MatchType.PREFIX) { content ->
content.removePrefix(command.trigger).trim()
}
2019-11-14 15:10:30 +01:00
addField(Config.localization[LocalizationSpec.redirectedMessage], redirectedText)
Log.info("Redirected message: $redirectedText")
}
// No inlined if/else because the types are different.
// Passing the full message author will also include the avatar in the embed.
embed.apply {
if (anonymous) {
setAuthor("Anonymous")
} else {
setAuthor(message.messageAuthor)
}
}
2019-11-14 15:10:30 +01:00
if (MessageUtil.sendEmbed(targetChannel, embed).asOption().isEmpty()) {
Log.warn("Could not redirect message to channel $targetChannel")
}
2019-11-14 15:10:30 +01:00
}
2019-07-13 14:52:05 +02:00
}