-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
176 additions
and
9 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
common/src/main/kotlin/com/gitlab/kordlib/common/entity/DiscordNull.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.gitlab.kordlib.common.entity | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
/** | ||
* Type to represent a Discord value that can only be null. This class cannot be instantiated. | ||
*/ | ||
@Serializable(with = DiscordNull.Serializer::class) | ||
class DiscordNull private constructor() { | ||
|
||
internal object Serializer : KSerializer<DiscordNull> { | ||
|
||
override val descriptor: SerialDescriptor | ||
get() = buildClassSerialDescriptor("Kord.DiscordNull") | ||
|
||
override fun deserialize(decoder: Decoder): DiscordNull { | ||
throw SerializationException("DiscordNull cannot have an instance.") | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: DiscordNull) { | ||
throw SerializationException("DiscordNull cannot be encoded.") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
core/src/main/kotlin/com/gitlab/kordlib/core/cache/data/RoleTagsData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.gitlab.kordlib.core.cache.data | ||
|
||
import com.gitlab.kordlib.common.entity.DiscordRoleTags | ||
import com.gitlab.kordlib.common.entity.optional.Optional | ||
import com.gitlab.kordlib.common.entity.optional.OptionalSnowflake | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RoleTagsData( | ||
val botId: OptionalSnowflake, | ||
val integrationId: OptionalSnowflake, | ||
val premiumSubscriber: Boolean, | ||
) { | ||
|
||
companion object { | ||
fun from(entity: DiscordRoleTags): RoleTagsData = with(entity) { | ||
RoleTagsData(botId, integrationId, premiumSubscriber is Optional.Null) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
core/src/main/kotlin/com/gitlab/kordlib/core/entity/RoleTags.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.gitlab.kordlib.core.entity | ||
|
||
import com.gitlab.kordlib.common.entity.Snowflake | ||
import com.gitlab.kordlib.core.Kord | ||
import com.gitlab.kordlib.core.KordObject | ||
import com.gitlab.kordlib.core.behavior.GuildBehavior | ||
import com.gitlab.kordlib.core.cache.data.IntegrationData | ||
import com.gitlab.kordlib.core.cache.data.RoleTagsData | ||
import com.gitlab.kordlib.core.supplier.EntitySupplier | ||
import com.gitlab.kordlib.common.exception.RequestException | ||
import com.gitlab.kordlib.core.exception.EntityNotFoundException | ||
import com.gitlab.kordlib.core.supplier.EntitySupplyStrategy | ||
|
||
class RoleTags( | ||
val data: RoleTagsData, | ||
val guildId: Snowflake, | ||
override val kord: Kord, | ||
override val supplier: EntitySupplier = kord.defaultSupplier, | ||
) : KordObject, Strategizable { | ||
|
||
/** | ||
* The ID of the bot this belongs to. | ||
*/ | ||
val botId: Snowflake? get() = data.botId.value | ||
|
||
/** | ||
* The ID of the of the [Integration] this role belongs to. | ||
*/ | ||
val integrationId: Snowflake? get() = data.integrationId.value | ||
|
||
/** | ||
* Whether this is the guild's premium subscriber role. | ||
*/ | ||
val isPremiumRole: Boolean get() = data.premiumSubscriber | ||
|
||
/** | ||
* The guild behavior this tag belongs to. | ||
*/ | ||
val guild: GuildBehavior get() = GuildBehavior(guildId, kord) | ||
|
||
/** | ||
* Requests to get the bot of this tag through the [supplier], | ||
* returns null if the [Member] isn't present. | ||
* | ||
* @throws [RequestException] if anything went wrong during the request. | ||
*/ | ||
suspend fun getBot(): Member? { | ||
val id = botId ?: return null | ||
return supplier.getMemberOrNull(guildId, id) | ||
} | ||
|
||
/** | ||
* Requests to get the integration of this tag through the [supplier], | ||
* returns null if the [Integration] isn't present. | ||
* | ||
* @throws [RequestException] if anything went wrong during the request. | ||
*/ | ||
suspend fun getIntegration(): Integration? { | ||
val id = integrationId ?: return null | ||
val response = kord.rest.guild.getGuildIntegrations(guildId) | ||
.firstOrNull { it.id == id } ?: return null | ||
|
||
return Integration(IntegrationData.from(guildId, response), kord) | ||
} | ||
|
||
/** | ||
* Requests to get the guild of this tag through the [supplier]. | ||
* | ||
* @throws [RequestException] if anything went wrong during the request. | ||
* @throws [EntityNotFoundException] if the [Guild] wasn't present. | ||
*/ | ||
suspend fun getGuild(): Guild = supplier.getGuild(guildId) | ||
|
||
/** | ||
* Requests to get the guild of this tag through the [supplier], | ||
* returns null if the [Guild] isn't present. | ||
* | ||
* @throws [RequestException] if anything went wrong during the request. | ||
*/ | ||
suspend fun getGuildOrNull(): Guild? = supplier.getGuildOrNull(guildId) | ||
|
||
override fun withStrategy(strategy: EntitySupplyStrategy<*>): Strategizable = | ||
RoleTags(data, guildId, kord, strategy.supply(kord)) | ||
|
||
} |