Skip to content

Commit

Permalink
Refactor AppEmojisLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Jan 11, 2025
1 parent e6079fc commit b027476
Showing 1 changed file with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,44 +135,27 @@ internal class AppEmojisLoader internal constructor(

val applicationEmojis = jda.retrieveApplicationEmojis().complete()

val missingRequests = arrayListOf<LoadRequest>()
toLoad.forEach { request ->
val appEmoji = applicationEmojis.find { it.name == request.emojiName }
if (appEmoji != null) {
loadedEmojis[request.identifier] = appEmoji
} else {
missingRequests += request
}
}

val missingRequests = getMissingEmojis(applicationEmojis)
if (missingRequests.isEmpty()) {
logger.debug { "Application emojis loaded, none were created" }
loaded = true
return
} else {
checkRemainingSlots(missingRequests, applicationEmojis)
uploadEmojis(missingRequests, jda)
logger.info { "Application emojis loaded, ${missingRequests.size} were created" }
}

checkRemainingSlots(missingRequests, applicationEmojis)

withScannedResources(packages) { scan ->
for ((basePath, assetPattern, emojiName, identifier) in missingRequests) {
// CG doesn't need / as root
val wildcardString = "${basePath.drop(1)}/$assetPattern"
val resources = scan.getResourcesMatchingWildcard(wildcardString)
requireThrowing(resources.isNotEmpty(), ::NoEmojiResourceException) {
"Found no resources for '$identifier', matching '$wildcardString'"
}
requireThrowing(resources.size == 1, ::NonUniqueEmojiResourceException) {
"Found multiple resources for '$identifier': ${resources.joinToString { it.pathRelativeToClasspathElement }}"
}
loaded = true
}

val icon = resources.single().open().use(Icon::from)
val applicationEmoji = jda.createApplicationEmoji(emojiName, icon).complete()
loadedEmojis.putIfAbsentOrThrowInternal(identifier, applicationEmoji)
private fun getMissingEmojis(applicationEmojis: MutableList<ApplicationEmoji>): List<LoadRequest> = buildList {
toLoad.forEach { request ->
val appEmoji = applicationEmojis.find { it.name == request.emojiName }
if (appEmoji != null) {
loadedEmojis[request.identifier] = appEmoji
} else {
add(request)
}
}

logger.info { "Application emojis loaded, ${missingRequests.size} were created" }
loaded = true
}

private fun checkRemainingSlots(missingRequests: List<LoadRequest>, applicationEmojis: List<ApplicationEmoji>) {
Expand Down Expand Up @@ -213,6 +196,26 @@ internal class AppEmojisLoader internal constructor(
.take(amountToDelete)
}

private fun uploadEmojis(missingRequests: List<LoadRequest>, jda: JDA) {
withScannedResources(packages) { scan ->
for ((basePath, assetPattern, emojiName, identifier) in missingRequests) {
// CG doesn't need / as root
val wildcardString = "${basePath.drop(1)}/$assetPattern"
val resources = scan.getResourcesMatchingWildcard(wildcardString)
requireThrowing(resources.isNotEmpty(), ::NoEmojiResourceException) {
"Found no resources for '$identifier', matching '$wildcardString'"
}
requireThrowing(resources.size == 1, ::NonUniqueEmojiResourceException) {
"Found multiple resources for '$identifier': ${resources.joinToString { it.pathRelativeToClasspathElement }}"
}

val icon = resources.single().open().use(Icon::from)
val applicationEmoji = jda.createApplicationEmoji(emojiName, icon).complete()
loadedEmojis.putIfAbsentOrThrowInternal(identifier, applicationEmoji)
}
}
}

private inline fun withScannedResources(packages: Collection<String>, action: (ScanResult) -> Unit) {
ClassGraph()
.acceptPackagesNonRecursive(*packages.toTypedArray())
Expand Down

0 comments on commit b027476

Please sign in to comment.