Skip to content

Commit

Permalink
Behaviour improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteAlex committed Sep 24, 2021
1 parent a059a59 commit dc34d7b
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 10 deletions.
18 changes: 18 additions & 0 deletions core/src/main/kotlin/behavior/RoleBehavior.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.kord.core.behavior

import dev.kord.common.entity.Snowflake
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.cache.data.RoleData
import dev.kord.core.entity.KordEntity
import dev.kord.core.entity.Role
import dev.kord.core.entity.Strategizable
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.indexOfFirstOrNull
import dev.kord.core.sorted
import dev.kord.core.supplier.EntitySupplier
Expand Down Expand Up @@ -43,6 +45,22 @@ interface RoleBehavior : KordEntity, Strategizable {
else "<@&${id.asString}>"
}

/**
* Requests to get the this behavior as a [Role] if it's not an instance of a [Role].
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
suspend fun asRole(): Role = supplier.getRole(guildId, id)

/**
* Requests to get this behavior as a [Role] if its not an instance of a [Role],
* returns null if the user isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun asRoleOrNull(): Role? = supplier.getRoleOrNull(guildId, id)

/**
* Requests to change the [position] of this role.
*
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/kotlin/behavior/StageInstanceBehavior.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.kord.core.behavior

import dev.kord.common.entity.Snowflake
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.cache.data.StageInstanceData
import dev.kord.core.entity.KordEntity
import dev.kord.core.entity.StageInstance
import dev.kord.core.entity.Strategizable
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.rest.json.request.StageInstanceUpdateRequest
Expand All @@ -22,8 +24,22 @@ interface StageInstanceBehavior : KordEntity, Strategizable {
return StageInstance(data, kord, supplier)
}

/**
* Requests to get the this behavior as a [StageInstance] if it's not an instance of a [StageInstance].
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
suspend fun asStageInstance(): StageInstance = supplier.getStageInstance(channelId)

/**
* Requests to get this behavior as a [StageInstance] if its not an instance of a [StageInstance],
* returns null if the user isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun asStageInstanceOrNull(): StageInstance? = supplier.getStageInstanceOrNull(channelId)

override fun withStrategy(strategy: EntitySupplyStrategy<*>): StageInstanceBehavior =
StageInstanceBehavior(id, channelId, kord, strategy.supply(kord))
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/kotlin/entity/Guild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class Guild(
*/
val afkChannelId: Snowflake? get() = data.afkChannelId

override suspend fun asGuild(): Guild = this

override suspend fun asGuildOrNull(): Guild = this

val afkChannel: VoiceChannelBehavior?
get() = afkChannelId?.let { VoiceChannelBehavior(guildId = id, id = it, kord = kord) }

Expand Down
14 changes: 13 additions & 1 deletion core/src/main/kotlin/entity/Member.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import dev.kord.core.cache.data.UserData
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import kotlinx.coroutines.flow.*
import kotlinx.datetime.toInstant
import kotlinx.datetime.Instant
import kotlinx.datetime.toInstant
import java.util.*

