Skip to content

Commit

Permalink
Add crash info
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-korsa committed Jan 28, 2022
1 parent 9b26bb6 commit 34038fb
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class DiscordIntegration: JavaPlugin() {
val discordFormatter = DiscordFormatter(this)
val minecraftFormatter = MinecraftFormatter(this)
val avatarService = AvatarService(this)
val lockFileService = LockFileService(this)
lateinit var configManager: ConfigManager
lateinit var messageManager: MessageManager
private var activityJob: Job? = null
Expand All @@ -45,11 +46,15 @@ class DiscordIntegration: JavaPlugin() {
messageManager = MessageManager(this)
initCommands()
registerEvents()
this.launchAsync { connect(true) }
this.launchAsync {
connect(true)
lockFileService.start()
}
}

override fun onDisable() {
super.onDisable()
lockFileService.stop()
runBlocking {
withTimeout(Duration.ofSeconds(5)) {
disconnect(true)
Expand Down Expand Up @@ -86,7 +91,9 @@ class DiscordIntegration: JavaPlugin() {
client.disconnect()
}

suspend fun reconnect() {
suspend fun reload() {
configManager.reload()
messageManager.reload()
connectionLock.withLock {
disconnect(false)
connect(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.dominikkorsa.discordintegration

import com.github.shynixn.mccoroutine.launchAsync
import discord4j.core.spec.EmbedCreateSpec
import kotlinx.coroutines.Job
import kotlinx.coroutines.isActive
import kotlinx.coroutines.time.delay
import java.time.Duration
import java.util.*

class LockFileService(private val plugin: DiscordIntegration) {
private var job: Job? = null
private val file = plugin.dataFolder.resolve("lock.txt")

suspend fun start() {
readFile()?.let { notifyCrashed(it) }
job = plugin.launchAsync {
while (isActive) {
updateFile()
delay(Duration.ofSeconds(15))
}
}
}

fun stop() {
job?.cancel()
job = null
file.delete()
}

private fun readFile(): Long? {
if (!file.exists()) return null
return file.readText().toLong()
}

private fun updateFile() {
file.writeText(Date().time.toString())
}

private suspend fun notifyCrashed(timestamp: Long) {
if (!plugin.configManager.crashEmbed.enabled) return
val webhookBuilder = plugin.client.getWebhookBuilder()
plugin.client.sendWebhook(
webhookBuilder
.addEmbed(
EmbedCreateSpec.builder()
.title(plugin.messageManager.discordCrashEmbedTitle)
.description(plugin.messageManager.discordCrashEmbedContent)
.addField(
plugin.messageManager.discordCrashEmbedLastOnline,
"<t:${timestamp/1000}>",
false
)
.color(plugin.configManager.crashEmbed.color)
.build()
)
.build()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ class DiscordIntegrationCommand(val plugin: DiscordIntegration): BaseCommand() {
@Subcommand("reload")
@CommandPermission("discordintegration.command.reload")
fun onReload() {
plugin.launchAsync {
plugin.configManager.reload()
plugin.messageManager.reload()
plugin.reconnect()
}
plugin.launchAsync { plugin.reload() }
}

@Subcommand("help")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ class ConfigManager(private val plugin: DiscordIntegration) {
val joinEmbed get() = getEmbedConfig("chat.join-embed")
val quitEmbed get() = getEmbedConfig("chat.quit-embed")
val deathEmbed get() = getEmbedConfig("chat.death-embed")
val crashEmbed get() = getEmbedConfig("chat.crash-embed")
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class MessageManager(plugin: DiscordIntegration) {
val discordDeath get() = getString("discord.death")
val discordDeathFallback get() = getString("discord.death-fallback")
val discordDeathEmbedTitle get() = getString("discord.death-embed-title")
val discordCrashEmbedTitle get() = getString("discord.crash-embed.title")
val discordCrashEmbedContent get() = getString("discord.crash-embed.content")
val discordCrashEmbedLastOnline get() = getString("discord.crash-embed.last-online")
val commandsHelpHeader get() = getString("commands.help.header")
val commandsHelpCommand get() = getString("commands.help.command")
val commandsUnknown get() = getString("commands.unknown")
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ chat:
death-embed:
enabled: true
color: 0xffd166
crash-embed:
enabled: true
color: 0xff8d0a
player-as-status-author: false
avatar:
# Set to true on cracked servers,
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ discord:
death: '**%death-message%**'
death-fallback: '**%player% died**'
death-embed-title: '**%player% died**'
crash-embed:
title: Server started after crash
content: |
The previous server process did not exit properly.
It might have crashed or have been force stopped.
This might also be an error of Discord Integration.
last-online: Last online
minecraft:
message: '§2%nickname% §7in #%channel-name%§7 » §r%content%'
tooltip: |
Expand All @@ -21,7 +28,7 @@ commands:
§9§lDiscord Integration help§r
§9Version: §l%plugin-version%§r
command: '§9> §r%command% §9- %description%§r'
unknown: '§cUnknown command. See avaliable commands using §r/di help'
unknown: '§cUnknown command. See available commands using §r/di help'
descriptions:
help: 'Show list of plugin commands'
reload: 'Reload plugin configuration and reconnect'

0 comments on commit 34038fb

Please sign in to comment.