-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
155 additions
and
0 deletions.
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/checks/TopChannelChecks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |