Skip to content

Commit

Permalink
Fix bulkDelete message aging
Browse files Browse the repository at this point in the history
  • Loading branch information
HopeBaron committed Sep 7, 2020
1 parent aef4399 commit 60938f9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 0.6.3

## Fixes

* Fixed GuildMessageChannelBehavior#bulkdelete incorrectly deleting messages older than 14 days


# 0.6.2

## Additions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -64,18 +67,15 @@ interface GuildMessageChannelBehavior : GuildChannelBehavior, MessageChannelBeha
* @throws [RestRequestException] if something went wrong during the request.
*/
suspend fun bulkDelete(messages: Iterable<Snowflake>) {
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) }
Expand All @@ -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
Expand Down

0 comments on commit 60938f9

Please sign in to comment.