Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting node style configs from json to yaml #24

Merged
merged 14 commits into from
Feb 7, 2023
Merged
12 changes: 6 additions & 6 deletions src/main/kotlin/tech/goksi/pterobot/EmbedType.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package tech.goksi.pterobot

enum class EmbedType(private val fileName: String) {
GENERIC_ERROR("generic_error.json"),
GENERIC_SUCCESS("generic_success.json"),
NODE_INFO("node_info.json"),
SERVERS_COMMAND("servers_command.json"),
SERVER_INFO("server_info.json"),
NODE_STATUS("node_status.json");
GENERIC_ERROR("generic_error.yml"),
GENERIC_SUCCESS("generic_success.yml"),
NODE_INFO("node_info.yml"),
SERVERS_COMMAND("server_list.yml"),
SERVER_INFO("server_info.yml"),
NODE_STATUS("node_status.yml");

val path
get() = "embeds/$fileName"
Expand Down
24 changes: 12 additions & 12 deletions src/main/kotlin/tech/goksi/pterobot/commands/AccountCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ private class Link : SimpleSubcommand(
ConfigManager.config.getString("$ACCOUNT_PREFIX.Link.LinkSuccess")
.replace("%pteroName", account.userName)
)
.toEmbed(event.jda)
.toEmbed()
} catch (exception: SQLException) {
logger.error("Failed to link ${event.user.idLong}", exception)
EmbedManager.getGenericFailure(ConfigManager.config.getString("Messages.Embeds.UnexpectedError"))
.toEmbed(event.jda)
.toEmbed()
} catch (httpException: HttpException) {
EmbedManager.getGenericFailure(ConfigManager.config.getString("$ACCOUNT_PREFIX.Link.LinkWrongKey"))
.toEmbed(event.jda) // its probably wrong key if we got here, add check maybe
.toEmbed() // its probably wrong key if we got here, add check maybe
}
} else {
response =
EmbedManager.getGenericFailure(ConfigManager.config.getString("$ACCOUNT_PREFIX.Link.LinkExist"))
.toEmbed(event.jda)
.toEmbed()
}
event.reply_(embeds = listOf(response)).queue()
}
Expand All @@ -105,15 +105,15 @@ private class Unlink : SimpleSubcommand(
embeds = listOf(
EmbedManager
.getGenericSuccess(ConfigManager.config.getString("$ACCOUNT_PREFIX.Unlink.SuccessUnlink"))
.toEmbed(event.jda)
.toEmbed()
)
).queue()
} else {
event.reply_(
embeds = listOf(
EmbedManager
.getGenericFailure(ConfigManager.config.getString("$ACCOUNT_PREFIX.Unlink.NotLinked"))
.toEmbed(event.jda)
.toEmbed()
)
).queue()
}
Expand Down Expand Up @@ -170,7 +170,7 @@ private class Register : SimpleSubcommand(
!event.member!!.hasPermission(Permission.ADMINISTRATOR)
) {
event.replyEmbeds(
EmbedManager.getGenericFailure("$ACCOUNT_PREFIX.Register.DisabledMessage").toEmbed(event.jda)
EmbedManager.getGenericFailure("$ACCOUNT_PREFIX.Register.DisabledMessage").toEmbed()
).queue()
return
}
Expand All @@ -182,7 +182,7 @@ private class Register : SimpleSubcommand(
EmbedManager.getGenericFailure(
ConfigManager.config.getString("$ACCOUNT_PREFIX.Register.LimitReached")
.replace("%accounts", pteroMember.registeredAccounts.joinToString(","))
).toEmbed(event.jda)
).toEmbed()
).setEphemeral(true).queue()
}
}
Expand All @@ -197,7 +197,7 @@ private class Register : SimpleSubcommand(
ConfigManager.config.getString(
"$ACCOUNT_PREFIX.Register.InvalidEmail"
)
).toEmbed(event.jda)
).toEmbed()
)
.queue()
return
Expand All @@ -217,7 +217,7 @@ private class Register : SimpleSubcommand(
EmbedManager.getGenericSuccess(
ConfigManager.config.getString("$ACCOUNT_PREFIX.Register.Success")!!
.replace("%pteroName", it.userName)
).toEmbed(event.jda)
).toEmbed()
).queue()
PteroMember(event.user).registerAccount(it.userName)
}, {
Expand All @@ -231,11 +231,11 @@ private class Register : SimpleSubcommand(
EmbedManager.getGenericFailure(
ConfigManager.config.getString("$ACCOUNT_PREFIX.Register.FieldTaken")
.replace("%takenField", takenField)
).toEmbed(event.jda)
).toEmbed()
).queue()
} else event.hook.sendMessageEmbeds(
EmbedManager.getGenericFailure(ConfigManager.config.getString("Messages.Embeds.UnexpectedError"))
.toEmbed(event.jda)
.toEmbed()
).queue()
})
}
Expand Down
22 changes: 10 additions & 12 deletions src/main/kotlin/tech/goksi/pterobot/commands/NodeCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.Permission
import net.dv8tion.jda.api.entities.MessageEmbed
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
Expand Down Expand Up @@ -79,7 +78,6 @@ private class Info : SimpleSubcommand(
withContext(NodeCommand.coroutineScope.coroutineContext) {
getNodeInfoEmbed(
nodeId,
event.jda,
pteroMember.client!!
)
}
Expand All @@ -89,29 +87,29 @@ private class Info : SimpleSubcommand(
success = false
logger.debug("Thrown exception: ", exception)
EmbedManager.getGenericFailure(ConfigManager.config.getString("$NODE_PREFIX.Info.NodeNotFound"))
.toEmbed(event.jda)
.toEmbed()
} // shame that kotlin doesn't have multi catch
else -> throw exception
}
}
} else {
response = EmbedManager.getGenericFailure(ConfigManager.config.getString("$NODE_PREFIX.Info.NotAdmin"))
.toEmbed(event.jda)
.toEmbed()
}
event.hook.sendMessageEmbeds(response).queue {
if (success && update) {
val job = NodeCommand.coroutineScope.launch {
while (true) {
delay(300_000)
it.editMessageEmbeds(getNodeInfoEmbed(nodeId, event.jda, pteroMember.client!!)).queue()
it.editMessageEmbeds(getNodeInfoEmbed(nodeId, pteroMember.client!!)).queue()
}
} /*TODO: fixed 5 minutes delay, make configurable*/
NodeCommand.taskMap[it.idLong] = job
}
}
}

