From 746c47c138e3aff059ab16760be7b1d514b3279d Mon Sep 17 00:00:00 2001 From: kosugikun <> Date: Wed, 29 Mar 2023 20:32:43 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B3=E3=83=B3=E3=82=BD=E3=83=BC=E3=83=AB?= =?UTF-8?q?=E4=B8=8A=E3=81=A7=E3=83=9C=E3=83=83=E3=83=88=E3=81=AE=E7=B5=82?= =?UTF-8?q?=E4=BA=86=E5=87=A6=E7=90=86=E3=82=92=E5=AE=9F=E8=A1=8C=E3=81=97?= =?UTF-8?q?=E3=81=9F=E9=9A=9B=E3=81=AB=E7=99=BA=E7=94=9F=E3=81=99=E3=82=8B?= =?UTF-8?q?=E4=B8=8D=E5=85=B7=E5=90=88=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/dev/cosgy/textToSpeak/Bot.kt | 61 ++++++++++++------- .../java/dev/cosgy/textToSpeak/TextToSpeak.kt | 7 ++- .../cosgy/textToSpeak/audio/VoiceCreation.kt | 2 - 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/main/java/dev/cosgy/textToSpeak/Bot.kt b/src/main/java/dev/cosgy/textToSpeak/Bot.kt index 6eb02f6..0ebb274 100644 --- a/src/main/java/dev/cosgy/textToSpeak/Bot.kt +++ b/src/main/java/dev/cosgy/textToSpeak/Bot.kt @@ -33,6 +33,7 @@ import java.util.concurrent.Executors import java.util.concurrent.ScheduledExecutorService import java.util.concurrent.TimeUnit import java.util.function.Consumer +import javax.swing.JOptionPane import kotlin.system.exitProcess class Bot(val waiter: EventWaiter, val config: BotConfig, val settingsManager: SettingsManager) { @@ -74,46 +75,62 @@ class Bot(val waiter: EventWaiter, val config: BotConfig, val settingsManager: S fun shutdown() { if (shuttingDown) return shuttingDown = true - threadpool.shutdownNow() if (jda!!.status != JDA.Status.SHUTTING_DOWN) { jda!!.guilds.forEach(Consumer { g: Guild -> - g.audioManager.closeAudioConnection() - val ah = g.audioManager.sendingHandler as AudioHandler? - if (ah != null) { - ah.stopAndClear() - ah.player.destroy() + val am = g.audioManager + if (am.isConnected) { + am.closeAudioConnection() + val ah = am.sendingHandler as AudioHandler? + if (ah != null) { + ah.stopAndClear() + ah.player.destroy() + } } }) - // ここからJDAの終了処理 - jda!!.shutdown() + // Wait for any remaining tasks to complete before shutting down the thread pool + threadpool.shutdown() try { - if (!jda!!.awaitShutdown(10, TimeUnit.SECONDS)) { - jda!!.shutdownNow() - if (!jda!!.awaitShutdown(10, TimeUnit.SECONDS)) { - log.warn("JDAのシャットダウンがタイムアウトしました。") + if (!threadpool.awaitTermination(10, TimeUnit.SECONDS)) { + threadpool.shutdownNow() + if (!threadpool.awaitTermination(10, TimeUnit.SECONDS)) { + log.warn("Thread pool did not terminate") } } } catch (e: InterruptedException) { - jda!!.shutdownNow() + threadpool.shutdownNow() Thread.currentThread().interrupt() - log.warn("JDAのシャットダウンが中断されました。") + log.warn("Thread pool shutdown was interrupted") } + } - dictionary?.close() - // 一時ファイルを削除 - try { - FileUtils.cleanDirectory(File("tmp")) - FileUtils.cleanDirectory(File("wav")) - log.info("一時ファイルを削除しました。") - } catch (e: IOException) { - log.warn("一時ファイルの削除に失敗しました。") + // Shutdown JDA + jda?.shutdown() + try { + if (!jda!!.awaitShutdown(10, TimeUnit.SECONDS)) { + log.warn("JDA did not shutdown properly") } + } catch (e: InterruptedException) { + jda!!.shutdownNow() + Thread.currentThread().interrupt() + log.warn("JDA shutdown was interrupted") } + + dictionary?.close() + // Delete temporary files + try { + FileUtils.cleanDirectory(File("tmp")) + FileUtils.cleanDirectory(File("wav")) + log.info("Deleted temporary files") + } catch (e: IOException) { + log.warn("Failed to delete temporary files") + } + if (gui != null) gui!!.dispose() exitProcess(0) } + fun setGUI(gui: GUI?) { this.gui = gui } diff --git a/src/main/java/dev/cosgy/textToSpeak/TextToSpeak.kt b/src/main/java/dev/cosgy/textToSpeak/TextToSpeak.kt index a62b899..0d63c8e 100644 --- a/src/main/java/dev/cosgy/textToSpeak/TextToSpeak.kt +++ b/src/main/java/dev/cosgy/textToSpeak/TextToSpeak.kt @@ -39,6 +39,7 @@ import net.dv8tion.jda.api.entities.Activity import net.dv8tion.jda.api.requests.GatewayIntent import net.dv8tion.jda.api.utils.cache.CacheFlag import org.slf4j.LoggerFactory +import sun.misc.Signal import java.awt.Color import java.io.IOException import java.util.* @@ -169,6 +170,10 @@ System.exit(1); """.trimIndent()) exitProcess(1) } - Runtime.getRuntime().addShutdownHook(Thread { bot.shutdown() }) + + Signal.handle(Signal("INT")) { _ -> + println("プログラムを終了しています...") + bot.shutdown() + } } } \ No newline at end of file diff --git a/src/main/java/dev/cosgy/textToSpeak/audio/VoiceCreation.kt b/src/main/java/dev/cosgy/textToSpeak/audio/VoiceCreation.kt index e815b5b..fc0f017 100644 --- a/src/main/java/dev/cosgy/textToSpeak/audio/VoiceCreation.kt +++ b/src/main/java/dev/cosgy/textToSpeak/audio/VoiceCreation.kt @@ -110,8 +110,6 @@ class VoiceCreation( // 各種設定の値を保持するためのフィール return builder.toString() } - - // テキストファイルを作成するメソッド @Throws(FileNotFoundException::class, UnsupportedEncodingException::class) private fun createTmpTextFile(guildId: String, fileId: String, message: String): String {