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

42 lines
1.6 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
2019-07-17 23:17:16 +02:00
import moe.kageru.kagebot.Util.failed
import moe.kageru.kagebot.Util.unwrap
import moe.kageru.kagebot.config.Config
import moe.kageru.kagebot.config.LocalizationSpec
import org.javacord.api.entity.channel.TextChannel
import org.javacord.api.event.message.MessageCreateEvent
class MessageRedirect(target: String, private val anonymous: Boolean = false) {
private val targetChannel: TextChannel = Util.findChannel(target).unwrap()
fun execute(message: MessageCreateEvent, command: Command) {
2019-07-17 22:40:44 +02:00
val embed = MessageUtil.withEmbed {
val redirectedText = message.readableMessageContent
.applyIf(command.matchType == MatchType.PREFIX) { content ->
content.removePrefix(command.trigger).trim()
}
addField(Config.localization[LocalizationSpec.redirectedMessage], redirectedText)
Log.info("Redirected message: $redirectedText")
2019-07-17 22:40:44 +02:00
}
// 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)
}
}
if (MessageUtil.sendEmbed(targetChannel, embed).failed()) {
targetChannel.sendMessage("Error: could not redirect message.")
Log.warn("Could not redirect message to channel $targetChannel")
}
}
2019-07-13 14:52:05 +02:00
}