Skip to content

Commit

Permalink
feat: connection retry (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuuahp authored Oct 14, 2024
1 parent a1d9784 commit a815076
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/main/kotlin/com/jaoafa/vcspeaker/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import com.uchuhimo.konf.Config
import com.uchuhimo.konf.source.yaml
import dev.kord.common.entity.Snowflake
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import java.net.SocketException
import kotlin.io.path.Path
import kotlin.reflect.full.createInstance

Expand Down Expand Up @@ -89,7 +91,7 @@ class Main : CliktCommand() {
addSpec(EnvSpec)
}.from.yaml.file(configPath.toFile())

runBlocking {
suspend fun init() {
VCSpeaker.init(
config = config,
voicetext = VoiceTextAPI(apiKey = config[TokenSpec.voicetext]),
Expand Down Expand Up @@ -140,6 +142,20 @@ class Main : CliktCommand() {

instance.start()
}

suspend fun retryLoop() {
try {
init()
} catch (e: SocketException) {
logger.error(e) { "Failed to connect to Discord. Retrying after 10 seconds..." }
// wait 10 seconds before retrying
VCSpeaker.instance.stop()
delay(10000)
retryLoop()
}
}

runBlocking { retryLoop() }
}
}

Expand Down

0 comments on commit a815076

Please sign in to comment.