Skip to content

Commit

Permalink
Merge pull request #7 from Goksi/lint
Browse files Browse the repository at this point in the history
Adding ktlinter
  • Loading branch information
Goksi authored Nov 26, 2022
2 parents 8cd8b44 + 016e1f0 commit a8b756e
Show file tree
Hide file tree
Showing 26 changed files with 157 additions and 81 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root = true

[*.{kt,kts}]
ktlint_disabled_rules = multiline-if-else,no-wildcard-imports
25 changes: 25 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Lint check

on:
pull_request:
branches:
- main

jobs:
lint:
name: Lint check
runs-on: ubuntu-latest

steps:
- name: Checkout main branch
uses: actions/checkout@v2

- name: Setup JDK
uses: actions/setup-java@v3.6.0
with:
java-version: 17
distribution: temurin
cache: 'maven'

- name: Run lint checker
run: mvn verify
55 changes: 55 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,61 @@
<target>8</target>
</configuration>
</plugin>
<!--Kt-linter-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>ktlint</id>
<phase>verify</phase>
<configuration>
<target name="ktlint">
<java taskname="ktlint"
dir="${basedir}"
fork="true"
failonerror="true"
classpathref="maven.plugin.classpath"
classname="com.pinterest.ktlint.Main">
<jvmarg value="--add-opens=java.base/java.lang=ALL-UNNAMED"/>
<arg value="src/**/*.kt"/>
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>ktlint-format</id>
<configuration>
<target name="ktlint">
<java taskname="ktlint"
dir="${basedir}"
fork="true"
failonerror="true"
classpathref="maven.plugin.classpath"
classname="com.pinterest.ktlint.Main">
<jvmarg value="--add-opens=java.base/java.lang=ALL-UNNAMED"/>
<arg value="-F"/>
<arg value="src/**/*.kt"/>
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.pinterest</groupId>
<artifactId>ktlint</artifactId>
<version>0.47.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/tech/goksi/pterobot/EmbedType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ enum class EmbedType(private val fileName: String) {

val path
get() = "embeds/$fileName"

}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/tech/goksi/pterobot/EntryPoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package tech.goksi.pterobot

fun main(args: Array<String>) {
PteroBot()
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/tech/goksi/pterobot/NodeStatus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ private const val CONFIG_PREFIX = "Messages.Commands.NodeInfo."
enum class NodeStatus(val message: String, val emoji: String) {
ONLINE("Online", ConfigManager.config.getString(CONFIG_PREFIX + "OnlineEmoji")),
OFFLINE("Offline", ConfigManager.config.getString(CONFIG_PREFIX + "OfflineEmoji"));
}
}
17 changes: 10 additions & 7 deletions src/main/kotlin/tech/goksi/pterobot/PteroBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ class PteroBot {

init {
val tokenPair = Checks.checkInput(
ConfigManager.config.getString("BotInfo.Token"), DEFAULT_NO_TOKEN_MSG,
ConfigManager.config.getString("BotInfo.Token"),
DEFAULT_NO_TOKEN_MSG,
"You didn't provide your bot token, please input it right-now: "
) { readLine() }
val guildPair = Checks.checkInput(
ConfigManager.config.getString("BotInfo.ServerID"), DEFAULT_NO_ID_MSG,
ConfigManager.config.getString("BotInfo.ServerID"),
DEFAULT_NO_ID_MSG,
"You didn't provide your discord server id, please input it right-now: "
) {
var input: String
Expand All @@ -42,7 +44,8 @@ class PteroBot {
input
}
val appUrlPair = Checks.checkInput(
ConfigManager.config.getString("BotInfo.PterodactylUrl"), DEFAULT_NO_URL_MSG,
ConfigManager.config.getString("BotInfo.PterodactylUrl"),
DEFAULT_NO_URL_MSG,
"You didn't provide your pterodactyl url, please input it right-now:"
) {
var input: String
Expand All @@ -54,7 +57,8 @@ class PteroBot {
input
}
val apiKeyPair = Checks.checkInput(
ConfigManager.config.getString("BotInfo.AdminApiKey"), DEFAULT_NO_API_KEY_MSG,
ConfigManager.config.getString("BotInfo.AdminApiKey"),
DEFAULT_NO_API_KEY_MSG,
"You didn't provide admin key for actions like register and node info, please input it right-now:"
) {
var input: String
Expand Down Expand Up @@ -94,7 +98,6 @@ class PteroBot {
}
)
}

}.awaitReady()

val commandData = SimpleCommandData()
Expand All @@ -106,7 +109,7 @@ class PteroBot {
Unlink(),
NodeStatusCmd()
)
val guild = jda.getGuildById(guildPair.first!!) //what if wrong guild id ?
val guild = jda.getGuildById(guildPair.first!!) // what if wrong guild id ?
guild?.updateCommands()?.addCommands(commandData.buildData())?.queue()
jda.addEventListener(NodeStatusDelete())
commandData.registerListeners(jda)
Expand All @@ -123,4 +126,4 @@ class PteroBot {
} else logger.warn("Wrong command ! You mean \"stop\" ?")
}
}
}
}
4 changes: 2 additions & 2 deletions src/main/kotlin/tech/goksi/pterobot/commands/Link.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ class Link : SimpleCommand() {
.toEmbed(event.jda)
} catch (httpException: HttpException) {
EmbedManager.getGenericFailure(ConfigManager.config.getString(CONFIG_PREFIX + "LinkWrongKey"))
.toEmbed(event.jda) //its probably wrong key if we got here, add check maybe
.toEmbed(event.jda) // its probably wrong key if we got here, add check maybe
}
} else {
response = EmbedManager.getGenericFailure(ConfigManager.config.getString(CONFIG_PREFIX + "LinkExist"))
.toEmbed(event.jda)
}
event.reply_(embeds = listOf(response)).queue()
}
}
}
16 changes: 6 additions & 10 deletions src/main/kotlin/tech/goksi/pterobot/commands/NodeInfoCmd.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class NodeInfoCmd : SimpleCommand() {
private val logger by SLF4J

companion object TaskMapping {
val mapping: MutableMap<Long, Timer> = HashMap() //message id and timer
val mapping: MutableMap<Long, Timer> = HashMap() // message id and timer
}

init {
Expand Down Expand Up @@ -75,11 +75,10 @@ class NodeInfoCmd : SimpleCommand() {
logger.debug("Thrown exception: ", exception)
EmbedManager.getGenericFailure(ConfigManager.config.getString(CONFIG_PREFIX + "NodeNotFound"))
.toEmbed(event.jda)
} //shame that kotlin doesn't have multi catch
} // shame that kotlin doesn't have multi catch
else -> throw exception
}
}

} else {
response = EmbedManager.getGenericFailure(ConfigManager.config.getString(CONFIG_PREFIX + "NotAdmin"))
.toEmbed(event.jda)
Expand All @@ -97,14 +96,13 @@ class NodeInfoCmd : SimpleCommand() {
mapping[it.idLong] = timer
}
}

}

