Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Added (optional) automatic updating
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT2 committed Aug 28, 2020
1 parent 419fdce commit df58a86
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Bot {
return
}

val client = Kordis.create {
@Suppress("UNUSED_VARIABLE") val client = Kordis.create {
token = config.botToken

// Annotation based Event Listener
Expand Down Expand Up @@ -69,7 +69,7 @@ class Bot {
}

object Main {
const val currentVersion = "1.0.0"
const val currentVersion = "1.0.1"
/**
* Int colors, converted from here: https://www.shodor.org/stella2java/rgbint.html
*/
Expand Down
43 changes: 35 additions & 8 deletions src/main/kotlin/UpdateHelper.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import Main.currentVersion
import UpdateHelper.updateBot
import java.io.File
import java.io.IOException
import java.net.URL
import java.nio.file.Files
import java.nio.file.Paths

fun main() {
updateBot("")
}
import java.util.concurrent.TimeUnit

/**
* @author dominikaaaa
Expand All @@ -23,13 +21,15 @@ object UpdateHelper {
}

if (versionConfig.version != currentVersion) {
println("Not up to date:\nCurrent version: $currentVersion\nLatest Version: ${versionConfig.version}\n")
println("Not up to date:\nCurrent version: $currentVersion\nLatest Version: ${versionConfig.version}")

updateBot(versionConfig.version)
} else {
println("Up to date! Running on $currentVersion")
}
}

fun updateBot(version: String) {
private fun updateBot(version: String) {
val userConfig = FileManager.readConfig<UserConfig>(ConfigType.USER, false)

if (userConfig?.autoUpdate == null || !userConfig.autoUpdate) {
Expand All @@ -45,9 +45,36 @@ object UpdateHelper {
it.delete()
}
}
println("Auto Update - Deleted the following files:\n" + deleted.joinToString { it })

if (deleted.isNotEmpty()) {
println("Auto Update - Deleted the following files:\n" + deleted.joinToString { it })
}

val bytes = URL("https://github.com/kami-blue/bot-kt/releases/download/$version/bot-kt-$version.jar").readBytes()

println("Auto Update - Downloaded bot-kt-$version.jar ${bytes.size / 1000000}MB")
val appendSlash = if (path.endsWith("/")) "" else "/"
val targetFile = path.toString() + appendSlash + "bot-kt-$version.jar"
File(targetFile).writeBytes(bytes)
println("Auto Update - Finished updating to $version")

try {
"pm2 reload bot-kt".runCommand(File(path.toString()))
} catch (e: IOException) {
println("pm2 is not installed, failed to reload bot.\n" +
"You can ignore this if you're not using pm2, but you will need to manually restart the bot to start using the latest version.")
}
} else {
println("Auto Update - Couldn't find install path \"$path\"")
}
}

private fun String.runCommand(workingDir: File) {
ProcessBuilder(*split(" ").toTypedArray())
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start()
.waitFor(1, TimeUnit.MINUTES)
}
}

0 comments on commit df58a86

Please sign in to comment.