Skip to content

Commit

Permalink
Merge pull request #3 from Goksi/version-control
Browse files Browse the repository at this point in the history
Adding version control on startup
  • Loading branch information
Goksi authored Nov 8, 2022
2 parents 7e0c112 + 51382a1 commit c0ba199
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 7 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<artifactId>PteroBot</artifactId>
<groupId>tech.goksi</groupId>
<version>1.0-SNAPSHOT</version>
<version>1.0.3</version>
<packaging>jar</packaging>

<name>PteroBot</name>
Expand Down Expand Up @@ -34,6 +34,12 @@
</repositories>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/tech/goksi/pterobot/PteroBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import tech.goksi.pterobot.events.NodeStatusDelete
import tech.goksi.pterobot.manager.ConfigManager
import tech.goksi.pterobot.manager.EmbedManager
import tech.goksi.pterobot.util.Checks
import tech.goksi.pterobot.util.VersionCheck

private const val DEFAULT_NO_TOKEN_MSG = "YOUR TOKEN HERE"
private const val DEFAULT_NO_ID_MSG = "YOUR DISCORD SERVER ID HERE"
private const val DEFAULT_NO_URL_MSG = "YOUR URL HERE"
private const val DEFAULT_NO_API_KEY_MSG = "YOUR PTERODACTYL ADMIN CLIENT KEY HERE"

class PteroBot() {
class PteroBot {
private val logger by SLF4J
private val jda: JDA

Expand Down Expand Up @@ -109,6 +110,7 @@ class PteroBot() {
guild?.updateCommands()?.addCommands(commandData.buildData())?.queue()
jda.addEventListener(NodeStatusDelete())
commandData.registerListeners(jda)
VersionCheck.checkVersion()
listenStdin()
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/tech/goksi/pterobot/commands/Link.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEve
import net.dv8tion.jda.api.interactions.commands.OptionType
import net.dv8tion.jda.api.interactions.commands.build.OptionData
import tech.goksi.pterobot.commands.manager.abs.SimpleCommand
import tech.goksi.pterobot.manager.ConfigManager
import tech.goksi.pterobot.database.DataStorage
import tech.goksi.pterobot.entities.ApiKey
import tech.goksi.pterobot.entities.PteroMember
import tech.goksi.pterobot.manager.ConfigManager
import tech.goksi.pterobot.manager.EmbedManager
import tech.goksi.pterobot.manager.EmbedManager.toEmbed
import tech.goksi.pterobot.util.Checks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import tech.goksi.pterobot.manager.ConfigManager
import tech.goksi.pterobot.manager.EmbedManager
import tech.goksi.pterobot.manager.EmbedManager.toEmbed
import tech.goksi.pterobot.util.Common
import tech.goksi.pterobot.util.MemoryBar
import java.util.*
import kotlin.concurrent.fixedRateTimer

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/tech/goksi/pterobot/commands/Servers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Servers(jda: JDA) : SimpleCommand() {
EmbedManager.getGenericFailure(ConfigManager.config.getString(CONFIG_PREFIX + "NodeOffline"))
.toEmbed(event.jda)
).queue()
return;
return
}
val response = EmbedManager.getServerInfo(serverInfo).toEmbed(event.jda)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import net.dv8tion.jda.api.utils.data.DataObject
import net.dv8tion.jda.internal.JDAImpl
import net.dv8tion.jda.internal.utils.Helpers
import tech.goksi.pterobot.EmbedType
import tech.goksi.pterobot.NodeStatus
import tech.goksi.pterobot.entities.NodeInfo
import tech.goksi.pterobot.entities.ServerInfo
import tech.goksi.pterobot.util.MemoryBar
Expand Down
64 changes: 64 additions & 0 deletions src/main/kotlin/tech/goksi/pterobot/util/VersionCheck.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package tech.goksi.pterobot.util

import dev.minn.jda.ktx.util.SLF4J
import net.dv8tion.jda.api.utils.data.DataObject
import okhttp3.*
import java.io.IOException
import java.util.Properties

object VersionCheck {
private val logger by SLF4J

private const val API_URL = "https://api.github.com/repos/Goksi/PteroBot/releases/latest"

fun checkVersion() {
val properties = Properties()
.apply { this.load(VersionCheck::class.java.classLoader.getResourceAsStream("version.properties")) }
val currentVer = properties.getProperty("version")!!
val client = OkHttpClient()
val request = Request.Builder()
.url(API_URL)
.header("accept", "application/vnd.github+json")
.get()
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
logger.error("Error while requesting version !", e)
}

override fun onResponse(call: Call, response: Response) {
response.use {
if (!response.isSuccessful) {
logger.error("Error while requesting version, unexpected status code: $response")
return
}
val fetchedVer = DataObject.fromJson(response.body!!.byteStream())
.getString("tag_name")

if (compareVersions(fetchedVer.replace("v", ""), currentVer) > 0)
logger.warn("You are not running latest version of PteroBot ! Latest: $fetchedVer Current: v$currentVer")
else logger.info("You are running latest version of PteroBot !")
}

}
})
}

private fun compareVersions(version1: String, version2: String): Int {
var result = 0

val ver1List = version1.split(".")
val ver2List = version2.split(".")

for (i in 0..2) {
val v1 = ver1List[i].toInt()
val v2 = ver2List[i].toInt()
val compare = v1.compareTo(v2)
if (compare != 0) {
result = compare
break
}
}
return result
}
}
1 change: 1 addition & 0 deletions src/main/resources/version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = ${project.version}

0 comments on commit c0ba199

Please sign in to comment.