mirror of
https://github.com/HMCore/Orbot.git
synced 2025-12-12 13:56:18 +00:00
Update Json Serialization
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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()))
|
||||
|
||||
Reference in New Issue
Block a user