Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Commit

Permalink
Fix: emoji code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFruxz committed Dec 7, 2021
1 parent 9697c25 commit 04ef7a1
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions JET-JavaCord/src/main/kotlin/de/jet/javacord/extension/Emoji.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.jet.javacord.extension

import de.jet.jvm.extension.ifNotNull
import de.jet.jvm.extension.ifNull
import de.jet.jvm.extension.isNotNull
import org.javacord.api.entity.emoji.CustomEmojiBuilder
import org.javacord.api.entity.emoji.KnownCustomEmoji
Expand All @@ -14,7 +16,7 @@ import org.javacord.api.entity.server.Server
* @since 1.0
*/
fun Server.isCustomEmojiExisting(emojiName: String, ignoreCase: Boolean = false) =
getCustomEmoji(emojiName, ignoreCase).isNotNull
getCustomEmoji(emojiName, ignoreCase).isNotNull

/**
* Returns if a emoji with the [id] exists / stored at the server.
Expand All @@ -24,7 +26,7 @@ fun Server.isCustomEmojiExisting(emojiName: String, ignoreCase: Boolean = false)
* @since 1.0
*/
fun Server.isCustomEmojiExisting(id: Long) =
getCustomEmoji(id).isNotNull
getCustomEmoji(id).isNotNull

/**
* Returns the [KnownCustomEmoji] with the given name or null if it does not exist.
Expand All @@ -36,7 +38,7 @@ fun Server.isCustomEmojiExisting(id: Long) =
* @since 1.0
*/
fun Server.getCustomEmoji(emojiName: String, ignoreCase: Boolean = false) =
(if (ignoreCase) getCustomEmojisByNameIgnoreCase(emojiName) else getCustomEmojisByName(emojiName)).firstOrNull()
(if (ignoreCase) getCustomEmojisByNameIgnoreCase(emojiName).filterNotNull() else getCustomEmojisByName(emojiName).filterNotNull()).firstOrNull()

/**
* Returns the [KnownCustomEmoji] with the given [id] or nul if no emoji with the given [id] exists.
Expand All @@ -46,9 +48,9 @@ fun Server.getCustomEmoji(emojiName: String, ignoreCase: Boolean = false) =
* @since 1.0
*/
fun Server.getCustomEmoji(id: Long) = try {
getCustomEmojiById(id).get()
getCustomEmojiById(id).get()
} catch (exception: NoSuchElementException) {
null
null
}

/**
Expand All @@ -63,15 +65,20 @@ fun Server.getCustomEmoji(id: Long) = try {
* @author Fruxz
* @since 1.0
*/
fun Server.createCustomEmoji(emojiName: String, resource: ByteArray, replaceExisting: Boolean = false, process: CustomEmojiBuilder.() -> Unit = {}) = if (replaceExisting || !isCustomEmojiExisting(emojiName)) {
CustomEmojiBuilder(this)
.setName(emojiName)
.setImage(resource)
.apply(process)
.create()
.join()
fun Server.createCustomEmoji(
emojiName: String,
resource: ByteArray,
replaceExisting: Boolean = false,
process: CustomEmojiBuilder.() -> Unit = {}
) = if (replaceExisting || !isCustomEmojiExisting(emojiName)) {
CustomEmojiBuilder(this)
.setName(emojiName)
.setImage(resource)
.apply(process)
.create()
.join()
} else
getCustomEmoji(emojiName, false)
getCustomEmoji(emojiName)

/**
* Creates a custom emoji using the [CustomEmojiBuilder] and returns the created emoji. If a emoji with the name [emojiName]
Expand All @@ -83,15 +90,19 @@ fun Server.createCustomEmoji(emojiName: String, resource: ByteArray, replaceExis
* @author Fruxz
* @since 1.0
*/
fun Server.createCustomEmojiIfNotExists(emojiName: String, resource: ByteArray, process: CustomEmojiBuilder.() -> Unit = {}): KnownCustomEmoji = if (!isCustomEmojiExisting(emojiName)) {
CustomEmojiBuilder(this)
.setName(emojiName)
.setImage(resource)
.apply(process)
.create()
.join()
} else
getCustomEmoji(emojiName, false)!!
fun Server.createCustomEmojiIfNotExists(
emojiName: String,
resource: ByteArray,
process: CustomEmojiBuilder.() -> Unit = {}
): KnownCustomEmoji = with(getCustomEmoji(emojiName)) {
return@with this
?: CustomEmojiBuilder(this@createCustomEmojiIfNotExists)
.setName(emojiName)
.setImage(resource)
.apply(process)
.create()
.join()
}

/**
* Removes every custom emoji from the server, where the condition is returning true.
Expand All @@ -102,9 +113,9 @@ fun Server.createCustomEmojiIfNotExists(emojiName: String, resource: ByteArray,
* @since 1.0
*/
fun Server.removeCustomEmoji(condition: (KnownCustomEmoji) -> Boolean) = customEmojis.forEach {
if (condition(it)) {
it.delete().join()
}
if (condition(it)) {
it.delete().join()
}
}

/**
Expand Down

0 comments on commit 04ef7a1

Please sign in to comment.