From 906c41be88fb87bfa43626ff5d403a29bade0841 Mon Sep 17 00:00:00 2001 From: moonleay Date: Wed, 31 Jan 2024 13:47:14 +0100 Subject: [PATCH] feat: make the bot save a stacktrace, if an error occurs during a command or interaction Signed-off-by: moonleay --- src/main/kotlin/net/moonleay/lilJudd/Bot.kt | 26 +++++++++++-- .../moonleay/lilJudd/data/StacktraceSaver.kt | 37 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/net/moonleay/lilJudd/data/StacktraceSaver.kt diff --git a/src/main/kotlin/net/moonleay/lilJudd/Bot.kt b/src/main/kotlin/net/moonleay/lilJudd/Bot.kt index 4196213..7064745 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/Bot.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/Bot.kt @@ -26,12 +26,15 @@ import dev.kord.core.event.interaction.ButtonInteractionCreateEvent import dev.kord.core.on import dev.kord.gateway.Intent import dev.kord.gateway.PrivilegedIntent +import dev.kord.gateway.builder.Shards +import dev.kord.rest.builder.message.embed import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.launch import net.moonleay.lilJudd.buttons.component.EditButtonManager import net.moonleay.lilJudd.data.CredentialManager +import net.moonleay.lilJudd.data.StacktraceSaver import net.moonleay.lilJudd.data.api.splatoon3ink.Splatoon3Api import net.moonleay.lilJudd.data.database.DB import net.moonleay.lilJudd.extensions.* @@ -125,11 +128,26 @@ object Bot { +Intent.GuildMembers } - // Will add Sharding someday, I promise - /* - sharding { recommended -> + errorResponse { _, type -> + val stamp = System.currentTimeMillis() + this.embed { + this.title = "Oops. Something went wrong." + this.description = "The bot encountered an error during execution.\n" + + "Please report this to <@${BuildConstants.ownerID}>.\n" + + "The errorid is \"$stamp.stk\"" + this.field { + this.name = "Error message:" + this.value = type.error.message.toString() + this.inline = false + } + } + + StacktraceSaver.saveStacktrace(type.error, stamp) + } + + this.sharding { recommended -> Shards(recommended) - } */ + } } // Register button presses diff --git a/src/main/kotlin/net/moonleay/lilJudd/data/StacktraceSaver.kt b/src/main/kotlin/net/moonleay/lilJudd/data/StacktraceSaver.kt new file mode 100644 index 0000000..b84ea13 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/data/StacktraceSaver.kt @@ -0,0 +1,37 @@ +/* + * lilJudd + * Copyright (C) 2024 moonleay + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.moonleay.lilJudd.data + +import java.io.File + +object StacktraceSaver { + fun saveStacktrace(stacktrace: Throwable, timestamp: Long) { + createFolder() + val dir = File("data", "stacktraces") + File(dir, "$timestamp.stk").bufferedWriter().use { out -> + out.write(stacktrace.stackTraceToString()) + } + } + + private fun createFolder() { + val dir = File("data", "stacktraces") + if (!dir.exists()) + dir.mkdir() + } +}