discord-kagebot/src/main/kotlin/moe/kageru/kagebot/MessageActions.kt

42 lines
1.5 KiB
Kotlin
Raw Normal View History

2019-06-09 18:41:51 +02:00
package moe.kageru.kagebot
import moe.kageru.kagebot.Config.Companion.config
import moe.kageru.kagebot.Log.log
import moe.kageru.kagebot.Util.ifNotEmpty
import org.javacord.api.event.message.MessageCreateEvent
class MessageActions(private val delete: Boolean, private val redirect: Redirect?) {
fun run(message: MessageCreateEvent, command: Command) {
if (delete && message.message.canYouDelete()) {
message.deleteMessage()
}
redirect?.execute(message, command)
}
}
class Redirect(private val target: Long, private val anonymous: Boolean) {
fun execute(message: MessageCreateEvent, command: Command) {
val embed = MessageUtil.getEmbedBuilder()
.addField(
config.localization.redirectedMessage,
message.readableMessageContent.let { content ->
when (command.matchType) {
2019-06-10 08:18:44 +02:00
MatchType.PREFIX -> content.removePrefix(command.trigger).trim()
2019-06-09 18:41:51 +02:00
else -> content
}
}
)
// 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)
}
}
Config.server!!.getTextChannelById(target).ifNotEmpty { it.sendMessage(embed) }
?: log.warning("Could not redirect message to channel $target")
}
}