Skip to content

Commit

Permalink
Welcome: Allow extra serializers via DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed May 29, 2023
1 parent e2233e0 commit 84fd490
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ package org.quiltmc.community.cozy.modules.welcome.config
import com.kotlindiscord.kord.extensions.checks.types.Check
import dev.kord.core.entity.Guild
import dev.kord.core.entity.channel.GuildMessageChannel
import kotlinx.serialization.modules.PolymorphicModuleBuilder
import org.quiltmc.community.cozy.modules.welcome.blocks.Block
import kotlin.time.Duration

internal typealias LogChannelGetter = (suspend (channel: GuildMessageChannel, guild: Guild) -> GuildMessageChannel?)?

public class SimpleWelcomeChannelConfig(private val builder: Builder) : WelcomeChannelConfig() {
override val serializerBuilders: List<PolymorphicModuleBuilder<Block>.() -> Unit> =
builder.serializerBuilders

override suspend fun getLoggingChannel(channel: GuildMessageChannel, guild: Guild): GuildMessageChannel? =
builder.loggingChannelGetter?.invoke(channel, guild)

Expand All @@ -24,8 +29,13 @@ public class SimpleWelcomeChannelConfig(private val builder: Builder) : WelcomeC
internal val staffCommandChecks: MutableList<Check<*>> = mutableListOf()
internal var loggingChannelGetter: LogChannelGetter = null

public val serializerBuilders: MutableList<PolymorphicModuleBuilder<Block>.() -> Unit> = mutableListOf()
public var refreshDuration: Duration? = null

public fun serializer(body: PolymorphicModuleBuilder<Block>.() -> Unit) {
serializerBuilders.add(body)
}

public fun staffCommandCheck(body: Check<*>) {
staffCommandChecks.add(body)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ package org.quiltmc.community.cozy.modules.welcome.config
import com.kotlindiscord.kord.extensions.checks.types.Check
import dev.kord.core.entity.Guild
import dev.kord.core.entity.channel.GuildMessageChannel
import kotlinx.serialization.modules.PolymorphicModuleBuilder
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.modules.subclass
import org.quiltmc.community.cozy.modules.welcome.blocks.*
import kotlin.time.Duration

public abstract class WelcomeChannelConfig {
public val defaultSerializersModule: SerializersModule =
public open val serializerBuilders: List<PolymorphicModuleBuilder<Block>.() -> Unit> = mutableListOf()

public val defaultSerializersModule: SerializersModule by lazy {
SerializersModule {
polymorphic(Block::class) {
serializerBuilders.forEach { it() }

subclass(ComplianceBlock::class)
subclass(EmbedBlock::class)
subclass(LinksBlock::class)
Expand All @@ -29,6 +34,7 @@ public abstract class WelcomeChannelConfig {
subclass(ThreadListBlock::class)
}
}
}

/** Get the configured logging channel for the given channel and guild. **/
public abstract suspend fun getLoggingChannel(channel: GuildMessageChannel, guild: Guild): GuildMessageChannel?
Expand Down

0 comments on commit 84fd490

Please sign in to comment.