This repository has been archived by the owner on Apr 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite prefixes to be more composable
- Loading branch information
Showing
23 changed files
with
566 additions
and
74 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,4 +1,10 @@ | ||
#0.1.1 | ||
# 0.2.0 | ||
|
||
## Changes | ||
|
||
* Prefixes have been reworked considerably. #3 | ||
|
||
# 0.1.1 | ||
|
||
## Fixes | ||
|
||
|
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
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
29 changes: 0 additions & 29 deletions
29
...me-kord/src/main/kotlin/com/gitlab/kordlib/kordx/commands/kord/model/prefix/KordPrefix.kt
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
...ord/src/main/kotlin/com/gitlab/kordlib/kordx/commands/kord/model/prefix/KordPrefixRule.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,84 @@ | ||
package com.gitlab.kordlib.kordx.commands.kord.model.prefix | ||
|
||
import com.gitlab.kordlib.core.behavior.GuildBehavior | ||
import com.gitlab.kordlib.core.behavior.MemberBehavior | ||
import com.gitlab.kordlib.core.behavior.UserBehavior | ||
import com.gitlab.kordlib.core.behavior.channel.MessageChannelBehavior | ||
import com.gitlab.kordlib.core.entity.channel.Channel | ||
import com.gitlab.kordlib.core.event.message.MessageCreateEvent | ||
import com.gitlab.kordlib.kordx.commands.model.prefix.PrefixBuilder | ||
import com.gitlab.kordlib.kordx.commands.model.prefix.PrefixRule | ||
|
||
/** | ||
* Creates a [PrefixRule] that supplies][supplier] a prefix based on the [event's][MessageCreateEvent] guild. | ||
* | ||
* Events created outside of guilds (DMs) will automatically be denied. Invocations of the [supplier] that return | ||
* `null` will be considered as denied as well. | ||
*/ | ||
fun PrefixBuilder.guild( | ||
supplier: (guild: GuildBehavior) -> String? | ||
): PrefixRule<MessageCreateEvent> = PrefixRule { message, context -> | ||
val guild = context.message.guild ?: return@PrefixRule PrefixRule.Result.Denied | ||
val prefix = supplier(guild) ?: return@PrefixRule PrefixRule.Result.Denied | ||
if (message.startsWith(prefix)) PrefixRule.Result.Accepted(prefix) | ||
else PrefixRule.Result.Denied | ||
} | ||
|
||
/** | ||
* Creates a [PrefixRule] that supplies][supplier] a prefix based on the [event's][MessageCreateEvent] channel. | ||
* | ||
* Invocations of the [supplier] that return `null` will be considered as [denied][PrefixRule.Result.Denied]. | ||
*/ | ||
fun PrefixBuilder.channel( | ||
supplier: (channel: MessageChannelBehavior) -> String? | ||
): PrefixRule<MessageCreateEvent> = PrefixRule { message, context -> | ||
val channel = context.message.channel | ||
val prefix = supplier(channel) ?: return@PrefixRule PrefixRule.Result.Denied | ||
if (message.startsWith(prefix)) PrefixRule.Result.Accepted(prefix) | ||
else PrefixRule.Result.Denied | ||
} | ||
|
||
/** | ||
* Creates a [PrefixRule] that supplies][supplier] a prefix based on the [event's][MessageCreateEvent] channel. | ||
* | ||
* Events created outside of channels of type [T] will automatically be [denied][PrefixRule.Result.Denied]. | ||
* Invocations of the [supplier] that return `null` will be considered as [denied][PrefixRule.Result.Denied] as well. | ||
*/ | ||
@JvmName("channelReified") | ||
inline fun <reified T : Channel> PrefixBuilder.channel( | ||
noinline supplier: (channel: T) -> String? | ||
): PrefixRule<MessageCreateEvent> = PrefixRule { message, context -> | ||
val channel = context.message.getChannel() | ||
if (channel !is T) return@PrefixRule PrefixRule.Result.Denied | ||
val prefix = supplier(channel) ?: return@PrefixRule PrefixRule.Result.Denied | ||
if (message.startsWith(prefix)) PrefixRule.Result.Accepted(prefix) | ||
else PrefixRule.Result.Denied | ||
} | ||
|
||
/** | ||
* Creates a [PrefixRule] that supplies][supplier] a prefix based on the [event's][MessageCreateEvent] user. | ||
* | ||
* Invocations of the [supplier] that return `null` will be considered as [denied][PrefixRule.Result.Denied]. | ||
*/ | ||
fun PrefixBuilder.user( | ||
supplier: (user: UserBehavior) -> String? | ||
): PrefixRule<MessageCreateEvent> = PrefixRule { message, context -> | ||
val user = context.message.author ?: return@PrefixRule PrefixRule.Result.Denied | ||
val prefix = supplier(user) ?: return@PrefixRule PrefixRule.Result.Denied | ||
if (message.startsWith(prefix)) PrefixRule.Result.Accepted(prefix) | ||
else PrefixRule.Result.Denied | ||
} | ||
|
||
/** | ||
* Creates a [PrefixRule] that supplies][supplier] a prefix based on the [event's][MessageCreateEvent] member. | ||
* | ||
* Invocations of the [supplier] that return `null` will be considered as [denied][PrefixRule.Result.Denied]. | ||
*/ | ||
fun PrefixBuilder.member( | ||
supplier: (user: MemberBehavior) -> String? | ||
): PrefixRule<MessageCreateEvent> = PrefixRule { message, context -> | ||
val member = context.message.getAuthorAsMember() ?: return@PrefixRule PrefixRule.Result.Denied | ||
val prefix = supplier(member) ?: return@PrefixRule PrefixRule.Result.Denied | ||
if (message.startsWith(prefix)) PrefixRule.Result.Accepted(prefix) | ||
else PrefixRule.Result.Denied | ||
} |
29 changes: 29 additions & 0 deletions
29
.../src/main/kotlin/com/gitlab/kordlib/kordx/commands/kord/model/prefix/MentionPrefixRule.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,29 @@ | ||
package com.gitlab.kordlib.kordx.commands.kord.model.prefix | ||
|
||
import com.gitlab.kordlib.core.Kord | ||
import com.gitlab.kordlib.core.event.message.MessageCreateEvent | ||
import com.gitlab.kordlib.kordx.commands.model.prefix.PrefixBuilder | ||
import com.gitlab.kordlib.kordx.commands.model.prefix.PrefixRule | ||
import org.koin.core.get | ||
|
||
/** | ||
* Creates [PrefixRule] that accepts the bot's mention as a valid prefix. | ||
* | ||
* > Note that due to the nature of Discord's mentions requiring a space for them to be rendered in the client, | ||
* this rule will also require and consume a whitespace character directly after the mention. | ||
* If, for example, this rule is enabled and the given input is `"@Bot ping"` the matched prefix will be `"@Bot "`, | ||
* including the space. This also means that simply mentioning the bot `"@Bot"` will not be considered valid. | ||
*/ | ||
fun PrefixBuilder.mention(): PrefixRule<MessageCreateEvent> = MentionPrefixRule(get()) | ||
|
||
internal class MentionPrefixRule(kord: Kord) : PrefixRule<MessageCreateEvent> { | ||
|
||
private val regex = Regex("""<@!?${kord.selfId.value}>\s""") | ||
|
||
override suspend fun consume(message: String, context: MessageCreateEvent): PrefixRule.Result { | ||
val result = regex.find(message) ?: return PrefixRule.Result.Denied | ||
if (result.range.first != 0) return PrefixRule.Result.Denied | ||
return PrefixRule.Result.Accepted(message.substring(result.range)) | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.