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

Commit

Permalink
Added file deletion to updating and userConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT2 committed Aug 26, 2020
1 parent d038c79 commit 419fdce
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Bot configuration files
auth.json
mutes.json
config/

# Intellij IDEA configuration files
.idea/
Expand Down
19 changes: 15 additions & 4 deletions src/main/kotlin/FileManager.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {

Expand Down Expand Up @@ -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)
}

/**
Expand All @@ -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)
Expand All @@ -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)
36 changes: 30 additions & 6 deletions src/main/kotlin/UpdateHelper.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,12 +29,25 @@ object UpdateHelper {
}
}

/**
* TODO: FINISH
*/
fun updateBot(version: String) {
if (!File("autoUpdate").exists()) return
val userConfig = FileManager.readConfig<UserConfig>(ConfigType.USER, false)

if (userConfig?.autoUpdate == null || !userConfig.autoUpdate) {
return
}

println()
val path = Paths.get(userConfig.installPath)
if (Files.isDirectory(path)) {
val deleted = arrayListOf<String>()
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\"")
}
}
}
}

0 comments on commit 419fdce

Please sign in to comment.