diff --git a/src/main/kotlin/moe/kageru/kagebot/MessageUtil.kt b/src/main/kotlin/moe/kageru/kagebot/MessageUtil.kt index b162377..d7e25dc 100644 --- a/src/main/kotlin/moe/kageru/kagebot/MessageUtil.kt +++ b/src/main/kotlin/moe/kageru/kagebot/MessageUtil.kt @@ -2,6 +2,7 @@ package moe.kageru.kagebot import moe.kageru.kagebot.Util.failed import moe.kageru.kagebot.config.Config +import moe.kageru.kagebot.Util.toPairs import org.javacord.api.entity.message.Message import org.javacord.api.entity.message.MessageAuthor import org.javacord.api.entity.message.Messageable @@ -54,7 +55,7 @@ object MessageUtil { throw IllegalStateException("Embed must have even number of content strings (title/content pairs)") } return withEmbed { - contents.zipWithNext().forEach { (heading, content) -> + contents.toPairs().forEach { (heading, content) -> addField(heading, content) } } diff --git a/src/main/kotlin/moe/kageru/kagebot/Util.kt b/src/main/kotlin/moe/kageru/kagebot/Util.kt index 6f946ef..626cdca 100644 --- a/src/main/kotlin/moe/kageru/kagebot/Util.kt +++ b/src/main/kotlin/moe/kageru/kagebot/Util.kt @@ -135,4 +135,15 @@ object Util { Config.server.getMemberById(id).orElse(null) } } + + /** + * Convert a list of elements to pairs, retaining order. + * The last element is dropped if the input size is odd. + * [1, 2, 3, 4, 5] -> [[1, 2], [3, 4]] + */ + fun Collection.toPairs(): List> = this.iterator().run { + (0 until size / 2).map { + Pair(next(), next()) + } + } }