Compare commits

...

2 commits

Author SHA1 Message Date
d928258028
Merge remote-tracking branch 'origin/master'
All checks were successful
Build Gradle project / build-gradle-project (push) Successful in 3m55s
2024-02-13 08:57:32 +01:00
8e1551cd6c
feat: added NewsManager, made bot dm news, when there are any at boot 2024-02-13 08:57:18 +01:00
2 changed files with 145 additions and 4 deletions

View file

@ -19,6 +19,7 @@
package net.moonleay.lilJudd package net.moonleay.lilJudd
import com.kotlindiscord.kord.extensions.ExtensibleBot import com.kotlindiscord.kord.extensions.ExtensibleBot
import com.kotlindiscord.kord.extensions.utils.dm
import dev.kord.common.entity.PresenceStatus import dev.kord.common.entity.PresenceStatus
import dev.kord.core.behavior.interaction.response.respond import dev.kord.core.behavior.interaction.response.respond
import dev.kord.core.event.gateway.ReadyEvent import dev.kord.core.event.gateway.ReadyEvent
@ -34,22 +35,20 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.moonleay.lilJudd.buttons.component.EditButtonManager import net.moonleay.lilJudd.buttons.component.EditButtonManager
import net.moonleay.lilJudd.data.CredentialManager import net.moonleay.lilJudd.data.CredentialManager
import net.moonleay.lilJudd.data.NewsManager
import net.moonleay.lilJudd.data.StacktraceSaver 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.data.database.DB
import net.moonleay.lilJudd.extensions.* import net.moonleay.lilJudd.extensions.*
import net.moonleay.lilJudd.features.AvailabilityManager import net.moonleay.lilJudd.features.AvailabilityManager
import net.moonleay.lilJudd.features.MatchManager import net.moonleay.lilJudd.features.MatchManager
import net.moonleay.lilJudd.features.TimeManager 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.EmbedColor
import net.moonleay.lilJudd.util.Logger import net.moonleay.lilJudd.util.Logger
import net.moonleay.lilJudd.util.MessageUtil import net.moonleay.lilJudd.util.MessageUtil
import net.moonleay.liljudd.build.BuildConstants import net.moonleay.liljudd.build.BuildConstants
import kotlin.system.exitProcess import kotlin.system.exitProcess
object Bot { object Bot {
//The kord object gets set at app launch //The kord object gets set at app launch
lateinit var bot: ExtensibleBot lateinit var bot: ExtensibleBot
@ -185,6 +184,28 @@ object Bot {
bot.kordRef.on<ReadyEvent> { bot.kordRef.on<ReadyEvent> {
AvailabilityManager.runThread() // Update Availabilities AvailabilityManager.runThread() // Update Availabilities
MatchManager.update() // Update Matches 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 // Make the bot update the status every 6 seconds
// JobManager.addJob(StatusUpdater) // JobManager.addJob(StatusUpdater)
} }

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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()
}
}
}