Skip to content

Commit

Permalink
Behaviour improvement (#396)
Browse files Browse the repository at this point in the history
* Behaviour improvement

* Provide "fetch" method in behaviours
  • Loading branch information
ByteAlex authored Sep 25, 2021
1 parent a059a59 commit 5b46c75
Show file tree
Hide file tree
Showing 34 changed files with 442 additions and 11 deletions.
17 changes: 17 additions & 0 deletions core/src/main/kotlin/behavior/GuildBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,23 @@ interface GuildBehavior : KordEntity, Strategizable {
*/
suspend fun asGuildOrNull(): Guild? = supplier.getGuildOrNull(id)

/**
* Retrieve the [Guild] associated with this behaviour from the provided [EntitySupplier]
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
suspend fun fetchGuild(): Guild = supplier.getGuild(id)


/**
* Retrieve the [Guild] associated with this behaviour from the provided [EntitySupplier]
* returns null if the [Guild] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun fetchGuildOrNull(): Guild? = supplier.getGuildOrNull(id)

/**
* Requests to delete this guild.
*
Expand Down
17 changes: 17 additions & 0 deletions core/src/main/kotlin/behavior/MemberBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ interface MemberBehavior : KordEntity, UserBehavior {
*/
suspend fun asMemberOrNull(): Member? = supplier.getMemberOrNull(guildId, id)

/**
* Retrieve the [Member] associated with this behaviour from the provided [EntitySupplier]
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
suspend fun fetchMember(): Member = supplier.getMember(guildId, id)


/**
* Retrieve the [Member] associated with this behaviour from the provided [EntitySupplier]
* returns null if the [Member] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun fetchMemberOrNull(): Member? = supplier.getMemberOrNull(guildId, id)

/**
* Requests to kick this member from its guild.
*
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/kotlin/behavior/MessageBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ interface MessageBehavior : KordEntity, Strategizable {
*/
suspend fun asMessageOrNull(): Message? = supplier.getMessageOrNull(channelId = channelId, messageId = id)

/**
* Retrieve the [Message] associated with this behaviour from the provided [EntitySupplier]
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
suspend fun fetchMessage(): Message = supplier.getMessage(channelId, id)


/**
* Retrieve the [Message] associated with this behaviour from the provided [EntitySupplier]
* returns null if the [Message] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun fetchMessageOrNull(): Message? = supplier.getMessageOrNull(channelId, id)

/**
* Requests to delete this message.
Expand Down
36 changes: 36 additions & 0 deletions core/src/main/kotlin/behavior/RoleBehavior.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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.entity.User
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 +46,39 @@ 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)

/**
* Retrieve the [Role] associated with this behaviour from the provided [EntitySupplier]
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
suspend fun fetchRole(): Role = supplier.getRole(guildId, id)


/**
* Retrieve the [Role] associated with this behaviour from the provided [EntitySupplier]
* returns null if the [Role] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun fetchRoleOrNull(): Role? = supplier.getRoleOrNull(guildId, id)

/**
* Requests to change the [position] of this role.
*
Expand Down
34 changes: 34 additions & 0 deletions core/src/main/kotlin/behavior/StageInstanceBehavior.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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.entity.User
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 +25,39 @@ 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)

/**
* Retrieve the [StageInstance] associated with this behaviour from the provided [EntitySupplier]
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
suspend fun fetchStageInstance(): StageInstance = supplier.getStageInstance(id)


/**
* Retrieve the [StageInstance] associated with this behaviour from the provided [EntitySupplier]
* returns null if the [StageInstance] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun fetchStageInstanceOrNull(): StageInstance? = supplier.getStageInstanceOrNull(id)

override fun withStrategy(strategy: EntitySupplyStrategy<*>): StageInstanceBehavior =
StageInstanceBehavior(id, channelId, kord, strategy.supply(kord))
}
Expand Down
33 changes: 33 additions & 0 deletions core/src/main/kotlin/behavior/UserBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,39 @@ interface UserBehavior : KordEntity, Strategizable {
*/
suspend fun asUserOrNull(): User? = supplier.getUserOrNull(id)

/**
* Retrieve the [Member] associated with this behaviour from the provided [EntitySupplier]
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
suspend fun fetchMember(guildId: Snowflake): Member = supplier.getMember(guildId, id)

/**
* Retrieve the [Member] associated with this behaviour from the provided [EntitySupplier]
* returns null if the [Member] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun fetchMemberOrNull(guildId: Snowflake): Member? = supplier.getMemberOrNull(guildId, id)

/**
* Retrieve the [User] associated with this behaviour from the provided [EntitySupplier]
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
suspend fun fetchUser(): User = supplier.getUser(id)


/**
* Retrieve the [User] associated with this behaviour from the provided [EntitySupplier]
* returns null if the [User] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun fetchUserOrNull(): User? = supplier.getUserOrNull(id)


/**
* Requests to get or create a [DmChannel] between this bot and the user.
Expand Down
20 changes: 19 additions & 1 deletion core/src/main/kotlin/behavior/channel/CategoryBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.core.supplier.getChannelOf
import dev.kord.core.supplier.getChannelOfOrNull
import dev.kord.rest.builder.channel.CategoryModifyBuilder
import dev.kord.rest.builder.channel.NewsChannelCreateBuilder
import dev.kord.rest.builder.channel.TextChannelCreateBuilder
Expand Down Expand Up @@ -48,7 +49,24 @@ 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? = supplier.getChannelOfOrNull(id)

/**
* Retrieve the [Category] associated with this behaviour from the provided [EntitySupplier]
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
override suspend fun fetchChannel(): Category = supplier.getChannelOf(id)


/**
* Retrieve the [Category] associated with this behaviour from the provided [EntitySupplier]
* returns null if the [Category] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
override suspend fun fetchChannelOrNull(): Category? = supplier.getChannelOfOrNull(id)


/**
Expand Down
18 changes: 18 additions & 0 deletions core/src/main/kotlin/behavior/channel/ChannelBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.kord.core.behavior.channel
import dev.kord.common.entity.Snowflake
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.entity.Guild
import dev.kord.core.entity.KordEntity
import dev.kord.core.entity.Strategizable
import dev.kord.core.entity.channel.Channel
Expand Down Expand Up @@ -43,6 +44,23 @@ interface ChannelBehavior : KordEntity, Strategizable {
*/
suspend fun asChannelOrNull(): Channel? = supplier.getChannelOrNull(id)

/**
* Retrieve the [Channel] associated with this behaviour from the provided [EntitySupplier]
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
suspend fun fetchChannel(): Channel = supplier.getChannel(id)


/**
* Retrieve the [Channel] associated with this behaviour from the provided [EntitySupplier]
* returns null if the [Channel] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
suspend fun fetchChannelOrNull(): Channel? = supplier.getChannelOrNull(id)

/**
* Requests to delete a channel (or close it if this is a dm channel).
*
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/kotlin/behavior/channel/GuildChannelBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.behavior.GuildBehavior
import dev.kord.core.entity.*
import dev.kord.core.entity.channel.Category
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
import dev.kord.core.supplier.getChannelOf
import dev.kord.core.supplier.getChannelOfOrNull
import java.util.*

/**
Expand Down Expand Up @@ -44,6 +47,23 @@ interface GuildChannelBehavior : ChannelBehavior, Strategizable {
*/
override suspend fun asChannelOrNull(): GuildChannel? = super.asChannelOrNull() as? GuildChannel

/**
* Retrieve the [GuildChannel] associated with this behaviour from the provided [EntitySupplier]
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
override suspend fun fetchChannel(): GuildChannel = super.fetchChannel() as GuildChannel


/**
* Retrieve the [GuildChannel] associated with this behaviour from the provided [EntitySupplier]
* returns null if the [GuildChannel] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
override suspend fun fetchChannelOrNull(): GuildChannel? = super.fetchChannelOrNull() as? GuildChannel

/**
* Requests to get this channel's [Guild].
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package dev.kord.core.behavior.channel

import dev.kord.common.entity.Snowflake
import dev.kord.common.exception.RequestException
import dev.kord.core.Kord
import dev.kord.core.entity.channel.GuildChannel
import dev.kord.core.entity.channel.GuildMessageChannel
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.rest.json.request.BulkDeleteRequest
Expand Down Expand Up @@ -46,6 +49,26 @@ interface GuildMessageChannelBehavior : GuildChannelBehavior, MessageChannelBeha
return super<GuildChannelBehavior>.asChannelOrNull() as? GuildMessageChannel
}


/**
* Retrieve the [GuildMessageChannel] associated with this behaviour from the provided [EntitySupplier]
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
override suspend fun fetchChannel(): GuildMessageChannel =
super<GuildChannelBehavior>.fetchChannel() as GuildMessageChannel


/**
* Retrieve the [GuildMessageChannel] associated with this behaviour from the provided [EntitySupplier]
* returns null if the [GuildMessageChannel] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
override suspend fun fetchChannelOrNull(): GuildMessageChannel? =
super<GuildChannelBehavior>.fetchChannelOrNull() as? GuildMessageChannel

/**
* Returns a new [GuildMessageChannelBehavior] with the given [strategy].
*/
Expand Down
18 changes: 18 additions & 0 deletions core/src/main/kotlin/behavior/channel/MessageChannelBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dev.kord.core.Kord
import dev.kord.core.cache.data.MessageData
import dev.kord.core.entity.Message
import dev.kord.core.entity.Strategizable
import dev.kord.core.entity.channel.GuildChannel
import dev.kord.core.entity.channel.MessageChannel
import dev.kord.core.exception.EntityNotFoundException
import dev.kord.core.supplier.EntitySupplier
Expand Down Expand Up @@ -54,6 +55,23 @@ interface MessageChannelBehavior : ChannelBehavior, Strategizable {
*/
override suspend fun asChannelOrNull(): MessageChannel? = super.asChannelOrNull() as? MessageChannel

/**
* Retrieve the [MessageChannel] associated with this behaviour from the provided [EntitySupplier]
*
* @throws [RequestException] if anything went wrong during the request.
* @throws [EntityNotFoundException] if the user wasn't present.
*/
override suspend fun fetchChannel(): MessageChannel = super.fetchChannel() as MessageChannel


/**
* Retrieve the [MessageChannel] associated with this behaviour from the provided [EntitySupplier]
* returns null if the [MessageChannel] isn't present.
*
* @throws [RequestException] if anything went wrong during the request.
*/
override suspend fun fetchChannelOrNull(): MessageChannel? = super.fetchChannelOrNull() as? MessageChannel

/**
* Requests to get all messages in this channel.
*
Expand Down
Loading

0 comments on commit 5b46c75

Please sign in to comment.