Update Json Serialization

This commit is contained in:
Wieland Schöbl
2021-05-28 19:07:47 +02:00
parent f0f6015d6f
commit 7f207674ba
7 changed files with 69 additions and 118 deletions

View File

@@ -1,22 +1,6 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value>
<package name="java.util" alias="false" withSubpackages="false" />
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
<package name="io.ktor" alias="false" withSubpackages="true" />
</value>
</option>
<option name="PACKAGES_IMPORT_LAYOUT">
<value>
<package name="" alias="false" withSubpackages="true" />
<package name="java" alias="false" withSubpackages="true" />
<package name="javax" alias="false" withSubpackages="true" />
<package name="kotlin" alias="false" withSubpackages="true" />
<package name="" alias="true" withSubpackages="true" />
</value>
</option>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<codeStyleSettings language="kotlin">

View File

@@ -21,5 +21,10 @@
<option name="name" value="BintrayJCenter" />
<option name="url" value="https://jcenter.bintray.com/" />
</remote-repository>
<remote-repository>
<option name="id" value="maven" />
<option name="name" value="maven" />
<option name="url" value="https://m2.dv8tion.net/releases" />
</remote-repository>
</component>
</project>

30
.idea/workspace.xml generated
View File

@@ -4,15 +4,14 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="1aabf22b-2f57-46ac-9973-367d8668ffd3" name="Default Changelist" comment="no idea what that did">
<change afterPath="$PROJECT_DIR$/src/main/java/org/hmcore/TwitterJob.java" afterDir="false" />
<list default="true" id="1aabf22b-2f57-46ac-9973-367d8668ffd3" name="Default Changelist" comment="Update dependencies">
<change beforePath="$PROJECT_DIR$/.idea/codeStyles/Project.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/codeStyles/Project.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/jarRepositories.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/jarRepositories.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/build.gradle" beforeDir="false" afterPath="$PROJECT_DIR$/build.gradle" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/Admin.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/Admin.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/Channels.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/Channels.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/DataIO.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/DataIO.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/DiscordRpc.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/DiscordRpc.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/Main.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/Main.kt" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -78,10 +77,10 @@
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/build/libs" />
<property name="project.structure.last.edited" value="Modules" />
<property name="project.structure.last.edited" value="Project" />
<property name="project.structure.proportion" value="0.15" />
<property name="project.structure.side.proportion" value="0.2" />
<property name="settings.editor.selected.configurable" value="reference.settingsdialog.project.gradle" />
<property name="settings.editor.selected.configurable" value="preferences.pluginManager" />
</component>
<component name="RecentsManager">
<key name="MoveKotlinTopLevelDeclarationsDialog.RECENTS_KEY">
@@ -96,7 +95,7 @@
<recent name="E:\Projects\Kotlin_Proj\HytaleUpdateBot\src\main\kotlin\de\wulkanat" />
</key>
</component>
<component name="RunManager" selected="Application.MainKt">
<component name="RunManager" selected="Gradle.HytaleUpdateBot [build]">
<configuration name="HytaleUpdateBot [build]" type="GradleRunConfiguration" factoryName="Gradle" temporary="true">
<ExternalSystemSettings>
<option name="executionName" />
@@ -165,14 +164,8 @@
<method v="2" />
</configuration>
<configuration name="MainKt" type="JetRunConfigurationType" temporary="true" nameIsGenerated="true">
<module name="HytaleUpdateBot.main" />
<option name="VM_PARAMETERS" />
<option name="PROGRAM_PARAMETERS" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="MAIN_CLASS_NAME" value="de.wulkanat.MainKt" />
<option name="WORKING_DIRECTORY" />
<module name="HytaleUpdateBot.main" />
<method v="2">
<option name="Make" enabled="true" />
</method>
@@ -257,15 +250,6 @@
<option name="LAST_COMMIT_MESSAGE" value="prepare twitter integration" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="kotlin-line">
<url>file://$PROJECT_DIR$/src/main/kotlin/de/wulkanat/AdminCli.kt</url>
<line>22</line>
<option name="timeStamp" value="1" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
<watches-manager>
<configuration name="JetRunConfigurationType">
<watch expression="((org.jsoup.nodes.Element.NodeList)((Document)doc).childNodes).get(2)" custom="org.jsoup.nodes.Element.NodeList,org.jsoup.nodes.Document" />

