diff --git a/.gitignore b/.gitignore index 36c2d057..4a2b1f70 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ # Bot configuration files -auth.json -mutes.json +config/ # Intellij IDEA configuration files .idea/ diff --git a/src/main/kotlin/FileManager.kt b/src/main/kotlin/FileManager.kt index 630fb115..1103e8ae 100644 --- a/src/main/kotlin/FileManager.kt +++ b/src/main/kotlin/FileManager.kt @@ -1,5 +1,6 @@ import FileManager.authConfigData import FileManager.mutesConfigData +import FileManager.userConfigData import FileManager.versionConfigData import com.google.gson.Gson import java.net.URL @@ -15,6 +16,7 @@ object FileManager { var authConfigData: AuthConfig? = null var mutesConfigData: MuteConfig? = null var versionConfigData: VersionConfig? = null + var userConfigData: UserConfig? = null fun writeConfig(configType: ConfigType) { @@ -76,9 +78,10 @@ object FileManager { * [clazz] is the associated class with the [data] format */ enum class ConfigType(val configPath: String, var data: Any?, val clazz: Class<*>) { - AUTH("auth.json", authConfigData, AuthConfig::class.java), - MUTE("mutes.json", mutesConfigData, MuteConfig::class.java), - VERSION("https://raw.githubusercontent.com/kami-blue/bot-kt/master/version.json", versionConfigData, VersionConfig::class.java); + AUTH("config/auth.json", authConfigData, AuthConfig::class.java), + MUTE("config/mutes.json", mutesConfigData, MuteConfig::class.java), + VERSION("https://raw.githubusercontent.com/kami-blue/bot-kt/master/version.json", versionConfigData, VersionConfig::class.java), + USER("config/user.json", userConfigData, UserConfig::class.java) } /** @@ -89,7 +92,7 @@ data class AuthConfig(val botToken: String, val githubToken: String) /** * [id] is the user snowflake ID - * [unixUnmute] is the UNIX time of then they should be unmuted. + * [unixUnmute] is the UNIX time of when they should be unmuted. * When adding a new [unixUnmute] time, it should be current UNIX time + mute time in seconds */ data class MuteConfig(val id: Long, val unixUnmute: Long) @@ -99,3 +102,11 @@ data class MuteConfig(val id: Long, val unixUnmute: Long) * Checked by comparing [Main.currentVersion] against https://raw.githubusercontent.com/kami-blue/bot-kt/master/version.json */ data class VersionConfig(val version: String) + +/** + * [autoUpdate] is whether the bot should automatically update after a successful update check. Will not do anything when set to true if update checking is disabled. + * [installPath] is the full file path of where your bot is, eg /home/mika/bot-kt + * I'm not sure why you would be running a server on Windows in the first place, as such, auto update is only tested on Linux + * [startUpChannel] is the channel ID of where to send bot startup messages. Omit from config to disable startup messages. + */ +data class UserConfig(val autoUpdate: Boolean, val installPath: String, val startUpChannel: Long) diff --git a/src/main/kotlin/UpdateHelper.kt b/src/main/kotlin/UpdateHelper.kt index b1cfdccc..6d610da5 100644 --- a/src/main/kotlin/UpdateHelper.kt +++ b/src/main/kotlin/UpdateHelper.kt @@ -1,6 +1,17 @@ import Main.currentVersion +import UpdateHelper.updateBot import java.io.File +import java.nio.file.Files +import java.nio.file.Paths +fun main() { + updateBot("") +} + +/** + * @author dominikaaaa + * @since 2020/08/25 20:06 + */ object UpdateHelper { fun updateCheck() { if (File("noUpdateCheck").exists()) return @@ -18,12 +29,25 @@ object UpdateHelper { } } - /** - * TODO: FINISH - */ fun updateBot(version: String) { - if (!File("autoUpdate").exists()) return + val userConfig = FileManager.readConfig(ConfigType.USER, false) + + if (userConfig?.autoUpdate == null || !userConfig.autoUpdate) { + return + } - println() + val path = Paths.get(userConfig.installPath) + if (Files.isDirectory(path)) { + val deleted = arrayListOf() + File(userConfig.installPath).walk().forEach { + if (it.name.matches(Regex("bot-kt.*.jar"))) { + deleted.add(it.name) + it.delete() + } + } + println("Auto Update - Deleted the following files:\n" + deleted.joinToString { it }) + } else { + println("Auto Update - Couldn't find install path \"$path\"") + } } -} \ No newline at end of file +}