From 9f12ef1493c8eb9f390dbf624ed57c6f78c71f80 Mon Sep 17 00:00:00 2001 From: Devoxin Date: Tue, 17 Oct 2023 01:28:51 +0100 Subject: [PATCH] Optimisations and a few new features. --- build.gradle | 4 ++-- .../kotlin/me/devoxin/flight/api/CommandClientBuilder.kt | 2 ++ .../me/devoxin/flight/api/arguments/types/Emoji.kt | 4 +++- .../me/devoxin/flight/api/arguments/types/Invite.kt | 5 ++++- .../me/devoxin/flight/internal/arguments/ArgParser.kt | 9 +++++---- .../me/devoxin/flight/internal/parsers/InviteParser.kt | 2 +- .../me/devoxin/flight/internal/parsers/MemberParser.kt | 8 ++------ .../me/devoxin/flight/internal/parsers/RoleParser.kt | 6 +----- .../devoxin/flight/internal/parsers/SnowflakeParser.kt | 1 + .../devoxin/flight/internal/parsers/TextChannelParser.kt | 8 ++------ .../flight/internal/parsers/VoiceChannelParser.kt | 8 ++------ 11 files changed, 25 insertions(+), 32 deletions(-) diff --git a/build.gradle b/build.gradle index 790ed78..6797e6d 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group 'me.devoxin' -version '3.0.5' +version '3.0.6' repositories { maven { @@ -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" diff --git a/src/main/kotlin/me/devoxin/flight/api/CommandClientBuilder.kt b/src/main/kotlin/me/devoxin/flight/api/CommandClientBuilder.kt index 1b94fd7..5116a67 100644 --- a/src/main/kotlin/me/devoxin/flight/api/CommandClientBuilder.kt +++ b/src/main/kotlin/me/devoxin/flight/api/CommandClientBuilder.kt @@ -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 diff --git a/src/main/kotlin/me/devoxin/flight/api/arguments/types/Emoji.kt b/src/main/kotlin/me/devoxin/flight/api/arguments/types/Emoji.kt index 3c562fe..6f05c9b 100644 --- a/src/main/kotlin/me/devoxin/flight/api/arguments/types/Emoji.kt +++ b/src/main/kotlin/me/devoxin/flight/api/arguments/types/Emoji.kt @@ -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) } } diff --git a/src/main/kotlin/me/devoxin/flight/api/arguments/types/Invite.kt b/src/main/kotlin/me/devoxin/flight/api/arguments/types/Invite.kt index 73031a7..e9b08ef 100644 --- a/src/main/kotlin/me/devoxin/flight/api/arguments/types/Invite.kt +++ b/src/main/kotlin/me/devoxin/flight/api/arguments/types/Invite.kt @@ -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 @@ -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() } diff --git a/src/main/kotlin/me/devoxin/flight/internal/arguments/ArgParser.kt b/src/main/kotlin/me/devoxin/flight/internal/arguments/ArgParser.kt index b0688f9..8cdc75b 100644 --- a/src/main/kotlin/me/devoxin/flight/internal/arguments/ArgParser.kt +++ b/src/main/kotlin/me/devoxin/flight/internal/arguments/ArgParser.kt @@ -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) @@ -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) } @@ -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)) } diff --git a/src/main/kotlin/me/devoxin/flight/internal/parsers/InviteParser.kt b/src/main/kotlin/me/devoxin/flight/internal/parsers/InviteParser.kt index 8e654c1..fcd8501 100644 --- a/src/main/kotlin/me/devoxin/flight/internal/parsers/InviteParser.kt +++ b/src/main/kotlin/me/devoxin/flight/internal/parsers/InviteParser.kt @@ -17,6 +17,6 @@ class InviteParser : Parser { } 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() } } diff --git a/src/main/kotlin/me/devoxin/flight/internal/parsers/MemberParser.kt b/src/main/kotlin/me/devoxin/flight/internal/parsers/MemberParser.kt index baa5a6a..ee71030 100644 --- a/src/main/kotlin/me/devoxin/flight/internal/parsers/MemberParser.kt +++ b/src/main/kotlin/me/devoxin/flight/internal/parsers/MemberParser.kt @@ -6,7 +6,7 @@ import java.util.* class MemberParser : Parser { override fun parse(ctx: MessageContext, param: String): Optional { - 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) @@ -14,13 +14,9 @@ class MemberParser : Parser { 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 - } } diff --git a/src/main/kotlin/me/devoxin/flight/internal/parsers/RoleParser.kt b/src/main/kotlin/me/devoxin/flight/internal/parsers/RoleParser.kt index d797058..49bd162 100644 --- a/src/main/kotlin/me/devoxin/flight/internal/parsers/RoleParser.kt +++ b/src/main/kotlin/me/devoxin/flight/internal/parsers/RoleParser.kt @@ -6,15 +6,11 @@ import java.util.* class RoleParser : Parser { override fun parse(ctx: MessageContext, param: String): Optional { - 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 - } } diff --git a/src/main/kotlin/me/devoxin/flight/internal/parsers/SnowflakeParser.kt b/src/main/kotlin/me/devoxin/flight/internal/parsers/SnowflakeParser.kt index 1f9b9eb..8ac033d 100644 --- a/src/main/kotlin/me/devoxin/flight/internal/parsers/SnowflakeParser.kt +++ b/src/main/kotlin/me/devoxin/flight/internal/parsers/SnowflakeParser.kt @@ -17,6 +17,7 @@ class SnowflakeParser : Parser { } companion object { + internal val INSTANCE = SnowflakeParser() val SNOWFLAKE_PATTERN = "^(?:<(?:@!?|@&|#)(?\\d{17,21})>|(?\\d{17,21}))$".toPattern() } } diff --git a/src/main/kotlin/me/devoxin/flight/internal/parsers/TextChannelParser.kt b/src/main/kotlin/me/devoxin/flight/internal/parsers/TextChannelParser.kt index 247fd9f..9393b52 100644 --- a/src/main/kotlin/me/devoxin/flight/internal/parsers/TextChannelParser.kt +++ b/src/main/kotlin/me/devoxin/flight/internal/parsers/TextChannelParser.kt @@ -6,15 +6,11 @@ import java.util.* class TextChannelParser : Parser { override fun parse(ctx: MessageContext, param: String): Optional { - 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 - } } diff --git a/src/main/kotlin/me/devoxin/flight/internal/parsers/VoiceChannelParser.kt b/src/main/kotlin/me/devoxin/flight/internal/parsers/VoiceChannelParser.kt index cb80aa0..aefc2a4 100644 --- a/src/main/kotlin/me/devoxin/flight/internal/parsers/VoiceChannelParser.kt +++ b/src/main/kotlin/me/devoxin/flight/internal/parsers/VoiceChannelParser.kt @@ -6,15 +6,11 @@ import java.util.* class VoiceChannelParser : Parser { override fun parse(ctx: MessageContext, param: String): Optional { - 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 - } }