Skip to content

Commit

Permalink
Added core support for guild emoji behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
BartArys committed Jun 21, 2020
1 parent 079a262 commit 5bfbe6e
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 0.5.1

## Additions

* Added `GuildBehavior.createEmoji`
* Added `GuildEmoji.delete` and `GuildEmoji.edit`

## Fixes

* `DiscordInvite#targetUser` is now correctly nullable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import com.gitlab.kordlib.core.supplier.EntitySupplier
import com.gitlab.kordlib.core.supplier.EntitySupplyStrategy
import com.gitlab.kordlib.rest.builder.ban.BanCreateBuilder
import com.gitlab.kordlib.rest.builder.channel.*
import com.gitlab.kordlib.rest.builder.guild.EmojiCreateBuilder
import com.gitlab.kordlib.rest.builder.guild.EmojiModifyBuilder
import com.gitlab.kordlib.rest.builder.guild.GuildModifyBuilder
import com.gitlab.kordlib.rest.builder.role.RoleCreateBuilder
import com.gitlab.kordlib.rest.builder.role.RolePositionsModifyBuilder
Expand Down Expand Up @@ -330,6 +332,10 @@ suspend inline fun GuildBehavior.edit(builder: GuildModifyBuilder.() -> Unit): G
return Guild(data, kord)
}

suspend inline fun GuildBehavior.createEmoji(builder: EmojiCreateBuilder.() -> Unit) {
kord.rest.emoji.createEmoji(guildId = id.value, builder = builder)
}

/**
* Requests to create a new text channel.
*
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/kotlin/com/gitlab/kordlib/core/entity/GuildEmoji.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.gitlab.kordlib.core.cache.data.EmojiData
import com.gitlab.kordlib.core.supplier.EntitySupplier
import com.gitlab.kordlib.core.supplier.EntitySupplyStrategy
import com.gitlab.kordlib.core.toSnowflakeOrNull
import com.gitlab.kordlib.rest.builder.guild.EmojiModifyBuilder
import io.ktor.utils.io.bits.withMemory
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.filter
Expand Down Expand Up @@ -98,6 +100,24 @@ class GuildEmoji(
*/
val user: UserBehavior? get() = userId?.let { UserBehavior(it, kord) }

/**
* Requests to delete this emoji, with the given [reason].
*
* @throws RequestException if anything went wrong during the request.
*/
suspend fun delete(reason: String? = null) {
kord.rest.emoji.deleteEmoji(guildId = guildId.value, emojiId = id.value, reason = reason)
}

/**
* Requests to edit the emoji.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend inline fun edit(builder: EmojiModifyBuilder.() -> Unit) {
kord.rest.emoji.modifyEmoji(guildId = guildId.value, emojiId = id.value, builder = builder)
}

/**
* Requests to get the creator of the emoji as a [Member],
* returns null if the [Member] isn't present or [userId] is null.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.gitlab.kordlib.rest.builder.guild

import com.gitlab.kordlib.common.annotation.KordDsl
import com.gitlab.kordlib.common.entity.Snowflake
import com.gitlab.kordlib.rest.Image
import com.gitlab.kordlib.rest.builder.AuditRequestBuilder
import com.gitlab.kordlib.rest.json.request.EmojiCreateRequest
import com.gitlab.kordlib.rest.json.request.EmojiModifyRequest

@KordDsl
class EmojiCreateBuilder : AuditRequestBuilder<EmojiCreateRequest> {
override var reason: String? = null
lateinit var name: String
lateinit var image: Image
var roles: Set<Snowflake> = emptySet()

override fun toRequest(): EmojiCreateRequest = EmojiCreateRequest(
name = name,
image = image.dataUri,
roles = roles.map { it.value }
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gitlab.kordlib.rest.service

import com.gitlab.kordlib.rest.builder.guild.EmojiCreateBuilder
import com.gitlab.kordlib.rest.builder.guild.EmojiModifyBuilder
import com.gitlab.kordlib.rest.json.request.EmojiCreateRequest
import com.gitlab.kordlib.rest.json.request.EmojiModifyRequest
Expand All @@ -8,6 +9,16 @@ import com.gitlab.kordlib.rest.route.Route

class EmojiService(requestHandler: RequestHandler) : RestService(requestHandler) {


suspend inline fun createEmoji(guildId: String, builder: EmojiCreateBuilder.() -> Unit) = call(Route.GuildEmojiPost) {
keys[Route.GuildId] = guildId
val emoji = EmojiCreateBuilder().apply(builder)

body(EmojiCreateRequest.serializer(), emoji.toRequest())
emoji.reason?.let { header("X-Audit-Log-Reason", it) }
}

@Deprecated("use the inline builder instead", ReplaceWith("createEmoji(guildId) { }") ,level = DeprecationLevel.WARNING)
suspend fun createEmoji(guildId: String, emoji: EmojiCreateRequest, reason: String? = null) = call(Route.GuildEmojiPost) {
keys[Route.GuildId] = guildId
body(EmojiCreateRequest.serializer(), emoji)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gitlab.kordlib.rest.services

import com.gitlab.kordlib.common.entity.*
import com.gitlab.kordlib.rest.Image
import com.gitlab.kordlib.rest.json.request.*
import com.gitlab.kordlib.rest.ratelimit.ExclusionRequestRateLimiter
import com.gitlab.kordlib.rest.request.KtorRequestHandler
Expand All @@ -21,6 +22,14 @@ fun image(path: String): String {
return "data:image/$imageType;base64, $encoded"
}

fun imageBinary(path: String) : Image {
val loader = Unit::class.java.classLoader
val image = loader?.getResource(path)?.readBytes()
val imageType = path.split(".").last()
val format = Image.Format.fromContentType(imageType)
return Image.raw(image!!, format)
}

@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@EnabledIfEnvironmentVariable(named = "TARGET_BRANCH", matches = "master")
Expand Down Expand Up @@ -371,7 +380,11 @@ class RestServiceTest {
fun `emojis in guilds`() = runBlocking {
with(rest.emoji) {

val emoji = createEmoji(guildId, EmojiCreateRequest("kord", image("images/kord.png"), listOf(guildId)))
val emoji = createEmoji(guildId) {
name = "kord"
image = imageBinary("images/kord.png")
roles = setOf(Snowflake(guildId))
}

modifyEmoji(guildId, emoji.id!!) {
name = "edited"
Expand Down

0 comments on commit 5bfbe6e

Please sign in to comment.