private suspend fun getNodeInfoEmbed(id: Int, jda: JDA, pteroClient: PteroClient): MessageEmbed {
private suspend fun getNodeInfoEmbed(id: Int, pteroClient: PteroClient): MessageEmbed {
val pteroApplication = Common.getDefaultApplication()
val node = pteroApplication.retrieveNodeById(id.toLong()).await()
var memoryUsed: Long = 0
Expand Down Expand Up @@ -144,7 +142,7 @@ private class Info : SimpleSubcommand(
diskUsed = diskSpaceUsed,
cpuUsed = cpuUsed
)
).toEmbed(jda)
).toEmbed()
}
}

Expand All @@ -167,13 +165,13 @@ private class Status : SimpleSubcommand(
val update = event.getOption("update")?.asBoolean ?: false
event.deferReply(ConfigManager.config.getBoolean("BotInfo.Ephemeral")).queue()

event.hook.sendMessageEmbeds(withContext(NodeCommand.coroutineScope.coroutineContext) { getInfoEmbed(event.jda) })
event.hook.sendMessageEmbeds(withContext(NodeCommand.coroutineScope.coroutineContext) { getInfoEmbed() })
.queue {
if (update) {
val job = NodeCommand.coroutineScope.launch {
while (true) {
delay(300_000)
it.editMessageEmbeds(getInfoEmbed(event.jda))
it.editMessageEmbeds(getInfoEmbed())
.queue()
}
}
Expand All @@ -183,13 +181,13 @@ private class Status : SimpleSubcommand(
} else {
event.replyEmbeds(
EmbedManager.getGenericFailure(ConfigManager.config.getString("$NODE_PREFIX.Status.NotAdmin"))
.toEmbed(event.jda)
.toEmbed()
)
}
}

private fun getInfoEmbed(jda: JDA): MessageEmbed {
val embedBuilder = EmbedBuilder(EmbedManager.getNodeStatus().toEmbed(jda))
private fun getInfoEmbed(): MessageEmbed {
val embedBuilder = EmbedBuilder(EmbedManager.getNodeStatus().toEmbed())
val fieldTemplate = embedBuilder.fields[0].also { embedBuilder.clearFields() }
val pteroApplication = Common.getDefaultApplication()
pteroApplication.retrieveNodes().forEach { node ->
Expand Down
36 changes: 18 additions & 18 deletions src/main/kotlin/tech/goksi/pterobot/commands/ServerCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ private class List(jda: JDA) : SimpleSubcommand(
server.sendCommand(it.getValue("command")!!.asString).executeAsync({ _ ->
it.replyEmbeds(
EmbedManager.getGenericSuccess(ConfigManager.config.getString("$SERVER_PATH.List.SuccessCommand"))
.toEmbed(it.jda)
.toEmbed()
).setEphemeral(true).queue()
}) { throwable ->
it.replyEmbeds(
EmbedManager.getGenericFailure(ConfigManager.config.getString("Embeds.UnexpectedError"))
.toEmbed(it.jda)
.toEmbed()
).setEphemeral(true).queue()
logger.error("Error while sending command to ${server.name}", throwable)
}
Expand All @@ -81,7 +81,7 @@ private class List(jda: JDA) : SimpleSubcommand(
event.hook.sendMessageEmbeds(
EmbedManager
.getGenericFailure(ConfigManager.config.getString("$SERVER_PATH.List.NotFound"))
.toEmbed(event.jda)
.toEmbed()
).queue()
return
}
Expand All @@ -91,13 +91,13 @@ private class List(jda: JDA) : SimpleSubcommand(
}
this.placeholder = ConfigManager.config.getString("$SERVER_PATH.List.MenuPlaceholder")
}
val response = EmbedManager.getServersCommand(AccountInfo(pteroMember.getAccount())).toEmbed(event.jda)
val response = EmbedManager.getServersCommand(AccountInfo(pteroMember.getAccount())).toEmbed()

event.hook.sendMessageEmbeds(response).addActionRow(selectMenu).queue()
} else {
event.hook.sendMessageEmbeds(
EmbedManager
.getGenericFailure(ConfigManager.config.getString("$SERVER_PATH.List.NotLinked")).toEmbed(event.jda)
.getGenericFailure(ConfigManager.config.getString("$SERVER_PATH.List.NotLinked")).toEmbed()
).queue()
}
}
Expand All @@ -107,7 +107,7 @@ private class List(jda: JDA) : SimpleSubcommand(
if (event.componentId.split(":")[2] != event.user.id) {
event.replyEmbeds(
EmbedManager.getGenericFailure(ConfigManager.config.getString("$SERVER_PATH.List.WrongUser"))
.toEmbed(event.jda)
.toEmbed()
)
.setEphemeral(true).queue()
return
Expand All @@ -118,7 +118,7 @@ private class List(jda: JDA) : SimpleSubcommand(
if (!pteroMember.isLinked()) {
event.hook.sendMessageEmbeds(
EmbedManager
.getGenericFailure(ConfigManager.config.getString("$SERVER_PATH.List.NotLinked")).toEmbed(event.jda)
.getGenericFailure(ConfigManager.config.getString("$SERVER_PATH.List.NotLinked")).toEmbed()
).setEphemeral(true).queue()
return
}
Expand All @@ -127,7 +127,7 @@ private class List(jda: JDA) : SimpleSubcommand(
} catch (exception: LoginException) {
event.hook.sendMessageEmbeds(
EmbedManager.getGenericFailure(ConfigManager.config.getString("$SERVER_PATH.List.WrongKey"))
.toEmbed(event.jda)
.toEmbed()
).queue()
return
}
Expand All @@ -136,11 +136,11 @@ private class List(jda: JDA) : SimpleSubcommand(
} catch (exception: ServerException) {
event.hook.sendMessageEmbeds(
EmbedManager.getGenericFailure(ConfigManager.config.getString("$SERVER_PATH.List.NodeOffline"))
.toEmbed(event.jda)
.toEmbed()
).queue()
return
}
val response = EmbedManager.getServerInfo(serverInfo).toEmbed(event.jda)
val response = EmbedManager.getServerInfo(serverInfo).toEmbed()
val buttons = getButtons(server, serverInfo, event)
event.hook.sendMessageEmbeds(response).addActionRow(buttons.subList(0, 5))
.addActionRow(buttons.subList(5, buttons.size)).queue()
Expand Down Expand Up @@ -168,13 +168,13 @@ private class List(jda: JDA) : SimpleSubcommand(
ConfigManager.config.getString(
"$SERVER_PATH.List.SuccessStop"
)
).toEmbed(event.jda)
).toEmbed()
)
.setEphemeral(true).queue()
}) {
buttonEvent.hook.sendMessageEmbeds(
EmbedManager.getGenericFailure(ConfigManager.config.getString("Embeds.UnexpectedError"))
.toEmbed(event.jda)
.toEmbed()
)
.setEphemeral(true).queue().also { _ ->
logger.error("Error while changing server state !", it)
Expand All @@ -196,13 +196,13 @@ private class List(jda: JDA) : SimpleSubcommand(
ConfigManager.config.getString(
"$SERVER_PATH.List.SuccessStart"
)
).toEmbed(event.jda)
).toEmbed()
)
.setEphemeral(true).queue()
}) {
buttonEvent.hook.sendMessageEmbeds(
EmbedManager.getGenericFailure(ConfigManager.config.getString("Messages.Embeds.UnexpectedError"))
.toEmbed(event.jda)
.toEmbed()
)
.setEphemeral(true).queue().also { _ ->
logger.error("Error while changing server state !", it)
Expand All @@ -225,13 +225,13 @@ private class List(jda: JDA) : SimpleSubcommand(
ConfigManager.config.getString(
"$SERVER_PATH.List.SuccessRestart"
)
).toEmbed(event.jda)
).toEmbed()
)
.setEphemeral(true).queue()
}) {
buttonEvent.hook.sendMessageEmbeds(
EmbedManager.getGenericFailure(ConfigManager.config.getString("Embeds.UnexpectedError"))
.toEmbed(event.jda)
.toEmbed()
)
.setEphemeral(true).queue().also { _ ->
logger.error("Error while changing server state !", it)
Expand Down Expand Up @@ -301,14 +301,14 @@ private class List(jda: JDA) : SimpleSubcommand(
} catch (exception: ServerException) {
it.editMessageEmbeds(
EmbedManager.getGenericFailure(ConfigManager.config.getString("$SERVER_PATH.List.NodeOffline"))
.toEmbed(event.jda)
.toEmbed()
).setActionRow(closeButton).queue()
return@cooldownButton
}
val newButtons = getButtons(serverNew, serverInfoNew, event)
val rows =
listOf(ActionRow.of(newButtons.subList(0, 5)), ActionRow.of(newButtons.subList(5, newButtons.size)))
it.editMessageEmbeds(EmbedManager.getServerInfo(serverInfoNew).toEmbed(event.jda)).setReplace(true)
it.editMessageEmbeds(EmbedManager.getServerInfo(serverInfoNew).toEmbed()).setReplace(true)
.setComponents(rows).queue()
}

Expand Down
Loading