View File

@@ -9,14 +9,17 @@ version '2.0.0'
repositories {
mavenCentral()
jcenter()
maven {
url 'https://m2.dv8tion.net/releases'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation 'net.dv8tion:JDA:4.2.0_189'
implementation 'net.dv8tion:JDA:4.2.1_253'
implementation 'org.jsoup:jsoup:1.13.1'
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.14.0"
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.1'
implementation 'com.github.redouane59.twitter:twittered:1.20'
implementation group: 'org.quartz-scheduler', name: 'quartz', version: '2.3.2'

View File

@@ -1,18 +1,17 @@
@file:JvmName("Admin")
package de.wulkanat
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.entities.Activity
import net.dv8tion.jda.api.entities.MessageEmbed
import net.dv8tion.jda.api.entities.User
import java.awt.Color
object Admin {
@JvmField
val adFile = Json(JsonConfiguration.Stable).parse(AdminFile.serializer(), ADMIN_FILE.readText())
val adFile = Json.decodeFromString<AdminFile>(ADMIN_FILE.readText())
val userId: Long = adFile.adminId
val token: String = adFile.token
val updateMs: Long = adFile.updateMs
@@ -21,8 +20,8 @@ object Admin {
fun connectToUser() {
Main.jdas.forEach {
if(admin != null) return;
admin = it.retrieveUserById(userId)?.complete()
admin ?: return;
admin = it.retrieveUserById(userId).complete()
}
if (admin == null) {
kotlin.io.println("Connection to de.wulkanat.Admin failed!")
@@ -30,6 +29,7 @@ object Admin {
kotlin.io.println("Connected to ${admin!!.name}. No further errors will be printed here.")
}
}
var admin: User? = null
fun println(msg: String) {
@@ -65,8 +65,7 @@ object Admin {
this.setAuthor(author.asTag, author.avatarUrl, author.avatarUrl)
}
}
.build()
, "$msg\n\n${error}"
.build(), "$msg\n\n${error}"
)
}
@@ -76,8 +75,7 @@ object Admin {
.setTitle(msg)
.setDescription(error.message)
.setColor(Color.RED)
.build()
, "$msg\n\n${error.message}"
.build(), "$msg\n\n${error.message}"
)
}
@@ -95,12 +93,14 @@ object Admin {
sendDevMessage(
EmbedBuilder()
.setTitle("Now watching for new Hytale Blogposts every ${updateMs / 1000}s")
.setDescription("""
.setDescription(
"""
${Channels.getServerNames().joinToString("\n")}
**_Service Channels_**
${Channels.getServiceChannelServers().joinToString("\n")}
""".trimIndent())
""".trimIndent()
)
.setColor(Color.GREEN)
.build(),
"Now watching for new Hytale BlogPosts"

View File

@@ -1,13 +1,13 @@
@file:JvmName("Channels")
package de.wulkanat
import de.wulkanat.extensions.crosspost
import kotlinx.serialization.list
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.Permission
import net.dv8tion.jda.api.entities.Message
import net.dv8tion.jda.api.entities.MessageEmbed
import net.dv8tion.jda.api.entities.TextChannel
import java.awt.Color
@@ -75,7 +75,8 @@ object Channels {
}
Admin.println("Service message distributed to ${serviceChannels.size} channels.")
Admin.sendDevMessage(serviceMessage, """
Admin.sendDevMessage(
serviceMessage, """
***************
SERVICE MESSAGE
@@ -83,7 +84,8 @@ object Channels {
-------
$message
***************
""".trimIndent())
""".trimIndent()
)
}
fun checkEveryonePermission() {
@@ -103,42 +105,33 @@ object Channels {
}
}
fun refreshChannelsFromDisk(): MutableList<DiscordChannel> {
return json.parse(
DiscordChannel.serializer().list, (SERVERS_FILE).readText()
).toMutableList()
}
fun refreshChannelsFromDisk() =
Json.decodeFromString<List<DiscordChannel>>(SERVERS_FILE.readText()).toMutableList()
fun refreshServiceChannelsFromDisk(): MutableList<ServiceChannel> {
return json.parse(
ServiceChannel.serializer().list, (SERVICE_CHANNELS_FILE).readText()
).toMutableList()
}
fun refreshServiceChannelsFromDisk() =
Json.decodeFromString<List<ServiceChannel>>(SERVICE_CHANNELS_FILE.readText()).toMutableList()
fun getServerNames(server: Long? = null): List<String> {
return Main.jdas.flatMap { jda ->
channels.filter { server == null || (jda!!.getTextChannelById(it.id)?.guild?.idLong == server) }.map {
val channel = jda!!.getTextChannelById(it.id)
if (channel == null) {
Admin.warning("Channel ${it.id} is no longer active!")
return@map "**${it.id}** *(inactive)*"
}
val role = when (it.mentionedRole) {
null -> ""
"everyone" -> " @everyone"
else -> " @${channel.guild.getRoleById(it.mentionedRole ?: "")?.name}"
}
val publish = if (it.autoPublish) " (publish)" else ""
"**${channel.guild.name}** #${channel.name}${role}${publish}${
if (it.message == null) {
""
} else {
"\n*${it.message!!.message}*${if (it.message!!.pushAnnouncement) " (publish)" else ""}"
}
}"
fun getServerNames(server: Long? = null) = Main.jdas.flatMap { jda ->
channels.filter { server == null || (jda.getTextChannelById(it.id)?.guild?.idLong == server) }.map {
val channel = jda.getTextChannelById(it.id)
if (channel == null) {
Admin.warning("Channel ${it.id} is no longer active!")
return@map "**${it.id}** *(inactive)*"
}
val role = when (it.mentionedRole) {
null -> ""
"everyone" -> " @everyone"
else -> " @${channel.guild.getRoleById(it.mentionedRole ?: "")?.name}"
}
val publish = if (it.autoPublish) " (publish)" else ""
"**${channel.guild.name}** #${channel.name}${role}${publish}${
if (it.message == null) {
""
} else {
"\n*${it.message!!.message}*${if (it.message!!.pushAnnouncement) " (publish)" else ""}"
}
}"
}
}
@@ -153,13 +146,8 @@ object Channels {
}
}
fun testServerId(id: Long): TextChannel? {
return Main.jdas.map {
it.getTextChannelById(id)
}.firstOrNull()
}
fun testServerId(id: Long) =
Main.jdas.map { it.getTextChannelById(id) }.firstOrNull()
fun addChannel(id: Long, role: String?): DiscordChannel? {
if (channels.find { it.id == id } != null) {
@@ -172,17 +160,7 @@ object Channels {
}
fun saveChannels() {
SERVERS_FILE.writeText(
json.stringify(
DiscordChannel.serializer().list,
channels
)
)
SERVICE_CHANNELS_FILE.writeText(
json.stringify(
ServiceChannel.serializer().list,
serviceChannels
)
)
SERVERS_FILE.writeText(Json.encodeToString(channels))
SERVICE_CHANNELS_FILE.writeText(Json.encodeToString(serviceChannels))
}
}

View File

@@ -3,9 +3,8 @@ package de.wulkanat
import de.wulkanat.extensions.ensureExists
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
import kotlinx.serialization.list
import java.io.File
@Serializable
@@ -45,9 +44,7 @@ data class TwitterApi(
val apiKeySecret: String = "Api Key secret here"
)
val json = Json(JsonConfiguration.Stable)
val SERVERS_FILE = File("servers.json").ensureExists(json.stringify(DiscordChannel.serializer().list, listOf()))
val SERVERS_FILE = File("servers.json").ensureExists(Json.encodeToString(listOf<DiscordChannel>()))
val SERVICE_CHANNELS_FILE =
File("service_channels.json").ensureExists(json.stringify(ServiceChannel.serializer().list, listOf()))
val ADMIN_FILE = File("admin.json").ensureExists(json.stringify(AdminFile.serializer(), AdminFile()))
File("service_channels.json").ensureExists(Json.encodeToString(listOf<ServiceChannel>()))
val ADMIN_FILE = File("admin.json").ensureExists(Json.encodeToString(AdminFile()))