Skip to content

Commit

Permalink
Behaviour improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteAlex committed Sep 23, 2021
1 parent a059a59 commit aa0dce2
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 36 deletions.
29 changes: 17 additions & 12 deletions core/src/main/kotlin/behavior/GuildBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ interface GuildBehavior : KordEntity, Strategizable {
*/
val activeThreads: Flow<ThreadChannel>
get() = supplier.getActiveThreads(id)
get() = supplier.getActiveThreads(id)

/**
* Requests to get all present webhooks for this guild.
Expand Down Expand Up @@ -243,20 +243,28 @@ interface GuildBehavior : KordEntity, Strategizable {
supplier.getGuildApplicationCommandOrNull(kord.resources.applicationId, id, commandId)

/**
* Requests to get the this behavior as a [Guild].
* Requests to get the this behavior as a [Guild] if it's not an instance of a [Guild].
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the guild wasn't present.
*/
suspend fun asGuild(): Guild = supplier.getGuild(id)
suspend fun asGuild(): Guild = if (this is Guild) {
this
} else {
supplier.getGuild(id)
}

/**
* Requests to get this behavior as a [Guild],
* Requests to get this behavior as a [Guild] if it's not an instance of a [Guild],
* returns null if the guild isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun asGuildOrNull(): Guild? = supplier.getGuildOrNull(id)
suspend fun asGuildOrNull(): Guild? = if (this is Guild) {
this
} else {
supplier.getGuildOrNull(id)
}

/**
* Requests to delete this guild.
Expand Down Expand Up @@ -528,13 +536,13 @@ interface GuildBehavior : KordEntity, Strategizable {
override fun withStrategy(strategy: EntitySupplyStrategy<*>): GuildBehavior = GuildBehavior(id, kord, strategy)
}

suspend inline fun <reified T: GuildApplicationCommand> GuildBehavior.getApplicationCommandOfOrNull(commandId: Snowflake): T? {
return supplier.getGuildApplicationCommandOfOrNull(kord.resources.applicationId,id, commandId)
suspend inline fun <reified T : GuildApplicationCommand> GuildBehavior.getApplicationCommandOfOrNull(commandId: Snowflake): T? {
return supplier.getGuildApplicationCommandOfOrNull(kord.resources.applicationId, id, commandId)
}


suspend inline fun <reified T: GuildApplicationCommand> GuildBehavior.getApplicationCommandOf(commandId: Snowflake): T? {
return supplier.getGuildApplicationCommandOf(kord.resources.applicationId,id, commandId)
suspend inline fun <reified T : GuildApplicationCommand> GuildBehavior.getApplicationCommandOf(commandId: Snowflake): T? {
return supplier.getGuildApplicationCommandOf(kord.resources.applicationId, id, commandId)
}


Expand Down Expand Up @@ -573,8 +581,6 @@ suspend inline fun GuildBehavior.createChatInputCommand(
}




@OptIn(ExperimentalContracts::class)

suspend inline fun GuildBehavior.createMessageCommand(
Expand All @@ -596,7 +602,6 @@ suspend inline fun GuildBehavior.createUserCommand(
}



@OptIn(ExperimentalContracts::class)

suspend inline fun GuildBehavior.createApplicationCommands(
Expand Down
16 changes: 12 additions & 4 deletions core/src/main/kotlin/behavior/MemberBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,28 @@ interface MemberBehavior : KordEntity, UserBehavior {
val nicknameMention get() = "<@!${id.asString}>"

/**
* Requests to get the this behavior as a [Member].
* Requests to get the this behavior as a [Member] if it's not an instance of a [Member].
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the member wasn't present.
*/
suspend fun asMember(): Member = supplier.getMember(guildId, id)
suspend fun asMember(): Member = if (this is Member) {
this
} else {
supplier.getMember(guildId, id)
}

/**
* Requests to get this behavior as a [Member],
* Requests to get this behavior as a [Member] if it's not an instance of a [Member],
* returns null if the member isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun asMemberOrNull(): Member? = supplier.getMemberOrNull(guildId, id)
suspend fun asMemberOrNull(): Member? = if (this is Member) {
this
} else {
supplier.getMemberOrNull(guildId, id)
}

/**
* Requests to kick this member from its guild.
Expand Down
17 changes: 12 additions & 5 deletions core/src/main/kotlin/behavior/MessageBehavior.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.kord.core.behavior

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.Permission
import dev.kord.common.entity.Snowflake
import dev.kord.common.exception.RequestException
Expand Down Expand Up @@ -49,20 +48,28 @@ interface MessageBehavior : KordEntity, Strategizable {
suspend fun getChannelOrNull(): MessageChannel? = supplier.getChannelOfOrNull(channelId)

/**
* Requests to get the this behavior as a [Message].
* Requests to get the this behavior as a [Message] if it's not an instance of a [Message].
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the message wasn't present.
*/
suspend fun asMessage(): Message = supplier.getMessage(channelId = channelId, messageId = id)
suspend fun asMessage(): Message = if (this is Message) {
this
} else {
supplier.getMessage(channelId = channelId, messageId = id)
}

/**
* Requests to get this behavior as a [Message],
* Requests to get this behavior as a [Message] if it's not an instance of a [Message],
* returns null if the message isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun asMessageOrNull(): Message? = supplier.getMessageOrNull(channelId = channelId, messageId = id)
suspend fun asMessageOrNull(): Message? = if (this is Message) {
this
} else {
supplier.getMessageOrNull(channelId = channelId, messageId = id)
}


/**
Expand Down
26 changes: 26 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,30 @@ 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 = if (this is Role) {
this
} else {
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? = if (this is Role) {
this
} else {
supplier.getRoleOrNull(guildId, id)
}

/**
* Requests to change the [position] of this role.
*
Expand Down
26 changes: 25 additions & 1 deletion 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,7 +24,29 @@ interface StageInstanceBehavior : KordEntity, Strategizable {
return StageInstance(data, kord, supplier)
}

suspend fun asStageInstance(): StageInstance = supplier.getStageInstance(channelId)
/**
* 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 = if (this is StageInstance) {
this
} else {
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? = if (this is StageInstance) {
this
} else {
supplier.getStageInstanceOrNull(channelId)
}

override fun withStrategy(strategy: EntitySupplyStrategy<*>): StageInstanceBehavior =
StageInstanceBehavior(id, channelId, kord, strategy.supply(kord))
Expand Down
16 changes: 12 additions & 4 deletions core/src/main/kotlin/behavior/UserBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,28 @@ interface UserBehavior : KordEntity, Strategizable {


/**
* Requests to get the this behavior as a [User].
* Requests to get the this behavior as a [User] if it's not an instance of a [User].
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
suspend fun asUser(): User = supplier.getUser(id)
suspend fun asUser(): User = if (this is User) {
this
} else {
supplier.getUser(id)
}

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


/**
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/behavior/channel/CategoryBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface CategoryBehavior : TopGuildChannelBehavior {
* @throws [EntityNotFoundException] if the channel wasn't present.
* @throws [ClassCastException] if the channel wasn't a category.
*/
override suspend fun asChannel(): Category = supplier.getChannelOf(id)
override suspend fun asChannel(): Category = super.asChannel() as Category

/**
* Requests to get this behavior as a [Category],
Expand All @@ -48,7 +48,7 @@ interface CategoryBehavior : TopGuildChannelBehavior {
* @throws [EntityNotFoundException] if the channel wasn't present.
* @throws [ClassCastException] if the channel wasn't a category.
*/
override suspend fun asChannelOrNull(): Category? = supplier.getChannelOf(id)
override suspend fun asChannelOrNull(): Category? = super.asChannelOrNull() as? Category


/**
Expand Down
22 changes: 14 additions & 8 deletions core/src/main/kotlin/behavior/channel/ChannelBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import dev.kord.core.Kord
import dev.kord.core.entity.KordEntity
import dev.kord.core.entity.Strategizable
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.GuildChannel
import dev.kord.core.entity.channel.TopGuildChannel
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
Expand All @@ -28,20 +26,28 @@ interface ChannelBehavior : KordEntity, Strategizable {
val mention get() = "<#${id.value}>"

/**
* Requests to get this behavior as a [Channel] .
* Requests to get this behavior as a [Channel] if its not an instance of a [Channel].
*
* @throws [RequestException] if something went wrong during the request.
* @throws [EntityNotFoundException] if the channel wasn't present.
*/
suspend fun asChannel(): Channel = supplier.getChannel(id)
suspend fun asChannel(): Channel = if (this is Channel) {
this
} else {
supplier.getChannel(id)
}

/**
* Requests to get this behavior as a [Channel],
* Requests to get this behavior as a [Channel] if its not an instance of a [Channel],
* returns null if the channel isn't present.
*
* @throws [RequestException] if something went wrong during the request.
*/
suspend fun asChannelOrNull(): Channel? = supplier.getChannelOrNull(id)
suspend fun asChannelOrNull(): Channel? = if (this is Channel) {
this
} else {
supplier.getChannelOrNull(id)
}

/**
* Requests to delete a channel (or close it if this is a dm channel).
Expand All @@ -67,7 +73,7 @@ interface ChannelBehavior : KordEntity, Strategizable {
* @throws [RequestException] if anything went wrong during the request.
* @throws [ClassCastException] if the channel is not of type [T]
*/
suspend inline fun <reified T: Channel> ChannelBehavior.ofOrNull(): T? = supplier.getChannelOfOrNull(id)
suspend inline fun <reified T : Channel> ChannelBehavior.ofOrNull(): T? = supplier.getChannelOfOrNull(id)

/**
* Requests to get the [Channel] represented by the [id].
Expand All @@ -76,7 +82,7 @@ suspend inline fun <reified T: Channel> ChannelBehavior.ofOrNull(): T? = supplie
* @throws [EntityNotFoundException] if the [Channel] wasn't present.
* @throws [ClassCastException] if the channel is not of type [T].
*/
suspend inline fun <reified T: Channel> ChannelBehavior.of(): T = supplier.getChannelOf(id)
suspend inline fun <reified T : Channel> ChannelBehavior.of(): T = supplier.getChannelOf(id)

fun ChannelBehavior(id: Snowflake, kord: Kord, strategy: EntitySupplyStrategy<*> = kord.resources.defaultStrategy) =
object : ChannelBehavior {
Expand Down

0 comments on commit aa0dce2

Please sign in to comment.