From 60938f97efe824ce6cf901d3b7bf298d712b4973 Mon Sep 17 00:00:00 2001 From: HopeBaron Date: Mon, 7 Sep 2020 12:12:10 +0300 Subject: [PATCH] Fix bulkDelete message aging --- CHANGELOG.md | 7 +++++++ .../channel/GuildMessageChannelBehavior.kt | 18 +++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ee5518b7a1..6c07fdae36e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# 0.6.3 + +## Fixes + +* Fixed GuildMessageChannelBehavior#bulkdelete incorrectly deleting messages older than 14 days + + # 0.6.2 ## Additions diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/channel/GuildMessageChannelBehavior.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/channel/GuildMessageChannelBehavior.kt index de5c092a47c..ffef7ef23a5 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/channel/GuildMessageChannelBehavior.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/channel/GuildMessageChannelBehavior.kt @@ -15,8 +15,11 @@ import com.gitlab.kordlib.rest.request.RestRequestException import com.gitlab.kordlib.rest.service.RestClient import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow +import java.time.Duration +import java.time.Instant import java.util.* import kotlin.time.days +import kotlin.time.toJavaDuration /** * The behavior of a Discord message channel associated to a [guild]. @@ -64,18 +67,15 @@ interface GuildMessageChannelBehavior : GuildChannelBehavior, MessageChannelBeha * @throws [RestRequestException] if something went wrong during the request. */ suspend fun bulkDelete(messages: Iterable) { + val daysLimit = Instant.now() - Duration.ofDays(14) //split up in bulk delete and manual delete // if message.timeMark + 14 days > now, then the message isn't 14 days old yet, and we can add it to the bulk delete // if message.timeMark + 14 days < now, then the message is more than 14 days old, and we'll have to manually delete them - val messagesByRemoval = messages.groupBy { it.timeMark.plus(14.days).hasPassedNow() } - val younger = messagesByRemoval[true].orEmpty() - val older = messagesByRemoval[false].orEmpty() + val (younger, older) = messages.partition { it.timeStamp.isAfter(daysLimit) } - when { - younger.size < 2 -> younger.forEach { kord.rest.channel.deleteMessage(id.value, it.value) } - else -> younger.map { it.value }.chunked(100) - .map { BulkDeleteRequest(it) } - .forEach { kord.rest.channel.bulkDelete(id.value, it) } + younger.map { it.value }.chunked(100).forEach { + if (it.size < 2) kord.rest.channel.deleteMessage(id.value, it.first()) + else kord.rest.channel.bulkDelete(id.value, BulkDeleteRequest(it)) } older.forEach { kord.rest.channel.deleteMessage(id.value, it.value) } @@ -95,7 +95,7 @@ interface GuildMessageChannelBehavior : GuildChannelBehavior, MessageChannelBeha override fun hashCode(): Int = Objects.hash(id, guildId) - override fun equals(other: Any?): Boolean = when(other) { + override fun equals(other: Any?): Boolean = when (other) { is GuildChannelBehavior -> other.id == id && other.guildId == guildId is ChannelBehavior -> other.id == id else -> false