From 0877883e3c3f918b2bcb5c859f436bea563442b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wieland=20Sch=C3=B6bl?= Date: Mon, 31 Aug 2020 20:00:54 +0200 Subject: [PATCH] fix crash --- .idea/codeStyles/Project.xml | 16 +++++ .idea/workspace.xml | 78 +++++++++++++------------ README.md | 2 + build.gradle | 2 +- src/main/kotlin/de/wulkanat/AdminCli.kt | 43 +++++++++++++- src/main/kotlin/de/wulkanat/Channels.kt | 7 +-- 6 files changed, 105 insertions(+), 43 deletions(-) diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 1bec35e..31d977a 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,6 +1,22 @@ + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f503bfd..33a3ac4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,20 +1,12 @@ - - - - + + - - - - - - - + - @@ -268,6 +269,10 @@ + + + + @@ -284,54 +289,54 @@ - + - + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + @@ -341,13 +346,14 @@ - + - - - + + + + @@ -363,7 +369,7 @@ file://$PROJECT_DIR$/src/main/kotlin/de/wulkanat/AdminCli.kt - 18 + 22 diff --git a/README.md b/README.md index ea85ad4..30cc982 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ I developed it under Windows, and had some trouble compiling it on Linux. You mi | !serviceMessage | message | Send a service message to all registered channels | | !fakeUpdate | | Cause a fake update (**WARNING**: This will show on **ALL** registered servers) | | !refreshList | | Refresh servers and service channels from disk (if you manually edit the JSON files) | +| !removeInactive | | Remove inactive channels | +| !help | | Show a help dialog with all these commands | These commands will only work by private messaging the bot (and will be ignored if they don't come from the admin registered in the `admin.json`. diff --git a/build.gradle b/build.gradle index 825896d..67acd02 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'de.wulkanat' -version '1.4' +version '1.4.1' repositories { mavenCentral() diff --git a/src/main/kotlin/de/wulkanat/AdminCli.kt b/src/main/kotlin/de/wulkanat/AdminCli.kt index dc40ea8..945c0b7 100644 --- a/src/main/kotlin/de/wulkanat/AdminCli.kt +++ b/src/main/kotlin/de/wulkanat/AdminCli.kt @@ -3,14 +3,18 @@ package de.wulkanat import de.wulkanat.model.BlogPostPreview import net.dv8tion.jda.api.hooks.ListenerAdapter import de.wulkanat.web.SiteWatcher +import net.dv8tion.jda.api.EmbedBuilder import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent +import java.awt.Color import kotlin.system.exitProcess class AdminCli : ListenerAdapter() { + val prefix = "!" + override fun onPrivateMessageReceived(event: PrivateMessageReceivedEvent) { val msg = event.message.contentRaw if (event.author.idLong != Admin.userId || - !msg.startsWith("!") + !msg.startsWith(prefix) ) { return } @@ -45,6 +49,43 @@ class AdminCli : ListenerAdapter() { Channels.serviceChannels = Channels.refreshServiceChannelsFromDisk() Admin.info() } + "removeInactive" -> { + Channels.channels.removeAll { channel -> + Channels.testServerId(channel.id) ?: run { + Admin.println("Removed ${channel.id}") + null + } == null + } + Admin.info() + Channels.saveChannels() + } + "help" -> { + event.message.channel.sendMessage( + EmbedBuilder() + .setTitle("Help") + .setColor(Color.YELLOW) + .setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl) + .setDescription( + """ + **${prefix}stop** + Stop the bot + **${prefix}fakeUpdate** + Post a fake update to every registered channel (can be used if bot missed the update) + **${prefix}info** + Show an overview over all registered channels + **${prefix}serviceMessage [title] [message]** + Show a service message (update info etc) to all registered service channels + **${prefix}refreshList** + Refresh server list from disk + **${prefix}removeInactive** + Remove inactive channels + **${prefix}help** + Show this message + """.trimIndent() + ) + .build() + ).queue() + } } } } \ No newline at end of file diff --git a/src/main/kotlin/de/wulkanat/Channels.kt b/src/main/kotlin/de/wulkanat/Channels.kt index 7307463..aaef499 100644 --- a/src/main/kotlin/de/wulkanat/Channels.kt +++ b/src/main/kotlin/de/wulkanat/Channels.kt @@ -1,15 +1,12 @@ package de.wulkanat import de.wulkanat.extensions.crosspost -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonConfiguration import kotlinx.serialization.list import net.dv8tion.jda.api.EmbedBuilder import net.dv8tion.jda.api.JDA import net.dv8tion.jda.api.Permission import net.dv8tion.jda.api.entities.MessageEmbed import net.dv8tion.jda.api.entities.TextChannel -import net.dv8tion.jda.api.exceptions.ErrorResponseException import java.awt.Color object Channels { @@ -53,8 +50,8 @@ object Channels { it.crosspost().queue() } } - } catch (e: ErrorResponseException) { - Admin.error("Error in server", e.message ?: e.localizedMessage) + } catch (e: Exception) { + Admin.error("Error in server ${channel_pair.id}", e.message ?: e.localizedMessage) } } }