From 16a6732b4cb8056ca57ba817b5bbf2ac0252a1bd Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 2 Nov 2023 07:51:01 +0000 Subject: [PATCH 1/2] refactor(remove-docs): Remove docs as they are unfinished and should be in a separate branch --- .../com/learnspigot/bot/docs/DocCommand.kt | 72 ----- .../bot/docs/DocCommandListener.kt | 9 - .../com/learnspigot/bot/docs/DocRegistry.kt | 290 ------------------ 3 files changed, 371 deletions(-) delete mode 100644 src/main/kotlin/com/learnspigot/bot/docs/DocCommand.kt delete mode 100644 src/main/kotlin/com/learnspigot/bot/docs/DocCommandListener.kt delete mode 100644 src/main/kotlin/com/learnspigot/bot/docs/DocRegistry.kt diff --git a/src/main/kotlin/com/learnspigot/bot/docs/DocCommand.kt b/src/main/kotlin/com/learnspigot/bot/docs/DocCommand.kt deleted file mode 100644 index c90bdad..0000000 --- a/src/main/kotlin/com/learnspigot/bot/docs/DocCommand.kt +++ /dev/null @@ -1,72 +0,0 @@ -package com.learnspigot.bot.docs - -import com.google.gson.Gson -import com.learnspigot.bot.Environment -import com.learnspigot.bot.util.embed -import gg.flyte.neptune.annotation.Command -import gg.flyte.neptune.annotation.Description -import gg.flyte.neptune.annotation.Inject -import gg.flyte.neptune.annotation.Optional -import net.dv8tion.jda.api.EmbedBuilder -import net.dv8tion.jda.api.Permission -import net.dv8tion.jda.api.entities.User -import net.dv8tion.jda.api.entities.channel.Channel -import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent -import net.dv8tion.jda.api.interactions.components.buttons.Button -import java.net.URI -import java.net.http.HttpClient -import java.net.http.HttpRequest -import java.net.http.HttpResponse -import kotlin.math.acosh - -class DocCommand { - -// @Inject -// private lateinit var docRegistry: DocRegistry - -// fun onDocCommand( -// event: SlashCommandInteractionEvent, -// @Description("The element you want to search") query: String -// ) { -// val result: DocRegistry.QueryResult? = docRegistry.search(query) -// if (result == null) { -// event.reply("No element on the Spigot Javadocs was found from your query. You can search classes or specific methods. Some example search terms: 'Player', 'Player#setHealth', 'setHealth'.").setEphemeral(true).queue() -// return -// } -// -// when (result) { -// is DocRegistry.ClassQueryResult -> { -// val extendsString = result.classExtends.joinToString(", ") -// val implementsString = result.classImplements.joinToString(", ") -// -// val extendsImplements = buildString { -// if (extendsString.isNotEmpty()) { -// append("extends $extendsString") -// } -// if (implementsString.isNotEmpty()) { -// if (extendsString.isNotEmpty()) { -// append(" ") -// } -// append("implements $implementsString") -// } -// } -// -// event.replyEmbeds( -// embed() -// .setDescription(result.classTitle + " " + extendsImplements) -// .addField("", result.description, false) -// .setFooter("Excerpt from the Spigot Javadocs", "https://i.imgur.com/zMyhPb7.png") -// .build() -// ).addActionRow( -// Button.link("https://stephen.gg", "Open Javadoc") -// -// ).queue() -// } -// -// is DocRegistry.MethodQueryResult -> { -// -// } -// } -// } - -} \ No newline at end of file diff --git a/src/main/kotlin/com/learnspigot/bot/docs/DocCommandListener.kt b/src/main/kotlin/com/learnspigot/bot/docs/DocCommandListener.kt deleted file mode 100644 index f75e097..0000000 --- a/src/main/kotlin/com/learnspigot/bot/docs/DocCommandListener.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.learnspigot.bot.docs - -import net.dv8tion.jda.api.hooks.ListenerAdapter - -class DocCommandListener : ListenerAdapter() { - - - -} \ No newline at end of file diff --git a/src/main/kotlin/com/learnspigot/bot/docs/DocRegistry.kt b/src/main/kotlin/com/learnspigot/bot/docs/DocRegistry.kt deleted file mode 100644 index 7d3fb74..0000000 --- a/src/main/kotlin/com/learnspigot/bot/docs/DocRegistry.kt +++ /dev/null @@ -1,290 +0,0 @@ -package com.learnspigot.bot.docs - -import com.learnspigot.bot.util.Mongo -import com.mongodb.client.model.Filters -import com.mongodb.client.model.ReplaceOptions -import org.bson.Document -import java.io.BufferedReader -import java.io.InputStreamReader -import java.net.URL -import java.time.Duration -import java.time.Instant -import java.util.* - -// Code written by Josh! (Only imported by Stephen) -// Sidenote: If the Javadocs structure changes, this whole system will go up in flames -class DocRegistry { - - private val baseURL = "https://hub.spigotmc.org/javadocs/bukkit" - - private var index: Map = TreeMap(String.CASE_INSENSITIVE_ORDER) - - init { - populateIndexFromMongo() - } - - fun search(query: String): QueryResult? { - index[query]?.let { - println("Found $it for search $query") - val result = query(it, if ("#" in query) query.split("#")[1] else null) - println(result) - return result - } - println("No result found") - return null - } - - private fun query(path: String, id: String?): QueryResult { - val connection = URL("$baseURL/$path").openConnection() - - BufferedReader(InputStreamReader(connection.getInputStream())).use { reader -> - val content = reader.readText() - - val split = content.split("
")[1].replace( - "", - "" - ).split("") - - val classTitle = split[0] - - val split2 = split[1].replace("", "") - - val classExtends = if ("enum" in classTitle || "extends" !in split2) null else - split2.split(if (split2.contains("implements ")) "implements " else "
")[0].split(", ").map { - val split3 = it.split("")[1].split("")[0] - return@map "[$name]($baseURL/${index[name]})" - } - - var classImplements: List? = null - - if (split2.contains("implements ")) { - classImplements = split2.split("implements ")[1].split("")[0].split(", ").map { - val split3 = it.split("")[1].split("")[0] - return@map "[$name]($baseURL/${index[name]})" - } - } - - if (id == null) { - val classDescription = content.split("")[1].replace("
", "").substring(1) - .split("
")[0].replace("\n", " ").replace("

", "\n\n").split(" ") - - if (part.size < 2) return@map part[0] - - return@map "[${part[0]}]($baseURL/${index[part[0]]})" + part[1].split("")[1] - }.joinToString(" ") - - return ClassQueryResult( - classTitle, - classExtends ?: listOf(), - classImplements ?: listOf(), - classDescription - ) - } - - val method = content.split("

")[1].split("
")[0] - - var methodTitle = - method.split("
")[1].split("
")[0] - .replace("", "") - .replace("", "") - .replace("", "") - .replace("", "") - .replace(" ", " ") - - var methodDescription: String? = null - - val split3 = path.substring(path.lastIndexOf("/") + 1).split("#") - - if ("enum" in methodTitle) { - methodTitle = - methodTitle.split("")[0] + "${split3[0].replace(".html", "")} ${split3[1]}" - } else { - methodTitle = - methodTitle.replace("", "").replace("", "") - .replace("", "") - val description = method.split("
")[1].split("
")[0].split(" 1) { - val desc2 = description[1].split("\">") - methodDescription = description[0] + "[${ - desc2[0].replace( - "#", - "" - ) - }]($baseURL/${path.split("#")[0]}${desc2[0]})" + desc2[1].split("")[1] - } else { - methodDescription = description[0] - } - } - - var methodReturn: String? = null - - if (method.contains("
Returns:
")) { - methodReturn = - method.split("
Returns:
\n")[1].split("\n")[0].replace("
", "").replace("
", "") - } - - val methodParameters = mutableMapOf() - - if (method.contains("
Parameters:
")) { - var stop = false - method.split("
Parameters:
\n")[1].split("\n")[0].split("
").forEach { - if (stop) return@forEach - val split4 = it.split(" - ") - if (split4.size > 1) methodParameters += split4[0].replace("", "") - .replace("", "") to split4[1].split("
")[0] - if (it.contains("
Throws:
")) { - stop = true - return@forEach - } - } - } - - val methodThrows = mutableMapOf() - - if (method.contains("
Throws:
")) { - method.split("
Throws:
\n")[1].split("\n")[0].split("
").forEach { - val split4 = it.split(" - ") - if (split4.size > 1) { - val link = split4[1].split("
")[0].replace("\n", " ").split("", "") - .replace("", "") - .split("\">")[1].split("")[0] to "${link[0]}[${link[1].split("\">")[0].replace("#", "")}]($baseURL/${path.split("#")[0]}${link[1].split("\">")[0]})" - } - } - } - - - return MethodQueryResult( - classTitle, - classExtends ?: listOf(), - classImplements ?: listOf(), - methodTitle, - methodDescription, - methodReturn, - methodParameters, - methodThrows - ) - } - } - - data class ClassQueryResult( - override val classTitle: String, - override val classExtends: List, - override val classImplements: List, - val description: String - ) : QueryResult(classTitle, classExtends, classImplements) - - data class MethodQueryResult( - override val classTitle: String, - override val classExtends: List, - override val classImplements: List, - val methodTitle: String, - val methodDescription: String?, - val methodReturn: String?, - val methodParameters: Map, - val methodThrows: Map, - ) : QueryResult(classTitle, classExtends, classImplements) - - open class QueryResult( - open val classTitle: String, - open val classExtends: List, - open val classImplements: List - ) - - private fun populateIndexFromMongo() { - val start = Instant.now() - - Mongo.docsCollection.find().first()?.let { - index = it["index"] as? Map ?: emptyMap() - } - - println("Indexed ${index.size} entries from MongoDB in ${Duration.between(start, Instant.now()).seconds} seconds") - } - - private fun populateIndex() { - val connection = URL("$baseURL/index-all.html").openConnection() - - val start = Instant.now() - - BufferedReader(InputStreamReader(connection.getInputStream())).use { reader -> - reader.readLines().forEach { - if (!(it.startsWith("
"))) return@forEach - val split = it.split("
")[1].split("") - - val link = split[0] - var name = split2[0] - val description = split2[1] - - if (description.lowercase().contains("in class") || description.lowercase() - .contains("in interface") || description.lowercase().contains("in enum") - ) { - // It's part of a class so we should turn the name/key into Class#thing - name = link.substring(link.lastIndexOf("/") + 1).replace(".html", "").split("(")[0] - } - - index += name to link - - println("$name ($link)") - - if (split2.size > 1) { - if (description.lowercase().contains("method in interface")) { - val interfaceLink = description.split(" - val extraName = subinterface.substring(subinterface.lastIndexOf("/") + 1) - .replace(".html", "") + "#${name.split("#")[1]}" - println("INTERFACE DUPLICATE $extraName ($link)") - index += extraName to link - } - } - } - } - } - - println("Finished indexing in ${Duration.between(start, Instant.now()).seconds} seconds") - - Document().apply { - this["version"] = "1.20.1" - this["index"] = index - }.also { - Mongo.docsCollection.replaceOne(Filters.eq("version", "1.20.1"), it, ReplaceOptions().upsert(true)) - } - } - - private fun getAllKnownSubinterfaces(interfaceLink: String): List { - val connection = URL("$baseURL/$interfaceLink").openConnection() - - val links = mutableListOf() - - BufferedReader(InputStreamReader(connection.getInputStream())).use { reader -> - var inAllKnownSubinterfaces = false - - reader.readLines().forEach { line -> - if (line == "
All Known Subinterfaces:
") { - inAllKnownSubinterfaces = true - return@forEach - } - - if (!inAllKnownSubinterfaces) return@forEach - - line.split(", ").forEach { - links += getSubinterfaceLinkFromString(it) - } - - return links - } - } - - return links - } - - private fun getSubinterfaceLinkFromString(string: String) = - string.split(" Date: Thu, 2 Nov 2023 07:55:54 +0000 Subject: [PATCH 2/2] feat(remove-docs): Remove commented out instantiate method --- src/main/kotlin/com/learnspigot/bot/Bot.kt | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/kotlin/com/learnspigot/bot/Bot.kt b/src/main/kotlin/com/learnspigot/bot/Bot.kt index da5a49b..42cc641 100644 --- a/src/main/kotlin/com/learnspigot/bot/Bot.kt +++ b/src/main/kotlin/com/learnspigot/bot/Bot.kt @@ -82,11 +82,6 @@ class Bot { return KnowledgebaseRegistry() } -// @Instantiate -// fun docRegistry(): DocRegistry { -// return DocRegistry() -// } - companion object { lateinit var jda: JDA }