Skip to content

Commit

Permalink
Optimisations and a few new features.
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Oct 17, 2023
1 parent f5ac055 commit 9f12ef1
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 32 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'me.devoxin'
version '3.0.5'
version '3.0.6'

repositories {
maven {
Expand All @@ -20,7 +20,7 @@ repositories {
dependencies {
def kotlinVersion = '1.7.22'
def coroutinesVersion = '1.6.4'
def jdaVersion = '5.0.0-beta.12'
def jdaVersion = '5.0.0-beta.15'

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$coroutinesVersion"
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/me/devoxin/flight/api/CommandClientBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import me.devoxin.flight.api.hooks.CommandEventAdapter
import me.devoxin.flight.api.hooks.DefaultCommandEventAdapter
import me.devoxin.flight.internal.arguments.ArgParser
import me.devoxin.flight.internal.parsers.*
import me.devoxin.flight.internal.parsers.TextChannelParser
import me.devoxin.flight.internal.parsers.VoiceChannelParser
import net.dv8tion.jda.api.entities.Member
import net.dv8tion.jda.api.entities.Role
import net.dv8tion.jda.api.entities.User
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package me.devoxin.flight.api.arguments.types

import net.dv8tion.jda.api.entities.emoji.CustomEmoji

class Emoji(val name: String, val id: Long, val animated: Boolean) {
val url: String
get() {
val extension = if (animated) "gif" else "png"
return "https://cdn.discordapp.com/emojis/$id.$extension"
return CustomEmoji.ICON_URL.format(id, extension)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.devoxin.flight.api.arguments.types

import kotlinx.coroutines.future.await
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.entities.Invite

Expand All @@ -8,5 +9,7 @@ class Invite(
val url: String,
val code: String
) {
fun resolve() = Invite.resolve(jda, code)
fun resolve(withCounts: Boolean = false) = Invite.resolve(jda, code, withCounts)

suspend fun resolveAsync(withCounts: Boolean = false) = Invite.resolve(jda, code, withCounts).submit().await()
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ArgParser(
var quoting = false
var escaping = false

loop@ while (iterator.hasNext()) {
while (iterator.hasNext()) {
val char = iterator.nextChar()
original.append(char)

Expand All @@ -41,8 +41,8 @@ class ArgParser(
!quoting && char == '"' -> quoting = true // accept other quote chars
!quoting && char == delimiter -> {
// Maybe this should throw? !test blah -- Extraneous whitespace is ignored.
if (argument.isEmpty()) continue@loop
else break@loop
if (argument.isEmpty()) continue
else break
}
else -> argument.append(char)
}
Expand All @@ -51,8 +51,9 @@ class ArgParser(
argument.append('"')

val remainingArgs = StringBuilder().apply {
iterator.forEachRemaining { this.append(it) }
iterator.forEachRemaining(this::append)
}

args = remainingArgs.toString().split(delimiter).toMutableList()
return Pair(argument.toString(), original.split(delimiterStr))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class InviteParser : Parser<Invite> {
}

companion object {
val INVITE_PATTERN = "discord(?:(?:app)?\\.com/invite|\\.gg)/([a-zA-Z\\d]{1,16})".toPattern()
val INVITE_PATTERN = "(?:https?://)?discord(?:(?:app)?\\.com/invite|\\.gg)/([a-zA-Z\\d]{1,16})".toPattern()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ import java.util.*

class MemberParser : Parser<Member> {
override fun parse(ctx: MessageContext, param: String): Optional<Member> {
val snowflake = snowflakeParser.parse(ctx, param).takeIf { it.isPresent }?.get()?.resolved
val snowflake = SnowflakeParser.INSTANCE.parse(ctx, param).takeIf { it.isPresent }?.get()?.resolved

val member = when {
snowflake != null -> ctx.message.mentions.members.firstOrNull { it.user.idLong == snowflake } ?: ctx.guild?.getMemberById(snowflake)
param.length > 5 && param[param.length - 5] == '#' -> {
val tag = param.split("#")
ctx.guild?.memberCache?.find { (it.user.discriminator != "0000" && it.user.name == tag[0]) || it.user.asTag == param }
}
else -> ctx.guild?.getMembersByName(param, false)?.firstOrNull()
else -> ctx.guild?.memberCache?.firstOrNull { it.user.name == param }
}

return Optional.ofNullable(member)
}

companion object {
private val snowflakeParser = SnowflakeParser() // We can reuse this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ import java.util.*

class RoleParser : Parser<Role> {
override fun parse(ctx: MessageContext, param: String): Optional<Role> {
val snowflake = snowflakeParser.parse(ctx, param).takeIf { it.isPresent }?.get()?.resolved
val snowflake = SnowflakeParser.INSTANCE.parse(ctx, param).takeIf { it.isPresent }?.get()?.resolved

return when {
snowflake != null -> Optional.ofNullable(ctx.guild?.getRoleById(snowflake))
else -> Optional.ofNullable(ctx.guild?.roleCache?.firstOrNull { it.name == param })
}
}

companion object {
private val snowflakeParser = SnowflakeParser() // We can reuse this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SnowflakeParser : Parser<Snowflake> {
}

companion object {
internal val INSTANCE = SnowflakeParser()
val SNOWFLAKE_PATTERN = "^(?:<(?:@!?|@&|#)(?<sid>\\d{17,21})>|(?<id>\\d{17,21}))$".toPattern()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ import java.util.*

class TextChannelParser : Parser<TextChannel> {
override fun parse(ctx: MessageContext, param: String): Optional<TextChannel> {
val snowflake = snowflakeParser.parse(ctx, param).takeIf { it.isPresent }?.get()?.resolved
val snowflake = SnowflakeParser.INSTANCE.parse(ctx, param).takeIf { it.isPresent }?.get()?.resolved

return when {
snowflake != null -> Optional.ofNullable(ctx.guild?.getTextChannelById(snowflake))
else -> Optional.ofNullable(ctx.guild?.textChannels?.firstOrNull { it.name == param })
else -> Optional.ofNullable(ctx.guild?.textChannelCache?.firstOrNull { it.name == param })
}
}

companion object {
val snowflakeParser = SnowflakeParser() // We can reuse this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ import java.util.*

class VoiceChannelParser : Parser<VoiceChannel> {
override fun parse(ctx: MessageContext, param: String): Optional<VoiceChannel> {
val snowflake = snowflakeParser.parse(ctx, param).takeIf { it.isPresent }?.get()?.resolved
val snowflake = SnowflakeParser.INSTANCE.parse(ctx, param).takeIf { it.isPresent }?.get()?.resolved

return when {
snowflake != null -> Optional.ofNullable(ctx.guild?.getVoiceChannelById(snowflake))
else -> Optional.ofNullable(ctx.guild?.voiceChannels?.firstOrNull { it.name == param })
else -> Optional.ofNullable(ctx.guild?.voiceChannelCache?.firstOrNull { it.name == param })
}
}

companion object {
private val snowflakeParser = SnowflakeParser() // We can reuse this
}
}

0 comments on commit 9f12ef1

Please sign in to comment.