forked from DiscordBots/lilJudd
feat: added NewsManager, made bot dm news, when there are any at boot
This commit is contained in:
parent
906c41be88
commit
8e1551cd6c
3 changed files with 149 additions and 8 deletions
|
@ -33,7 +33,7 @@ val ownerID = 372703841151614976L
|
||||||
group = "net.moonleay.liljudd"
|
group = "net.moonleay.liljudd"
|
||||||
version = System.getenv("CI_COMMIT_TAG")?.let { "$it-${System.getenv("CI_COMMIT_SHORT_SHA")}-prod" }
|
version = System.getenv("CI_COMMIT_TAG")?.let { "$it-${System.getenv("CI_COMMIT_SHORT_SHA")}-prod" }
|
||||||
?: System.getenv("CI_COMMIT_SHORT_SHA")?.let { "$it-dev" }
|
?: System.getenv("CI_COMMIT_SHORT_SHA")?.let { "$it-dev" }
|
||||||
?: "2.7.1"
|
?: "2.7.2"
|
||||||
|
|
||||||
val kordver = "1.7.1-SNAPSHOT"
|
val kordver = "1.7.1-SNAPSHOT"
|
||||||
val coroutinesver = "1.7.3"
|
val coroutinesver = "1.7.3"
|
||||||
|
|
|
@ -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,13 +184,35 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the Splatoon 3 api data and make sure it stays up-to-date
|
// Update the Splatoon 3 api data and make sure it stays up-to-date
|
||||||
Splatoon3Api.updateSchedule()
|
// Splatoon3Api.updateSchedule()
|
||||||
JobManager.addJob(Splatoon3ApiScheduleUpdateScheduler)
|
// JobManager.addJob(Splatoon3ApiScheduleUpdateScheduler)
|
||||||
/*
|
/*
|
||||||
Other caches will be added when implemented
|
Other caches will be added when implemented
|
||||||
its not used yet in order to reduce load on the api,
|
its not used yet in order to reduce load on the api,
|
||||||
|
|
120
src/main/kotlin/net/moonleay/lilJudd/data/NewsManager.kt
Normal file
120
src/main/kotlin/net/moonleay/lilJudd/data/NewsManager.kt
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue