Skip to content

Commit

Permalink
[#58] The dreaded checks overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Jul 13, 2021
1 parent 8046c0a commit dbe2ad2
Show file tree
Hide file tree
Showing 36 changed files with 1,345 additions and 821 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,4 @@ fabric.properties
*.hprof
.env
docs/site
.linkie-cache/
3 changes: 3 additions & 0 deletions extra-modules/extra-mappings/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ dependencies {
implementation(libs.logging)
implementation(libs.kotlin.stdlib)

testImplementation(libs.groovy) // For logback config
testImplementation(libs.logback)

implementation(project(":kord-extensions"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.kotlindiscord.kord.extensions.modules.extra.mappings

import com.kotlindiscord.kord.extensions.checks.channelFor
import com.kotlindiscord.kord.extensions.checks.guildFor
import com.kotlindiscord.kord.extensions.checks.types.Check
import dev.kord.common.entity.Snowflake
import dev.kord.core.entity.channel.CategorizableChannel
import dev.kord.core.entity.channel.GuildChannel
Expand All @@ -25,71 +26,62 @@ import mu.KotlinLogging
* @param allowed List of allowed category IDs
* @param banned List of banned category IDs
*/
suspend fun allowedCategory(
fun allowedCategory(
allowed: List<Snowflake>,
banned: List<Snowflake>
): suspend (MessageCreateEvent) -> Boolean {
): Check<MessageCreateEvent> = {
val logger = KotlinLogging.logger { }
val channel = channelFor(event)

suspend fun inner(event: MessageCreateEvent): Boolean {
val channel = channelFor(event)
if (channel == null) {
logger.debug { "Passing: Event is not channel-related" }

if (channel == null) {
logger.debug { "Passing: Event is not channel-related" }

return true
}

if (channel !is CategorizableChannel) {
logger.debug { "Passing: Channel is not categorizable (eg, it's a DM)" }

return true // It's a DM
}
pass()
} else if (channel !is CategorizableChannel) {
logger.debug { "Passing: Channel is not categorizable (eg, it's a DM)" }

pass()
} else {
val parent = channel.category

if (allowed.isNotEmpty()) {
if (parent == null) {
logger.debug { "Failing: We have allowed categories, but the message was sent outside of a category" }

return false
}

return if (allowed.contains(parent.id)) {
fail()
} else if (allowed.contains(parent.id)) {
logger.debug { "Passing: Event happened in an allowed category" }

true
pass()
} else {
logger.debug { "Failing: Event happened outside of the allowed categories" }

false
fail()
}
}
} else {
if (parent == null) {
logger.debug {
"Passing: We have no allowed categories, and the message was sent outside of a category"
}

if (parent == null) {
logger.debug { "Passing: We have no allowed categories, and the message was sent outside of a category" }
pass()
} else if (banned.isNotEmpty()) {
if (!banned.contains(parent.id)) {
logger.debug { "Passing: Event did not happen in a banned category" }

return true
}
pass()
} else {
logger.debug { "Failing: Event happened in a banned category" }

if (banned.isNotEmpty()) {
return if (!banned.contains(parent.id)) {
logger.debug { "Passing: Event did not happen in a banned category" }

true
fail()
}
} else {
logger.debug { "Failing: Event happened in a banned category" }
logger.debug { "Passing: No allowed or banned categories configured" }

false
pass()
}
}

logger.debug { "Passing: No allowed or banned categories configured" }

return true
}

return ::inner
}

/**
Expand All @@ -108,57 +100,46 @@ suspend fun allowedCategory(
* @param allowed List of allowed channel IDs
* @param banned List of banned channel IDs
*/
suspend fun allowedChannel(
fun allowedChannel(
allowed: List<Snowflake>,
banned: List<Snowflake>
): suspend (MessageCreateEvent) -> Boolean {
): Check<MessageCreateEvent> = {
val logger = KotlinLogging.logger { }
val channel = channelFor(event)

suspend fun inner(event: MessageCreateEvent): Boolean {
val channel = channelFor(event)

if (channel == null) {
logger.debug { "Passing: Event is not channel-related" }
if (channel == null) {
logger.debug { "Passing: Event is not channel-related" }

return true
}

if (channel !is GuildChannel) {
logger.debug { "Passing: Message was sent privately" }

return true // It's a DM
}
pass()
} else if (channel !is GuildChannel) {
logger.debug { "Passing: Message was sent privately" }

if (allowed.isNotEmpty()) {
return if (allowed.contains(channel.id)) {
logger.debug { "Passing: Event happened in an allowed channel" }
pass() // It's a DM
} else if (allowed.isNotEmpty()) {
if (allowed.contains(channel.id)) {
logger.debug { "Passing: Event happened in an allowed channel" }

true
} else {
logger.debug { "Failing: Event did not happen in an allowed channel" }
pass()
} else {
logger.debug { "Failing: Event did not happen in an allowed channel" }

false
}
fail()
}
} else if (banned.isNotEmpty()) {
if (!banned.contains(channel.id)) {
logger.debug { "Passing: Event did not happen in a banned channel" }

if (banned.isNotEmpty()) {
return if (!banned.contains(channel.id)) {
logger.debug { "Passing: Event did not happen in a banned channel" }

true
} else {
logger.debug { "Failing: Event happened in a banned channel" }
pass()
} else {
logger.debug { "Failing: Event happened in a banned channel" }

false
}
fail()
}

} else {
logger.debug { "Passing: No allowed or banned channels configured" }

return true
pass()
}

return ::inner
}

/**
Expand All @@ -176,49 +157,40 @@ suspend fun allowedChannel(
* @param allowed List of allowed guild IDs
* @param banned List of banned guild IDs
*/
suspend fun allowedGuild(
fun allowedGuild(
allowed: List<Snowflake>,
banned: List<Snowflake>
): suspend (MessageCreateEvent) -> Boolean {
): Check<MessageCreateEvent> = {
val logger = KotlinLogging.logger { }
val guild = guildFor(event)

suspend fun inner(event: MessageCreateEvent): Boolean {
val guild = guildFor(event)
if (guild == null) {
logger.debug { "Passing: Event is not guild-related" }

if (guild == null) {
logger.debug { "Passing: Event is not guild-related" }
pass()
} else if (allowed.isNotEmpty()) {
if (allowed.contains(guild.id)) {
logger.debug { "Passing: Event happened in an allowed guild" }

return true
}
pass()
} else {
logger.debug { "Failing: Event did not happen in an allowed guild" }

if (allowed.isNotEmpty()) {
return if (allowed.contains(guild.id)) {
logger.debug { "Passing: Event happened in an allowed guild" }

true
} else {
logger.debug { "Failing: Event did not happen in an allowed guild" }

false
}
fail()
}
} else if (banned.isNotEmpty()) {
if (!banned.contains(guild.id)) {
logger.debug { "Passing: Event did not happen in a banned guild" }

if (banned.isNotEmpty()) {
return if (!banned.contains(guild.id)) {
logger.debug { "Passing: Event did not happen in a banned guild" }
pass()
} else {
logger.debug { "Failing: Event happened in a banned guild" }

true
} else {
logger.debug { "Failing: Event happened in a banned guild" }

false
}
fail()
}

} else {
logger.debug { "Passing: No allowed or banned guilds configured" }

return true
pass()
}

return ::inner
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.kotlindiscord.kord.extensions.modules.extra.mappings

import com.kotlindiscord.kord.extensions.checks.and
import com.kotlindiscord.kord.extensions.checks.types.Check
import com.kotlindiscord.kord.extensions.commands.MessageCommandContext
import com.kotlindiscord.kord.extensions.commands.parser.Arguments
import com.kotlindiscord.kord.extensions.extensions.Extension
Expand All @@ -19,7 +21,6 @@ import com.kotlindiscord.kord.extensions.pagination.pages.Page
import com.kotlindiscord.kord.extensions.pagination.pages.Pages
import com.kotlindiscord.kord.extensions.utils.respond
import dev.kord.core.behavior.channel.withTyping
import dev.kord.core.event.message.MessageCreateEvent
import me.shedaniel.linkie.*
import me.shedaniel.linkie.namespaces.*
import me.shedaniel.linkie.utils.MappingsQuery
Expand Down Expand Up @@ -1188,16 +1189,18 @@ class MappingsExtension : Extension() {

private suspend fun getTimeout() = builder.config.getTimeout()

private suspend fun customChecks(command: String, namespace: Namespace): suspend (MessageCreateEvent) -> Boolean {
val allChecks = builder.commandChecks.map { it.invoke(command) }
val allNamespaceChecks = builder.namespaceChecks.map { it.invoke(namespace) }
private suspend fun customChecks(command: String, namespace: Namespace): Check<*> {
val allChecks = builder.commandChecks.map { it.invoke(command) }.toMutableList()
val allNamespaceChecks = builder.namespaceChecks.map { it.invoke(namespace) }.toMutableList()

suspend fun inner(event: MessageCreateEvent): Boolean =
allChecks.all { it.invoke(event) }.and(
allNamespaceChecks.all { it.invoke(event) }
)
var rootCheck = allChecks.removeFirstOrNull()
?: allNamespaceChecks.removeFirstOrNull()
?: return { pass() }

allChecks.forEach { rootCheck = rootCheck and it }
allNamespaceChecks.forEach { rootCheck = rootCheck and it }

return ::inner
return rootCheck
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kotlindiscord.kord.extensions.modules.extra.mappings.builders

import com.kotlindiscord.kord.extensions.checks.types.Check
import com.kotlindiscord.kord.extensions.modules.extra.mappings.configuration.MappingsConfigAdapter
import com.kotlindiscord.kord.extensions.modules.extra.mappings.configuration.TomlMappingsConfig
import dev.kord.core.event.message.MessageCreateEvent
Expand All @@ -11,18 +12,18 @@ class ExtMappingsBuilder {
var config: MappingsConfigAdapter = TomlMappingsConfig()

/** List of checks to apply against the name of the command. **/
val commandChecks: MutableList<suspend (String) -> (suspend (MessageCreateEvent) -> Boolean)> = mutableListOf()
val commandChecks: MutableList<suspend (String) -> Check<*>> = mutableListOf()

/** List of checks to apply against the namespace corresponding with the command. **/
val namespaceChecks: MutableList<suspend (Namespace) -> (suspend (MessageCreateEvent) -> Boolean)> = mutableListOf()
val namespaceChecks: MutableList<suspend (Namespace) -> Check<*>> = mutableListOf()

/** Register a check that applies against the name of a command, and its message creation event. **/
fun commandCheck(check: suspend (String) -> (suspend (MessageCreateEvent) -> Boolean)) {
commandChecks.add(check)
fun commandCheck(check: suspend (String) -> Check<MessageCreateEvent>) {
commandChecks.add(check as (suspend (String) -> Check<*>))
}

/** Register a check that applies against the mappings namespace for a command, and its message creation event. **/
fun namespaceCheck(check: suspend (Namespace) -> (suspend (MessageCreateEvent) -> Boolean)) {
namespaceChecks.add(check)
fun namespaceCheck(check: suspend (Namespace) -> Check<MessageCreateEvent>) {
namespaceChecks.add(check as (suspend (Namespace) -> Check<*>))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.kotlindiscord.kord.extensions.test.bot

import com.kotlindiscord.kord.extensions.ExtensibleBot
import com.kotlindiscord.kord.extensions.checks.isNotbot
import com.kotlindiscord.kord.extensions.modules.extra.mappings.extMappings
import com.kotlindiscord.kord.extensions.utils.env
import me.shedaniel.linkie.namespaces.YarnNamespace
import org.koin.core.logger.Level

suspend fun main() {
val bot = ExtensibleBot(env("TOKEN")!!) {
koinLogLevel = Level.DEBUG

messageCommands {
check(isNotbot)
}

slashCommands {
enabled = true
}

extensions {
extMappings {
namespaceCheck { namespace ->
{
if (namespace == YarnNamespace) {
pass()
} else {
fail("Yarn only, ya dummy.")
}
}
}
}
}
}

bot.start()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
junit.jupiter.execution.parallel.enabled=true
Loading

0 comments on commit dbe2ad2

Please sign in to comment.