From 8e1551cd6c3bcdc9867e1f3a438eea5bb985c9b3 Mon Sep 17 00:00:00 2001 From: moonleay Date: Thu, 1 Feb 2024 23:15:04 +0100 Subject: [PATCH] feat: added NewsManager, made bot dm news, when there are any at boot --- build.gradle.kts | 2 +- src/main/kotlin/net/moonleay/lilJudd/Bot.kt | 35 ++++- .../net/moonleay/lilJudd/data/NewsManager.kt | 120 ++++++++++++++++++ 3 files changed, 149 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/net/moonleay/lilJudd/data/NewsManager.kt diff --git a/build.gradle.kts b/build.gradle.kts index 7fb8244..eac9d28 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -33,7 +33,7 @@ val ownerID = 372703841151614976L group = "net.moonleay.liljudd" version = System.getenv("CI_COMMIT_TAG")?.let { "$it-${System.getenv("CI_COMMIT_SHORT_SHA")}-prod" } ?: System.getenv("CI_COMMIT_SHORT_SHA")?.let { "$it-dev" } - ?: "2.7.1" + ?: "2.7.2" val kordver = "1.7.1-SNAPSHOT" val coroutinesver = "1.7.3" diff --git a/src/main/kotlin/net/moonleay/lilJudd/Bot.kt b/src/main/kotlin/net/moonleay/lilJudd/Bot.kt index 7064745..23796dd 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/Bot.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/Bot.kt @@ -19,6 +19,7 @@ package net.moonleay.lilJudd import com.kotlindiscord.kord.extensions.ExtensibleBot +import com.kotlindiscord.kord.extensions.utils.dm import dev.kord.common.entity.PresenceStatus import dev.kord.core.behavior.interaction.response.respond import dev.kord.core.event.gateway.ReadyEvent @@ -34,22 +35,20 @@ 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.NewsManager 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.* import net.moonleay.lilJudd.features.AvailabilityManager import net.moonleay.lilJudd.features.MatchManager import net.moonleay.lilJudd.features.TimeManager -import net.moonleay.lilJudd.jobs.Splatoon3ApiScheduleUpdateScheduler -import net.moonleay.lilJudd.jobs.StatusUpdater -import net.moonleay.lilJudd.jobs.component.JobManager import net.moonleay.lilJudd.util.EmbedColor import net.moonleay.lilJudd.util.Logger import net.moonleay.lilJudd.util.MessageUtil import net.moonleay.liljudd.build.BuildConstants import kotlin.system.exitProcess + object Bot { //The kord object gets set at app launch lateinit var bot: ExtensibleBot @@ -185,13 +184,35 @@ object Bot { bot.kordRef.on { AvailabilityManager.runThread() // Update Availabilities MatchManager.update() // Update Matches + // Load news + NewsManager.load() + if(NewsManager.shouldPost == "yes"){ + bot.kordRef.guilds.collect { + val owner = it.owner.asUser() + Logger.out("Sent News to ${owner.username} from ${it.name}") + owner.dm { + this.embed { + this.title = NewsManager.title + this.description = NewsManager.news + this.footer { + this.icon = bot.kordRef.getSelf().avatar?.cdnUrl?.toUrl() + this.text = MessageUtil.getFooter() + } + } + } + } + NewsManager.shouldPost = "no" + NewsManager.update() + } + + // Make the bot update the status every 6 seconds - JobManager.addJob(StatusUpdater) +// JobManager.addJob(StatusUpdater) } // Update the Splatoon 3 api data and make sure it stays up-to-date - Splatoon3Api.updateSchedule() - JobManager.addJob(Splatoon3ApiScheduleUpdateScheduler) +// Splatoon3Api.updateSchedule() +// JobManager.addJob(Splatoon3ApiScheduleUpdateScheduler) /* Other caches will be added when implemented its not used yet in order to reduce load on the api, diff --git a/src/main/kotlin/net/moonleay/lilJudd/data/NewsManager.kt b/src/main/kotlin/net/moonleay/lilJudd/data/NewsManager.kt new file mode 100644 index 0000000..9db6a8c --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/data/NewsManager.kt @@ -0,0 +1,120 @@ +/* + * 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.* +import java.util.* + +object NewsManager { + private const val foldername = "data" + private const val filename = "news.nick" + lateinit var shouldPost: String + lateinit var title: String + lateinit var news: String + + ///Load the needed credentials, generate a config if there is none + fun load() { + val folder = File(foldername) + if (!folder.exists()) { + save() + return + } + val configFile = File(folder, filename) + if (!configFile.exists()) { + save() + return + } + try { + val input: InputStream = FileInputStream(foldername + File.separator + filename) + val prop = Properties() + prop.load(input) + shouldPost = prop.getProperty("shouldPost") + title = prop.getProperty("title") + news = prop.getProperty("news") + input.close() + } catch (e: IOException) { + e.printStackTrace() + } + } + + fun update(){ + val folder = File(foldername) + if (!folder.exists()) { + try { + folder.mkdirs() + } catch (e: IOException) { + e.printStackTrace() + } + } + val configFile = File(foldername + File.separator + filename) + if (!configFile.exists()) { + try { + configFile.createNewFile() + } catch (e: IOException) { + e.printStackTrace() + } + } + try { + val output: OutputStream = FileOutputStream(foldername + File.separator + filename) + val prop = Properties() + prop.setProperty("shouldPost", shouldPost) + prop.setProperty("title", title) + prop.setProperty("news", news) + prop.store(output, null) + output.close() + } catch (e: IOException) { + e.printStackTrace() + } + } + + ///generate a new sample config + private fun save() { + val folder = File(foldername) + if (!folder.exists()) { + try { + folder.mkdirs() + } catch (e: IOException) { + e.printStackTrace() + } + } + val configFile = File(foldername + File.separator + filename) + if (!configFile.exists()) { + try { + configFile.createNewFile() + } catch (e: IOException) { + e.printStackTrace() + } + } + try { + val output: OutputStream = FileOutputStream(foldername + File.separator + filename) + val prop = Properties() + prop.setProperty("shouldPost", "no") + prop.setProperty("title", "empty") + prop.setProperty("news", "empty") + prop.store(output, null) + output.close() + + shouldPost = "no" + title = "empty" + news = "empty" + } catch (e: IOException) { + e.printStackTrace() + } + } +}