From 736c36d0691af40b8335b136f61353651c75dc2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wieland=20Sch=C3=B6bl?= Date: Sun, 21 Feb 2021 13:55:35 +0100 Subject: [PATCH] Change CLI to use kordx.commands --- .gitignore | 6 +- src/main/kotlin/de/wulkanat/AdminCli.kt | 92 -------- src/main/kotlin/de/wulkanat/OwnerCli.kt | 216 ------------------ .../kotlin/de/wulkanat/web/SiteWatcher.kt | 11 +- 4 files changed, 12 insertions(+), 313 deletions(-) delete mode 100644 src/main/kotlin/de/wulkanat/AdminCli.kt delete mode 100644 src/main/kotlin/de/wulkanat/OwnerCli.kt diff --git a/.gitignore b/.gitignore index fbc4ac1..4e81152 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,6 @@ servers.json config.json service_channels.json *.hprof -/build -/.gradle -.idea \ No newline at end of file +/build/ +/.gradle/ +/.idea/ \ No newline at end of file diff --git a/src/main/kotlin/de/wulkanat/AdminCli.kt b/src/main/kotlin/de/wulkanat/AdminCli.kt deleted file mode 100644 index dab1321..0000000 --- a/src/main/kotlin/de/wulkanat/AdminCli.kt +++ /dev/null @@ -1,92 +0,0 @@ -package de.wulkanat - -import de.wulkanat.files.ServiceChannels -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(prefix) - ) { - return - } - val command = Regex("[^\\s`]+|`[^`]*`").findAll(msg.removePrefix("!")).toList() - - when (command[0].value) { - "stop" -> exitProcess(1) - "fakeUpdate" -> { - SiteWatcher.newestBlog = BlogPostPreview( - title = "FakePost", - imgUrl = "", - fullPostUrl = "", - author = "wulkanat", - date = "now", - description = "Lorem Ipsum" - ) - - Admin.println("Posting on next update cycle.") - } - "info" -> { - Admin.info() - } - "serviceMessage" -> { - if (command.size != 3) { - Admin.println("Enclose message and title in backticks (`)") - } else { - ServiceChannels.sendServiceMessage(command[1].value.trim('`'), command[2].value.trim('`')) - } - } - "refreshList" -> { - ServiceChannels.channels = ServiceChannels.refreshChannelsFromDisk() - ServiceChannels.serviceChannels = ServiceChannels.refreshServiceChannelsFromDisk() - Admin.info() - } - "removeInactive" -> { - ServiceChannels.channels.removeAll { channel -> - ServiceChannels.testServerId(channel.id) ?: run { - Admin.println("Removed ${channel.id}") - null - } == null - } - Admin.info() - ServiceChannels.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/OwnerCli.kt b/src/main/kotlin/de/wulkanat/OwnerCli.kt deleted file mode 100644 index eb9089b..0000000 --- a/src/main/kotlin/de/wulkanat/OwnerCli.kt +++ /dev/null @@ -1,216 +0,0 @@ -package de.wulkanat - -import de.wulkanat.files.ServiceChannels -import net.dv8tion.jda.api.EmbedBuilder -import net.dv8tion.jda.api.Permission -import net.dv8tion.jda.api.events.message.MessageReceivedEvent -import net.dv8tion.jda.api.hooks.ListenerAdapter -import java.awt.Color - -class OwnerCli : ListenerAdapter() { - private val prefix = "%!" - - override fun onMessageReceived(event: MessageReceivedEvent) { - val msg = event.message.contentRaw - // Only accept admin requests - if (event.message.member?.hasPermission(Permission.ADMINISTRATOR) != true || !msg.startsWith(prefix)) { - return - } - - val command = msg.removePrefix(prefix).split(Regex("\\s+")) - val channelId = event.message.channel.idLong - - when (command.first()) { - "add" -> { - val result = ServiceChannels.addChannel(channelId, null) - if (result == null) { - event.message.channel.sendMessage("Already added.").queue() - } else { - event.message.channel.sendMessage("Added.").queue() - Admin.info() - } - } - "remove" -> { - val result = ServiceChannels.channels.removeAll { it.id == channelId } - ServiceChannels.saveChannels() - if (result) { - event.message.channel.sendMessage("Removed.").queue() - } else { - event.message.channel.sendMessage("This channel is not registered.").queue() - } - } - "publish" -> { - val result = ServiceChannels.channels.find { it.id == channelId } - if (result != null) { - if (command.size > 1 && listOf("on", "off").contains(command[1])) { - result.autoPublish = command[1] == "on" - ServiceChannels.saveChannels() - - event.message.channel.sendMessage("Auto publish is now ${command[1]}").queue() - } else { - event.message.channel.sendMessage("Usage: `${prefix}publish [on|off]`") - } - } else { - event.message.channel.sendMessage("Channel not registered.").queue() - } - } - "ping" -> { - val result = ServiceChannels.channels.find { it.id == channelId } - if (result != null) { - if (command.size > 1) { - val roles = event.message.guild.getRolesByName(command[1], false) - result.mentionedRole = when { - command[1] == "everyone" -> { - event.message.channel.sendMessage("Now pinging everyone.").queue() - "everyone" - } - command[1] == "none" -> { - event.message.channel.sendMessage("Now pinging none.").queue() - null - } - roles.firstOrNull() != null -> { - event.message.channel.sendMessage("Now pinging ${roles.first().name}").queue() - roles.first().id - } - else -> { - event.message.channel.sendMessage("Unknown role.").queue() - result.mentionedRole - } - } - ServiceChannels.saveChannels() - } else { - event.message.channel.sendMessage("Usage: `${prefix}ping [everyone|none|roleName]`") - } - } else { - event.message.channel.sendMessage("Channel is not registered.").queue() - } - } - "setMessage" -> { - val result = ServiceChannels.channels.find { it.id == channelId } - if (result != null) { - if (command.size > 1) { - val message = event.message.contentRaw.removePrefix("${prefix}setMessage").trim() - result.message = CustomMessage(message) - ServiceChannels.saveChannels() - event.message.channel.sendMessage("Set `$message` as message.").queue() - } else { - event.message.channel.sendMessage("Usage: `${prefix}setMessage [message]`") - } - } else { - event.message.channel.sendMessage("Channel is not registered.").queue() - } - } - "resetMessage" -> { - val result = ServiceChannels.channels.find { it.id == channelId } - if (result != null) { - result.message = null - ServiceChannels.saveChannels() - event.message.channel.sendMessage("Reset to no message.").queue() - } else { - event.message.channel.sendMessage("Channel is not registered.").queue() - } - } - "serviceChannel" -> { - if (command.size > 1 && listOf("add", "remove").contains(command[1])) { - if (command[1] == "add") { - if (ServiceChannels.serviceChannels.find { it.id == channelId } != null) { - event.message.channel.sendMessage("Already a service channel.").queue() - } else { - ServiceChannels.serviceChannels.add(ServiceChannel(channelId)) - ServiceChannels.saveChannels() - event.message.channel.sendMessage("Added as service channel.").queue() - } - } else { - event.message.channel.sendMessage( - if (ServiceChannels.serviceChannels.removeAll { it.id == channelId }) "Channel removed." - else "Not a service channel." - ).queue() - } - ServiceChannels.saveChannels() - } else { - event.message.channel.sendMessage("Usage: `${prefix}serviceChannel [add|remove]`") - } - - } - "publishMessage" -> { - val result = ServiceChannels.channels.find { it.id == channelId } - if (result != null) { - if (result.message != null) { - if (command.size > 1 && listOf("on", "off").contains(command[1])) { - result.message?.pushAnnouncement = command[1] == "on" - ServiceChannels.saveChannels() - - event.message.channel.sendMessage("Auto publish (message) is now ${command[1]}").queue() - } else { - event.message.channel.sendMessage("Usage: `${prefix}publishMessage [on|off]`") - } - } else { - event.message.channel.sendMessage("Channel has no custom message.").queue() - } - } else { - event.message.channel.sendMessage("Channel not registered.").queue() - } - } - "info" -> { - event.message.channel.sendMessage( - EmbedBuilder() - .setTitle("Server overview") - .setColor(Color.GREEN) - .setDescription(""" - ${ServiceChannels.getServerNames(event.message.guild.idLong).joinToString("\n")} - - **_Service Channels_** - ${ServiceChannels.getServiceChannelServers(event.message.guild.idLong).joinToString("\n")} - """.trimIndent()) - .setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl) - .build() - ).queue() - } - "report" -> { - val errorReport = event.message.contentRaw.removePrefix("${prefix}report") - Admin.error(event.message.guild.name, errorReport, event.author) - event.message.channel.sendMessage( - EmbedBuilder() - .setTitle("Error Report Received") - .setColor(Color.RED) - .setDescription(errorReport) - .setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl) - .build() - ).queue() - } - "help" -> { - event.message.channel.sendMessage( - EmbedBuilder() - .setTitle("Help") - .setColor(Color.YELLOW) - .setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl) - .setDescription( - """ - **${prefix}add** - Add this channel to the notified list - **${prefix}serviceChannel [add|remove]** - Add or remove this channel to receive service message from the bot developer (recommended) - **${prefix}remove** - Remove this channel to the notified list - **${prefix}publish [on|off]** - [Community|Partner|Verified only] Auto publish the message if in an announcement channel - **${prefix}ping [none|everyone|roleName]** - What role to ping - **${prefix}setMessage [message]** - Set a custom message to show - **${prefix}resetMessage** - Reset the message - **${prefix}info** - Show an overview about all channels registered on this server - **${prefix}report** - Report an issue to the Bot Admin (this will share your user name so they can contact you) - **${prefix}help** - Show this message - """.trimIndent() - ) - .build() - ).queue() - } - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/de/wulkanat/web/SiteWatcher.kt b/src/main/kotlin/de/wulkanat/web/SiteWatcher.kt index da52100..fd1db97 100644 --- a/src/main/kotlin/de/wulkanat/web/SiteWatcher.kt +++ b/src/main/kotlin/de/wulkanat/web/SiteWatcher.kt @@ -3,6 +3,8 @@ package de.wulkanat.web import de.wulkanat.Admin import de.wulkanat.DiscordRpc import de.wulkanat.model.BlogPostPreview +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import org.jsoup.Jsoup import java.io.IOException @@ -11,9 +13,14 @@ object SiteWatcher { var newestBlog: BlogPostPreview? = null private var siteOnline = false - fun hasNewBlogPost(): Boolean { + suspend fun hasNewBlogPost(): Boolean { try { - val doc = Jsoup.connect(BLOG_INDEX_URL).get() + val doc = withContext(Dispatchers.IO) { + // solved by `withContext` + // https://stackoverflow.com/a/63332658 + @Suppress("BlockingMethodInNonBlockingContext") + Jsoup.connect(BLOG_INDEX_URL).get() + } val newBlog = BlogPostParser.getFistBlog(doc) if (newestBlog == newBlog) {