/**
Expand Down Expand Up @@ -111,6 +111,18 @@ class Member(
override fun withStrategy(strategy: EntitySupplyStrategy<*>): Member =
Member(memberData, data, kord, strategy.supply(kord))

override suspend fun asUser(): User = this

override suspend fun asUserOrNull(): User = this

override suspend fun asMember(guildId: Snowflake): Member = this

override suspend fun asMember(): Member = this

override suspend fun asMemberOrNull(guildId: Snowflake): Member = this

override suspend fun asMemberOrNull(): Member = this


override fun hashCode(): Int = Objects.hash(id, guildId)

Expand Down
2 changes: 2 additions & 0 deletions core/src/main/kotlin/entity/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Message(
override val channelId: Snowflake
get() = data.channelId

override suspend fun asMessageOrNull(): Message = this

/**
* The files attached to this message.
*/
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/kotlin/entity/Role.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ data class Role(

val rawPosition: Int get() = data.position

override suspend fun asRole(): Role = this

override suspend fun asRoleOrNull(): Role = this

/**
* The tags of this role, if present.
*/
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/kotlin/entity/StageInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import dev.kord.core.cache.data.StageInstanceData
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy

class StageInstance(val data: StageInstanceData, override val kord: Kord, override val supplier: EntitySupplier) : StageInstanceBehavior {
class StageInstance(val data: StageInstanceData, override val kord: Kord, override val supplier: EntitySupplier) :
StageInstanceBehavior {
override val id: Snowflake get() = data.id
val guildId: Snowflake get() = data.guildId
override val channelId: Snowflake get() = data.channelId
Expand All @@ -16,4 +17,7 @@ class StageInstance(val data: StageInstanceData, override val kord: Kord, overri
override fun withStrategy(strategy: EntitySupplyStrategy<*>): StageInstanceBehavior =
StageInstance(data, kord, strategy.supply(kord))

override suspend fun asStageInstance(): StageInstance = this

override suspend fun asStageInstanceOrNull(): StageInstance = this
}
8 changes: 8 additions & 0 deletions core/src/main/kotlin/entity/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ open class User(
@DeprecatedSinceKord("0.7.0")
val flags: UserFlags? by ::publicFlags

override suspend fun asUser(): User {
return this
}

override suspend fun asUserOrNull(): User {
return this
}

/**
* The flags on a user's account, if present.
*/
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/kotlin/entity/channel/Category.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Category(

override suspend fun asChannel(): Category = this

override suspend fun asChannelOrNull(): Category = this

override fun compareTo(other: Entity): Int {
return super<TopGuildChannel>.compareTo(other)
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/kotlin/entity/channel/DmChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ data class DmChannel(
.map { supplier.getUserOrNull(it) }
.filterNotNull()

override suspend fun asChannel(): MessageChannel = this

override suspend fun asChannelOrNull(): MessageChannel = this

/**
* returns a new [DmChannel] with the given [strategy].
*/
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/kotlin/entity/channel/NewsChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class NewsChannel(
override val supplier: EntitySupplier = kord.defaultSupplier
) : CategorizableChannel, TopGuildMessageChannel, ThreadParentChannel, NewsChannelBehavior {

override suspend fun asChannel(): NewsChannel = this

override fun hashCode(): Int = Objects.hash(id, guildId)

override fun equals(other: Any?): Boolean = when (other) {
Expand All @@ -28,6 +26,10 @@ class NewsChannel(
else -> false
}

override suspend fun asChannel(): NewsChannel = this

override suspend fun asChannelOrNull(): NewsChannel = this

/**
* Returns a new [NewsChannel] with the given [strategy].
*/
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/kotlin/entity/channel/ResolvedChannel.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.kord.core.entity.channel

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.Permissions
import dev.kord.core.Kord
import dev.kord.core.cache.data.ChannelData
Expand All @@ -19,6 +18,10 @@ class ResolvedChannel(

val permissions: Permissions get() = data.permissions.value!!

override suspend fun asChannel(): Channel = this

override suspend fun asChannelOrNull(): Channel = this

override val supplier: EntitySupplier
get() = strategy.supply(kord)

Expand Down
3 changes: 2 additions & 1 deletion core/src/main/kotlin/entity/channel/StageVoiceChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import dev.kord.common.entity.optional.getOrThrow
import dev.kord.core.Kord
import dev.kord.core.behavior.channel.ChannelBehavior
import dev.kord.core.behavior.channel.GuildChannelBehavior
import dev.kord.core.behavior.channel.TopGuildChannelBehavior
import dev.kord.core.behavior.channel.StageChannelBehavior
import dev.kord.core.cache.data.ChannelData
import dev.kord.core.exception.GatewayNotFoundException
Expand Down Expand Up @@ -45,6 +44,8 @@ class StageChannel(

override suspend fun asChannel(): StageChannel = this

override suspend fun asChannelOrNull(): StageChannel = this

override fun hashCode(): Int = Objects.hash(id, guildId)

override fun equals(other: Any?): Boolean = when (other) {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/kotlin/entity/channel/StoreChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ data class StoreChannel(

override suspend fun asChannel(): StoreChannel = this

override suspend fun asChannelOrNull(): StoreChannel = this

/**
* Returns a new [StoreChannel] with the given [strategy].
*/
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/kotlin/entity/channel/TextChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class TextChannel(
override fun withStrategy(strategy: EntitySupplyStrategy<*>): TextChannel =
TextChannel(data, kord, strategy.supply(kord))

override suspend fun asChannel(): TextChannel = this

override suspend fun asChannelOrNull(): TextChannel = this

override fun hashCode(): Int = Objects.hash(id, guildId)

override fun equals(other: Any?): Boolean = when (other) {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/kotlin/entity/channel/VoiceChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class VoiceChannel(
VoiceChannel(data, kord, strategy.supply(kord))

override suspend fun asChannel(): VoiceChannel = this

override suspend fun asChannelOrNull(): VoiceChannel = this

override fun hashCode(): Int = Objects.hash(id, guildId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class NewsChannelThread(
) : ThreadChannel {


override suspend fun asChannel(): NewsChannelThread = super.asChannel() as NewsChannelThread
override suspend fun asChannel(): NewsChannelThread = this

override suspend fun asChannelOrNull(): NewsChannelThread? = super.asChannelOrNull() as? NewsChannelThread
override suspend fun asChannelOrNull(): NewsChannelThread? = this


override suspend fun getParent(): NewsChannel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class TextChannelThread(
override val guildId: Snowflake
get() = data.guildId.value!!

override suspend fun asChannel(): TextChannelThread = super.asChannel() as TextChannelThread
override suspend fun asChannel(): TextChannelThread = this

override suspend fun asChannelOrNull(): TextChannelThread? = super.asChannelOrNull() as? TextChannelThread
override suspend fun asChannelOrNull(): TextChannelThread = this

override fun withStrategy(strategy: EntitySupplyStrategy<*>): TextChannelThread {
return TextChannelThread(data, kord, strategy.supply(kord))
Expand Down

0 comments on commit dc34d7b

Please sign in to comment.