/*TODO: catch login exception ?*/
private fun getNodeInfoEmbed(id: Int, jda: JDA): MessageEmbed {
val ptero by lazy {
Common.getDefaultApplication() to
Common.createClient(ConfigManager.config.getString("BotInfo.AdminApiKey"))
Common.createClient(ConfigManager.config.getString("BotInfo.AdminApiKey"))
}
val node = ptero.first.retrieveNodeById(id.toLong()).execute()
var memoryUsed: Long = 0
Expand All @@ -121,11 +119,10 @@ class NodeInfoCmd : SimpleCommand() {
status = NodeStatus.OFFLINE
return@filter false
}
memoryUsed += utilization.memory / 1024 / 1024 //mb
diskSpaceUsed += utilization.disk.toFloat() / 1024 / 1024 / 1024 //gb
memoryUsed += utilization.memory / 1024 / 1024 // mb
diskSpaceUsed += utilization.disk.toFloat() / 1024 / 1024 / 1024 // gb
cpuUsed += utilization.cpu
return@filter utilization.state == UtilizationState.RUNNING || utilization.state == UtilizationState.STARTING

} else return@filter false
}

Expand All @@ -139,6 +136,5 @@ class NodeInfoCmd : SimpleCommand() {
cpuUsed = cpuUsed
)
).toEmbed(jda)

}
}
}
12 changes: 7 additions & 5 deletions src/main/kotlin/tech/goksi/pterobot/commands/NodeStatusCmd.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class NodeStatusCmd : SimpleCommand() {
}
override fun execute(event: SlashCommandInteractionEvent) {
val pteroMember = PteroMember(event.member!!)
if(event.member!!.hasPermission(Permission.ADMINISTRATOR) || pteroMember.isPteroAdmin()){
if (event.member!!.hasPermission(Permission.ADMINISTRATOR) || pteroMember.isPteroAdmin()) {
val update = event.getOption("update")?.asBoolean ?: false
event.deferReply(ConfigManager.config.getBoolean("BotInfo.Ephemeral")).queue()

Expand All @@ -58,16 +58,18 @@ class NodeStatusCmd : SimpleCommand() {
daemon = true,
period = 300_000,
initialDelay = 300_000
) {//hardcoded 5 minutes, probably wrong to use mapping.size
) { // hardcoded 5 minutes, probably wrong to use mapping.size
it.editMessageEmbeds(getInfoEmbed(event.jda))
.queue()
}
mapping[it.idLong] = timer
}
}
} else {
event.replyEmbeds(EmbedManager.getGenericFailure(ConfigManager.config.getString(CONFIG_PREFIX + "NotAdmin"))
.toEmbed(event.jda))
event.replyEmbeds(
EmbedManager.getGenericFailure(ConfigManager.config.getString(CONFIG_PREFIX + "NotAdmin"))
.toEmbed(event.jda)
)
}
}

Expand All @@ -93,4 +95,4 @@ class NodeStatusCmd : SimpleCommand() {
}
return embedBuilder.build()
}
}
}
7 changes: 3 additions & 4 deletions src/main/kotlin/tech/goksi/pterobot/commands/Register.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import tech.goksi.pterobot.util.Checks
import tech.goksi.pterobot.util.Common

private const val CONFIG_PREFIX = "Messages.Commands.Register."

/*TODO: account deletion*/
/*TODO: maybe commands rework etc /account delete /account link...*/
class Register : SimpleCommand() {
Expand Down Expand Up @@ -113,7 +114,7 @@ class Register : SimpleCommand() {
val takenField = errorMessage.substring(
errorMessage.indexOf("Source:") + 8,
errorMessage.indexOf(")")
) //lvl pro string extraction
) // lvl pro string extraction

event.hook.sendMessageEmbeds(
EmbedManager.getGenericFailure(
Expand All @@ -122,13 +123,11 @@ class Register : SimpleCommand() {
).toEmbed(event.jda)
)
.complete()

} else event.hook.sendMessageEmbeds(
EmbedManager.getGenericFailure(ConfigManager.config.getString("Messages.Embeds.UnexpectedError"))
.toEmbed(event.jda)
)
.complete()
})

}
}
}
Loading

0 comments on commit a8b756e

Please sign in to comment.