From ce4f1167c1d3b280b9912101145bb75b96a0dbcd Mon Sep 17 00:00:00 2001 From: BartArys Date: Tue, 30 Jun 2020 19:59:54 +0200 Subject: [PATCH] Fix typo and add getChannel variants to guild --- CHANGELOG.md | 10 +++ .../kordlib/core/behavior/GuildBehavior.kt | 69 ++++++++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13a28149219..871984ca1b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +# 0.5.4 + +## Additions + +* Added `GuildBehavior#getChannel`, `GuildBehavior#getChannelOf` and their nullable variants. + +## Deprecations + +* `GuildBehavior#unBan` has been deprecated for `GuildBehavior#unban`. + # 0.5.3 ## Fixes diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/GuildBehavior.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/GuildBehavior.kt index c102f2a75c5..8cb947eaef7 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/GuildBehavior.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/GuildBehavior.kt @@ -13,6 +13,8 @@ import com.gitlab.kordlib.core.sorted import com.gitlab.kordlib.core.supplier.EntitySupplier import com.gitlab.kordlib.core.supplier.EntitySupplyStrategy import com.gitlab.kordlib.core.supplier.EntitySupplyStrategy.Companion.rest +import com.gitlab.kordlib.core.supplier.getChannelOf +import com.gitlab.kordlib.core.supplier.getChannelOfOrNull import com.gitlab.kordlib.rest.builder.ban.BanCreateBuilder import com.gitlab.kordlib.rest.builder.channel.* import com.gitlab.kordlib.rest.builder.guild.EmojiCreateBuilder @@ -257,16 +259,53 @@ interface GuildBehavior : Entity, Strategizable { */ suspend fun getBanOrNull(userId: Snowflake): Ban? = supplier.getGuildBanOrNull(id, userId) + /** + * Requests to get the [GuildChannel] represented by the [id]. + * + * @throws [RequestException] if anything went wrong during the request. + * @throws [EntityNotFoundException] if the [GuildChannel] wasn't present. + * @throws [ClassCastException] if the channel is not a [GuildChannel]. + * @throws [IllegalArgumentException] if the channel is not part of this guild. + */ + suspend fun getChannel(id: Snowflake) : GuildChannel { + val channel = supplier.getChannelOf(id) + require(channel.guildId == id) { "channel ${id.value} is not in guild ${id.value}" } + return channel + } + + /** + * Requests to get the [GuildChannel] represented by the [id], + * returns null if the [GuildChannel] isn't present. + * + * @throws [RequestException] if anything went wrong during the request. + * @throws [ClassCastException] if the channel is not a [GuildChannel]. + * @throws [IllegalArgumentException] if the channel is not part of this guild. + */ + suspend fun getChannelOrNull(id: Snowflake) : GuildChannel? { + val channel = supplier.getChannelOfOrNull(id) ?: return null + require(channel.guildId == id) { "channel ${id.value} is not in guild ${id.value}" } + return channel + } /** * Requests to unban the given [userId]. * * @throws [RestRequestException] if something went wrong during the request. */ + @Deprecated("unBan is a typo", ReplaceWith("unban")) suspend fun unBan(userId: Snowflake) { kord.rest.guild.deleteGuildBan(guildId = id.value, userId = userId.value) } + /** + * Requests to unban the given [userId]. + * + * @throws [RestRequestException] if something went wrong during the request. + */ + suspend fun unban(userId: Snowflake) { + kord.rest.guild.deleteGuildBan(guildId = id.value, userId = userId.value) + } + /** * Returns the preview of this guild. The bot does not need to present in this guild * for this to complete successfully. @@ -467,4 +506,32 @@ suspend inline fun GuildBehavior.addRole(builder: RoleCreateBuilder.() -> Unit): */ suspend inline fun GuildBehavior.ban(userId: Snowflake, builder: BanCreateBuilder.() -> Unit) { kord.rest.guild.addGuildBan(guildId = id.value, userId = userId.value, builder = builder) -} \ No newline at end of file +} + +/** + * Requests to get the [GuildChannel] represented by the [id] as type [T]. + * + * @throws [RequestException] if anything went wrong during the request. + * @throws [EntityNotFoundException] if the [T] wasn't present. + * @throws [ClassCastException] if the channel is not of type [T]. + * @throws [IllegalArgumentException] if the channel is not part of this guild. + */ +suspend inline fun GuildBehavior.getChannelOf(id: Snowflake) : T { + val channel = supplier.getChannelOf(id) + require(channel.guildId == id) { "channel ${id.value} is not in guild ${id.value}" } + return channel +} + +/** + * Requests to get the [GuildChannel] represented by the [id] as type [T], + * returns null if the [GuildChannel] isn't present. + * + * @throws [RequestException] if anything went wrong during the request. + * @throws [ClassCastException] if the channel is not of type [T]. + * @throws [IllegalArgumentException] if the channel is not part of this guild. + */ +suspend inline fun GuildBehavior.getChannelOfOrNull(id: Snowflake) : T? { + val channel = supplier.getChannelOfOrNull(id) ?: return null + require(channel.guildId == id) { "channel ${id.value} is not in guild ${id.value}" } + return channel +}