-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b26bb6
commit 34038fb
Showing
7 changed files
with
85 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/com/dominikkorsa/discordintegration/LockFileService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters