Skip to content

Commit

Permalink
Add WebhookClient mode and unify builders
Browse files Browse the repository at this point in the history
- Extract webhook related functions of Kord into WebhookClient
- Extract webhook constructor of Unsafe to WebhookUnsafe
- Add AbstractKordBuilder
- Add stackTraceRecovery to AbstractKordBuilder and therefore restOnly and webhook mode
  • Loading branch information
DRSchlaubi committed Aug 27, 2023
1 parent d94eeca commit 9ce6b6a
Show file tree
Hide file tree
Showing 10 changed files with 338 additions and 150 deletions.
103 changes: 68 additions & 35 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public final class dev/kord/core/ClientResources {
public fun toString ()Ljava/lang/String;
}

public final class dev/kord/core/Kord : kotlinx/coroutines/CoroutineScope {
public final class dev/kord/core/Kord : dev/kord/core/WebhookClient, kotlinx/coroutines/CoroutineScope {
public static final field Companion Ldev/kord/core/Kord$Companion;
public fun <init> (Ldev/kord/core/ClientResources;Ldev/kord/cache/api/DataCache;Ldev/kord/core/gateway/MasterGateway;Ldev/kord/rest/service/RestClient;Ldev/kord/common/entity/Snowflake;Lkotlinx/coroutines/flow/MutableSharedFlow;Lkotlinx/coroutines/CoroutineDispatcher;Ldev/kord/core/gateway/handler/GatewayEventInterceptor;)V
public final fun createGlobalApplicationCommands (Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down Expand Up @@ -66,23 +66,20 @@ public final class dev/kord/core/Kord : kotlinx/coroutines/CoroutineScope {
public static synthetic fun getInviteOrNull$default (Ldev/kord/core/Kord;Ljava/lang/String;ZZLdev/kord/common/entity/Snowflake;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun getNitroStickerPacks ()Lkotlinx/coroutines/flow/Flow;
public final fun getRegions ()Lkotlinx/coroutines/flow/Flow;
public final fun getResources ()Ldev/kord/core/ClientResources;
public fun getResources ()Ldev/kord/core/ClientResources;
public final fun getRest ()Ldev/kord/rest/service/RestClient;
public final fun getSelf (Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun getSelf$default (Ldev/kord/core/Kord;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun getSelfId ()Ldev/kord/common/entity/Snowflake;
public final fun getSticker (Ldev/kord/common/entity/Snowflake;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getUnsafe ()Ldev/kord/core/Unsafe;
public fun getUnsafe ()Ldev/kord/core/Unsafe;
public synthetic fun getUnsafe ()Ldev/kord/core/WebhookUnsafe;
public final fun getUser (Ldev/kord/common/entity/Snowflake;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun getUser$default (Ldev/kord/core/Kord;Ldev/kord/common/entity/Snowflake;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun getWebhook (Ldev/kord/common/entity/Snowflake;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun getWebhook$default (Ldev/kord/core/Kord;Ldev/kord/common/entity/Snowflake;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun getWebhookOrNull (Ldev/kord/common/entity/Snowflake;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun getWebhookOrNull$default (Ldev/kord/core/Kord;Ldev/kord/common/entity/Snowflake;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun getWebhookWithToken (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun getWebhookWithToken$default (Ldev/kord/core/Kord;Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun getWebhookWithTokenOrNull (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun getWebhookWithTokenOrNull$default (Ldev/kord/core/Kord;Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public fun getWebhook (Ldev/kord/common/entity/Snowflake;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getWebhookOrNull (Ldev/kord/common/entity/Snowflake;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getWebhookWithToken (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun getWebhookWithTokenOrNull (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun hashCode ()I
public final fun login (Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun login$default (Ldev/kord/core/Kord;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
Expand All @@ -98,6 +95,7 @@ public final class dev/kord/core/Kord$Companion {
public static synthetic fun proxy$default (Ldev/kord/core/Kord$Companion;Ldev/kord/common/entity/Snowflake;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ldev/kord/core/Kord;
public final fun restOnly (Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Ldev/kord/core/Kord;
public static synthetic fun restOnly$default (Ldev/kord/core/Kord$Companion;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ldev/kord/core/Kord;
public final fun webhookClient (Lkotlin/jvm/functions/Function1;)Ldev/kord/core/WebhookClient;
}

public final class dev/kord/core/KordKt {
Expand All @@ -110,7 +108,7 @@ public abstract interface class dev/kord/core/KordObject {
public abstract fun getKord ()Ldev/kord/core/Kord;
}

public final class dev/kord/core/Unsafe {
public final class dev/kord/core/Unsafe : dev/kord/core/WebhookUnsafe {
public fun <init> (Ldev/kord/core/Kord;)V
public final fun applicationCommandInteraction (Ldev/kord/common/entity/Snowflake;Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/common/entity/Snowflake;)Ldev/kord/core/behavior/interaction/ApplicationCommandInteractionBehavior;
public final fun autoModerationRule (Ldev/kord/common/entity/Snowflake;Ldev/kord/common/entity/Snowflake;)Ldev/kord/core/behavior/automoderation/AutoModerationRuleBehavior;
Expand Down Expand Up @@ -149,7 +147,7 @@ public final class dev/kord/core/Unsafe {
public final fun topGuildMessageChannel (Ldev/kord/common/entity/Snowflake;Ldev/kord/common/entity/Snowflake;)Ldev/kord/core/behavior/channel/TopGuildMessageChannelBehavior;
public final fun user (Ldev/kord/common/entity/Snowflake;)Ldev/kord/core/behavior/UserBehavior;
public final fun voiceChannel (Ldev/kord/common/entity/Snowflake;Ldev/kord/common/entity/Snowflake;)Ldev/kord/core/behavior/channel/VoiceChannelBehavior;
public final fun webhook (Ldev/kord/common/entity/Snowflake;)Ldev/kord/core/behavior/WebhookBehavior;
public fun webhook (Ldev/kord/common/entity/Snowflake;)Ldev/kord/core/behavior/WebhookBehavior;
}

public final class dev/kord/core/UtilKt {
Expand All @@ -158,6 +156,26 @@ public final class dev/kord/core/UtilKt {
public static final fun enableEvents (Ldev/kord/gateway/Intents$IntentsBuilder;[Lkotlin/reflect/KClass;)V
}

public abstract interface class dev/kord/core/WebhookClient {
public abstract fun getResources ()Ldev/kord/core/ClientResources;
public abstract fun getUnsafe ()Ldev/kord/core/WebhookUnsafe;
public abstract fun getWebhook (Ldev/kord/common/entity/Snowflake;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun getWebhookOrNull (Ldev/kord/common/entity/Snowflake;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun getWebhookWithToken (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun getWebhookWithTokenOrNull (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class dev/kord/core/WebhookClient$DefaultImpls {
public static synthetic fun getWebhook$default (Ldev/kord/core/WebhookClient;Ldev/kord/common/entity/Snowflake;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun getWebhookOrNull$default (Ldev/kord/core/WebhookClient;Ldev/kord/common/entity/Snowflake;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun getWebhookWithToken$default (Ldev/kord/core/WebhookClient;Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun getWebhookWithTokenOrNull$default (Ldev/kord/core/WebhookClient;Ldev/kord/common/entity/Snowflake;Ljava/lang/String;Ldev/kord/core/supplier/EntitySupplyStrategy;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
}

public abstract interface class dev/kord/core/WebhookUnsafe {
public abstract fun webhook (Ldev/kord/common/entity/Snowflake;)Ldev/kord/core/behavior/WebhookBehavior;
}

public abstract interface class dev/kord/core/behavior/ApplicationCommandBehavior : dev/kord/core/entity/Entity {
public abstract fun delete (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun getApplicationId ()Ldev/kord/common/entity/Snowflake;
Expand Down Expand Up @@ -2073,30 +2091,48 @@ public final class dev/kord/core/builder/components/ButtonBuilderExtensionsKt {
public static final fun emoji (Ldev/kord/rest/builder/component/ButtonBuilder;Ldev/kord/core/entity/ReactionEmoji$Unicode;)V
}

public abstract class dev/kord/core/builder/kord/BaseKordBuilder {
public abstract class dev/kord/core/builder/kord/AbstractKordBuilder {
protected final fun buildRequestHandler (Ldev/kord/core/ClientResources;)Ldev/kord/rest/request/RequestHandler;
public final fun getDefaultDispatcher ()Lkotlinx/coroutines/CoroutineDispatcher;
protected final fun getHandlerBuilder ()Lkotlin/jvm/functions/Function1;
public final fun getHttpClient ()Lio/ktor/client/HttpClient;
public final fun getStackTraceRecovery ()Z
public final fun requestHandler (Lkotlin/jvm/functions/Function1;)V
public final fun setDefaultDispatcher (Lkotlinx/coroutines/CoroutineDispatcher;)V
protected final fun setHandlerBuilder (Lkotlin/jvm/functions/Function1;)V
public final fun setHttpClient (Lio/ktor/client/HttpClient;)V
public final fun setStackTraceRecovery (Z)V
}

public abstract class dev/kord/core/builder/kord/BaseKordBuilder : dev/kord/core/builder/kord/AbstractKordBuilder, dev/kord/core/builder/kord/HasApplication {
public fun build (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
protected final fun buildBase (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun cache (Lkotlin/jvm/functions/Function2;)V
public final fun gateways (Lkotlin/jvm/functions/Function2;)V
public final fun getApplicationId ()Ldev/kord/common/entity/Snowflake;
public final fun getDefaultDispatcher ()Lkotlinx/coroutines/CoroutineDispatcher;
public fun getActualApplicationId ()Ldev/kord/common/entity/Snowflake;
public fun getApplicationId ()Ldev/kord/common/entity/Snowflake;
public final fun getDefaultStrategy ()Ldev/kord/core/supplier/EntitySupplyStrategy;
public final fun getEventFlow ()Lkotlinx/coroutines/flow/MutableSharedFlow;
public final fun getGatewayEventInterceptor ()Ldev/kord/core/gateway/handler/GatewayEventInterceptor;
public final fun getHttpClient ()Lio/ktor/client/HttpClient;
public final fun getStackTraceRecovery ()Z
public final fun getToken ()Ljava/lang/String;
public final fun requestHandler (Lkotlin/jvm/functions/Function1;)V
public final fun setApplicationId (Ldev/kord/common/entity/Snowflake;)V
public final fun setDefaultDispatcher (Lkotlinx/coroutines/CoroutineDispatcher;)V
public fun getToken ()Ljava/lang/String;
public fun setApplicationId (Ldev/kord/common/entity/Snowflake;)V
public final fun setDefaultStrategy (Ldev/kord/core/supplier/EntitySupplyStrategy;)V
public final fun setEventFlow (Lkotlinx/coroutines/flow/MutableSharedFlow;)V
public final fun setGatewayEventInterceptor (Ldev/kord/core/gateway/handler/GatewayEventInterceptor;)V
public final fun setHttpClient (Lio/ktor/client/HttpClient;)V
public final fun setStackTraceRecovery (Z)V
public final fun sharding (Lkotlin/jvm/functions/Function1;)V
}

public abstract interface class dev/kord/core/builder/kord/HasApplication {
public abstract fun getActualApplicationId ()Ldev/kord/common/entity/Snowflake;
public abstract fun getApplicationId ()Ldev/kord/common/entity/Snowflake;
public abstract fun getToken ()Ljava/lang/String;
public abstract fun setApplicationId (Ldev/kord/common/entity/Snowflake;)V
}

public final class dev/kord/core/builder/kord/HasApplication$DefaultImpls {
public static fun getActualApplicationId (Ldev/kord/core/builder/kord/HasApplication;)Ldev/kord/common/entity/Snowflake;
}

public final class dev/kord/core/builder/kord/KordBuilder : dev/kord/core/builder/kord/BaseKordBuilder {
public fun <init> (Ljava/lang/String;)V
public fun build (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand All @@ -2110,6 +2146,7 @@ public final class dev/kord/core/builder/kord/KordBuilderUtilKt {
public final class dev/kord/core/builder/kord/KordProxyBuilder : dev/kord/core/builder/kord/RestOnlyBuilder {
public fun <init> (Ldev/kord/common/entity/Snowflake;)V
public fun getApplicationId ()Ldev/kord/common/entity/Snowflake;
public fun getToken ()Ljava/lang/String;
public fun setApplicationId (Ldev/kord/common/entity/Snowflake;)V
}

Expand All @@ -2121,19 +2158,15 @@ public final class dev/kord/core/builder/kord/KordRestOnlyBuilder : dev/kord/cor
public fun setToken (Ljava/lang/String;)V
}

public abstract class dev/kord/core/builder/kord/RestOnlyBuilder {
public abstract class dev/kord/core/builder/kord/RestOnlyBuilder : dev/kord/core/builder/kord/AbstractKordBuilder, dev/kord/core/builder/kord/HasApplication {
public fun <init> ()V
public final fun build ()Ldev/kord/core/Kord;
public abstract fun getApplicationId ()Ldev/kord/common/entity/Snowflake;
public final fun getDefaultDispatcher ()Lkotlinx/coroutines/CoroutineDispatcher;
protected final fun getHandlerBuilder ()Lkotlin/jvm/functions/Function1;
public final fun getHttpClient ()Lio/ktor/client/HttpClient;
protected abstract fun getToken ()Ljava/lang/String;
public final fun requestHandler (Lkotlin/jvm/functions/Function1;)V
public abstract fun setApplicationId (Ldev/kord/common/entity/Snowflake;)V
public final fun setDefaultDispatcher (Lkotlinx/coroutines/CoroutineDispatcher;)V
protected final fun setHandlerBuilder (Lkotlin/jvm/functions/Function1;)V
public final fun setHttpClient (Lio/ktor/client/HttpClient;)V
public fun getActualApplicationId ()Ldev/kord/common/entity/Snowflake;
}

public final class dev/kord/core/builder/kord/WebhookClientBuilder : dev/kord/core/builder/kord/AbstractKordBuilder {
public fun <init> ()V
public final fun build ()Ldev/kord/core/WebhookClient;
}

public final class dev/kord/core/cache/CachingGateway : dev/kord/cache/api/DataCache, dev/kord/gateway/Gateway, kotlinx/coroutines/CoroutineScope {
Expand Down
48 changes: 37 additions & 11 deletions core/src/commonMain/kotlin/Kord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import dev.kord.common.exception.RequestException
import dev.kord.core.builder.kord.KordBuilder
import dev.kord.core.builder.kord.KordProxyBuilder
import dev.kord.core.builder.kord.KordRestOnlyBuilder
import dev.kord.core.builder.kord.WebhookClientBuilder
import dev.kord.core.cache.data.ApplicationCommandData
import dev.kord.core.cache.data.GuildData
import dev.kord.core.cache.data.UserData
Expand Down Expand Up @@ -48,15 +49,15 @@ public val kordLogger: KLogger = KotlinLogging.logger { }
* The central adapter between other Kord modules and source of core [events].
*/
public class Kord(
public val resources: ClientResources,
public override val resources: ClientResources,
public val cache: DataCache,
public val gateway: MasterGateway,
public val rest: RestClient,
public val selfId: Snowflake,
private val eventFlow: MutableSharedFlow<Event>,
dispatcher: CoroutineDispatcher,
private val interceptor: GatewayEventInterceptor,
) : CoroutineScope {
) : CoroutineScope, WebhookClient {

public val nitroStickerPacks: Flow<StickerPack>
get() = defaultSupplier.getNitroStickerPacks()
Expand All @@ -74,7 +75,7 @@ public class Kord(
* A reference to all exposed [unsafe][KordUnsafe] entity constructors for this instance.
*/
@OptIn(KordUnsafe::class)
public val unsafe: Unsafe = Unsafe(this)
public override val unsafe: Unsafe = Unsafe(this)

/**
* The events emitted from the [gateway]. Call [Kord.login] to start receiving events.
Expand Down Expand Up @@ -272,9 +273,9 @@ public class Kord(
* @throws [RestRequestException] if something went wrong during the request.
* @throws [EntityNotFoundException] if the webhook was not present.
*/
public suspend fun getWebhook(
override suspend fun getWebhook(
id: Snowflake,
strategy: EntitySupplyStrategy<*> = resources.defaultStrategy
strategy: EntitySupplyStrategy<*>
): Webhook = strategy.supply(this).getWebhook(id)

/**
Expand All @@ -284,9 +285,9 @@ public class Kord(
* @throws [RestRequestException] if something went wrong during the request.
*/

public suspend fun getWebhookOrNull(
override suspend fun getWebhookOrNull(
id: Snowflake,
strategy: EntitySupplyStrategy<*> = resources.defaultStrategy
strategy: EntitySupplyStrategy<*>
): Webhook? = strategy.supply(this).getWebhookOrNull(id)

/**
Expand All @@ -295,10 +296,10 @@ public class Kord(
* @throws [RestRequestException] if something went wrong during the request.
* @throws [EntityNotFoundException] if the webhook was not present.
*/
public suspend fun getWebhookWithToken(
override suspend fun getWebhookWithToken(
id: Snowflake,
token: String,
strategy: EntitySupplyStrategy<*> = resources.defaultStrategy
strategy: EntitySupplyStrategy<*>
): Webhook = strategy.supply(this).getWebhookWithToken(id, token)

/**
Expand All @@ -308,10 +309,10 @@ public class Kord(
* @throws [RestRequestException] if something went wrong during the request.
*/

public suspend fun getWebhookWithTokenOrNull(
override suspend fun getWebhookWithTokenOrNull(
id: Snowflake,
token: String,
strategy: EntitySupplyStrategy<*> = resources.defaultStrategy
strategy: EntitySupplyStrategy<*>
): Webhook? =
strategy.supply(this).getWebhookWithTokenOrNull(id, token)

Expand Down Expand Up @@ -416,6 +417,31 @@ public class Kord(
return KordRestOnlyBuilder(token).apply(builder).build()
}

/**
* Builds a [WebhookClient] instance configured by the [builder].
*
* This returns a [WebhookClient] instead of a normal [Kord] instance, the
* underlying [Kord] instance returned by [Webhook] objects will only work for
* webhook requests
*
* ```kotlin
* val client = Kord.webhookClient()
* val webhook = client.unsafe.webhook(Snowflake(1234))
*
* webhook.execute(webhookToken) {
* content = "Cool message"
* }
* ```
*/
@KordExperimental
public inline fun webhookClient(builder: WebhookClientBuilder.() -> Unit): WebhookClient {
contract {
callsInPlace(builder, InvocationKind.EXACTLY_ONCE)
}

return WebhookClientBuilder().apply(builder).build()
}

/**
* Builds a [Kord] instance configured by the [builder].
*
Expand Down
Loading

0 comments on commit 9ce6b6a

Please sign in to comment.