Skip to content

Commit

Permalink
Add top channel checks
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Aug 2, 2021
1 parent de5fcf5 commit 1621f2b
Showing 1 changed file with 155 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
@file:Suppress("RedundantSuspendModifier")

package com.kotlindiscord.kord.extensions.checks

import com.kotlindiscord.kord.extensions.checks.types.Check
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.channel.ChannelBehavior
import dev.kord.core.entity.channel.thread.ThreadChannel
import dev.kord.core.event.Event
import mu.KotlinLogging

// region: Entity DSL versions

/**
* Check asserting that an [Event] fired within a given channel. If the event fired within a thread,
* it checks the thread's parent channel instead.
*
* Only events that can reasonably be associated with a single channel are supported. Please raise
* an issue if an event you expected to be supported, isn't.
*
* @param builder Lambda returning the channel to compare to.
*/
public fun inTopChannel(builder: suspend () -> ChannelBehavior): Check<*> = {
val logger = KotlinLogging.logger("com.kotlindiscord.kord.extensions.checks.inChannel")

// TODO: When we can get the thread parent, checks will need looking at again
// val thread = threadFor(event)
//
// val eventChannel = when(thread) {
// is NewsChannelThread -> thread
// is TextChannelThread -> thread
//
// else -> channelFor(event)
// }

val eventChannel = topChannelFor(event)

if (eventChannel == null) {
logger.nullChannel(event)

fail()
} else {
val channel = builder()

if (eventChannel.id == channel.id) {
logger.passed()

pass()
} else {
logger.failed("Channel $eventChannel is not the same as channel $channel")

fail(
translate(
"checks.inChannel.failed",
replacements = arrayOf(channel.mention),
)
)
}
}
}

/**
* Check asserting that an [Event] did **not** fire within a given channel. If the event fired within a thread,
* it checks the thread's parent channel instead.
*
* Only events that can reasonably be associated with a single channel are supported. Please raise
* an issue if an event you expected to be supported, isn't.
*
* @param builder Lambda returning the channel to compare to.
*/
public fun notInTopChannel(builder: suspend () -> ChannelBehavior): Check<*> = {
val logger = KotlinLogging.logger("com.kotlindiscord.kord.extensions.checks.notInChannel")
val eventChannel = topChannelFor(event)

if (eventChannel == null) {
logger.nullChannel(event)

fail()
} else {
val channel = builder()

if (eventChannel.id != channel.id) {
logger.passed()

pass()
} else {
logger.failed("Channel $eventChannel is the same as channel $channel")

fail(
translate(
"checks.notInChannel.failed",
replacements = arrayOf(channel.mention)
)
)
}
}
}

// endregion

// region: Snowflake versions

/**
* Check asserting that an [Event] fired within a given channel. If the event fired within a thread,
* it checks the thread's parent channel instead.
*
* Only events that can reasonably be associated with a single channel are supported. Please raise
* an issue if an event you expected to be supported, isn't.
*
* @param id Channel snowflake to compare to.
*/
public fun inTopChannel(id: Snowflake): Check<*> = {
val logger = KotlinLogging.logger("com.kotlindiscord.kord.extensions.checks.inChannel")
var channel = event.kord.getChannel(id)

if (channel is ThreadChannel) {
channel = channel.parent.asChannel()
}

if (channel == null) {
logger.noChannelId(id)

fail()
} else {
inChannel { channel }()
}
}

/**
* Check asserting that an [Event] did **not** fire within a given channel. If the event fired within a thread,
* it checks the thread's parent channel instead.
*
* Only events that can reasonably be associated with a single channel are supported. Please raise
* an issue if an event you expected to be supported, isn't.
*
* @param id Channel snowflake to compare to.
*/
public fun notInTopChannel(id: Snowflake): Check<*> = {
val logger = KotlinLogging.logger("com.kotlindiscord.kord.extensions.checks.notInChannel")
var channel = event.kord.getChannel(id)

if (channel is ThreadChannel) {
channel = channel.parent.asChannel()
}

if (channel == null) {
logger.noChannelId(id)

fail()
} else {
notInChannel { channel }()
}
}

// endregion

0 comments on commit 1621f2b

Please sign in to comment.