big bang
Signed-off-by: moonleay <contact@moonleay.net>
This commit is contained in:
commit
5a39f49824
35 changed files with 2892 additions and 0 deletions
95
src/main/kotlin/net/moonleay/rssbot/Bot.kt
Normal file
95
src/main/kotlin/net/moonleay/rssbot/Bot.kt
Normal file
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot
|
||||
|
||||
import com.kotlindiscord.kord.extensions.ExtensibleBot
|
||||
import dev.kord.common.entity.PresenceStatus
|
||||
import dev.kord.core.event.gateway.ReadyEvent
|
||||
import dev.kord.core.on
|
||||
import net.moonleay.rssbot.data.CredentialManager
|
||||
import net.moonleay.rssbot.data.database.DB
|
||||
import net.moonleay.rssbot.extensions.FeedExtension
|
||||
import net.moonleay.rssbot.extensions.StuffExtension
|
||||
import net.moonleay.rssbot.jobs.FeedUpdater
|
||||
import net.moonleay.rssbot.jobs.component.JobManager
|
||||
import net.moonleay.rssbot.util.Logger
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
object Bot {
|
||||
//The kord object gets set at app launch
|
||||
lateinit var bot: ExtensibleBot
|
||||
|
||||
suspend fun start() {
|
||||
Logger.out("Starting Bot...")
|
||||
|
||||
// Load config
|
||||
CredentialManager.load()
|
||||
|
||||
// Don't run the bot when there is no bot token in config
|
||||
if (CredentialManager.token == "empty") {
|
||||
Logger.out("The config does not contain a bot token.")
|
||||
exitProcess(3)
|
||||
}
|
||||
|
||||
// Check if the credentials for the Database are existent, don't run if they are missing
|
||||
if (CredentialManager.dbDomain == "empty" || CredentialManager.dbName == "empty" || CredentialManager.dbUser == "empty" || CredentialManager.dbPassword == "empty") {
|
||||
Logger.out("The config does not contain the whole Database credentials.")
|
||||
exitProcess(3)
|
||||
}
|
||||
|
||||
// Connect to the database
|
||||
DB.connect(
|
||||
CredentialManager.dbDomain,
|
||||
CredentialManager.dbName,
|
||||
CredentialManager.dbUser,
|
||||
CredentialManager.dbPassword
|
||||
)
|
||||
// Make sure the database is up-to-date
|
||||
DB.register()
|
||||
|
||||
// Register all the jobs
|
||||
JobManager.addJob(FeedUpdater)
|
||||
|
||||
// Create the bot object
|
||||
bot = ExtensibleBot(CredentialManager.token) {
|
||||
applicationCommands {
|
||||
enabled = true
|
||||
}
|
||||
|
||||
extensions {
|
||||
add(::FeedExtension)
|
||||
add(::StuffExtension)
|
||||
}
|
||||
|
||||
this.presence {
|
||||
this.status = PresenceStatus.DoNotDisturb
|
||||
this.listening("rss feeds")
|
||||
}
|
||||
}
|
||||
|
||||
bot.kordRef.on<ReadyEvent> {
|
||||
FeedUpdater.update(true)
|
||||
}
|
||||
|
||||
|
||||
//Start the bot
|
||||
bot.start()
|
||||
}
|
||||
|
||||
}
|
27
src/main/kotlin/net/moonleay/rssbot/Main.kt
Normal file
27
src/main/kotlin/net/moonleay/rssbot/Main.kt
Normal file
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot
|
||||
|
||||
import net.moonleay.rssbot.build.BuildConstants
|
||||
import net.moonleay.rssbot.util.Logger
|
||||
|
||||
suspend fun main() {
|
||||
Logger.out("v.${BuildConstants.version}")
|
||||
Bot.start()
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.data
|
||||
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
|
||||
object CredentialManager {
|
||||
private const val foldername = "data"
|
||||
private const val filename = "credentials.nils"
|
||||
lateinit var token: String
|
||||
lateinit var dbDomain: String
|
||||
lateinit var dbName: String
|
||||
lateinit var dbUser: String
|
||||
lateinit var dbPassword: 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)
|
||||
token = prop.getProperty("token")
|
||||
dbDomain = prop.getProperty("dbDomain")
|
||||
dbName = prop.getProperty("dbName")
|
||||
dbUser = prop.getProperty("dbUser")
|
||||
dbPassword = prop.getProperty("dbPassword")
|
||||
input.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("token", "empty")
|
||||
prop.setProperty("dbDomain", "empty")
|
||||
prop.setProperty("dbName", "empty")
|
||||
prop.setProperty("dbUser", "empty")
|
||||
prop.setProperty("dbPassword", "empty")
|
||||
prop.store(output, null)
|
||||
output.close()
|
||||
|
||||
token = "empty"
|
||||
dbDomain = "empty"
|
||||
dbName = "empty"
|
||||
dbUser = "empty"
|
||||
dbPassword = "empty"
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
51
src/main/kotlin/net/moonleay/rssbot/data/database/DB.kt
Normal file
51
src/main/kotlin/net/moonleay/rssbot/data/database/DB.kt
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.data.database
|
||||
|
||||
import net.moonleay.rssbot.data.database.tables.RSSTable
|
||||
import net.moonleay.rssbot.data.database.tables.SubscriptionsTable
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.jetbrains.exposed.sql.SchemaUtils
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
object DB {
|
||||
private var connected = false
|
||||
|
||||
//Connect to the provided DB; trows errors, if the DB is not available.
|
||||
fun connect(dbDomain: String, dbName: String, dbUser: String, dbPasswd: String) {
|
||||
Database.connect(
|
||||
"jdbc:postgresql://$dbDomain/$dbName",
|
||||
driver = "org.postgresql.Driver",
|
||||
user = dbUser,
|
||||
password = dbPasswd
|
||||
)
|
||||
connected = true
|
||||
}
|
||||
|
||||
fun register() {
|
||||
if (!connected)
|
||||
return
|
||||
// Register tables here
|
||||
transaction {
|
||||
SchemaUtils.create(SubscriptionsTable)
|
||||
SchemaUtils.create(RSSTable)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.data.database.entry
|
||||
|
||||
data class RSSData(
|
||||
var guid: String,
|
||||
var subscriptionId: Int,
|
||||
)
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.data.database.entry
|
||||
|
||||
import net.moonleay.rssbot.extensions.components.FeedColor
|
||||
|
||||
data class SubscriptionData(
|
||||
val id: Int,
|
||||
val serverId: Long,
|
||||
val channelId: Long,
|
||||
val feedColor: FeedColor,
|
||||
val feedName: String,
|
||||
val feedUrl: String,
|
||||
)
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.data.database.repository
|
||||
|
||||
import net.moonleay.rssbot.data.database.entry.RSSData
|
||||
import net.moonleay.rssbot.data.database.tables.RSSTable
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.and
|
||||
import org.jetbrains.exposed.sql.deleteWhere
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.select
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
object RSSRepository {
|
||||
fun exists(data: RSSData): Boolean = transaction {
|
||||
RSSTable.select {
|
||||
(RSSTable.guid eq data.guid) and (RSSTable.subscriptionId eq data.subscriptionId)
|
||||
}.count() > 0
|
||||
}
|
||||
|
||||
fun write(data: RSSData) = transaction {
|
||||
RSSTable.insert {
|
||||
it[guid] = data.guid
|
||||
it[subscriptionId] = data.subscriptionId
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun delete(subid: Int) = transaction {
|
||||
RSSTable.deleteWhere {
|
||||
RSSTable.subscriptionId eq subid
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.data.database.repository
|
||||
|
||||
import net.moonleay.rssbot.data.database.entry.SubscriptionData
|
||||
import net.moonleay.rssbot.data.database.tables.SubscriptionsTable
|
||||
import net.moonleay.rssbot.util.EmbedUtil
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
object SubscriptionRepository {
|
||||
|
||||
fun getAll(): List<SubscriptionData> {
|
||||
val dataList = mutableListOf<SubscriptionData>()
|
||||
transaction {
|
||||
SubscriptionsTable.selectAll().forEach {
|
||||
dataList.add(
|
||||
SubscriptionData(
|
||||
it[SubscriptionsTable.id],
|
||||
it[SubscriptionsTable.serverId],
|
||||
it[SubscriptionsTable.channelId],
|
||||
EmbedUtil.getColorFromString(it[SubscriptionsTable.subscriptionColor]),
|
||||
it[SubscriptionsTable.subscriptionName],
|
||||
it[SubscriptionsTable.feedUrl]
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
return dataList
|
||||
}
|
||||
|
||||
|
||||
fun get(channelId: Long, feedName: String): SubscriptionData? {
|
||||
var data: SubscriptionData? = null
|
||||
transaction {
|
||||
SubscriptionsTable.select {
|
||||
(SubscriptionsTable.channelId eq channelId) and (SubscriptionsTable.subscriptionName eq feedName)
|
||||
}.forEach {
|
||||
data = SubscriptionData(
|
||||
it[SubscriptionsTable.id],
|
||||
it[SubscriptionsTable.serverId],
|
||||
it[SubscriptionsTable.channelId],
|
||||
EmbedUtil.getColorFromString(it[SubscriptionsTable.subscriptionColor]),
|
||||
it[SubscriptionsTable.subscriptionName],
|
||||
it[SubscriptionsTable.feedUrl]
|
||||
)
|
||||
}
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
fun exists(channelId: Long, feedName: String): Boolean {
|
||||
var exists = false
|
||||
transaction {
|
||||
exists = SubscriptionsTable.select {
|
||||
(SubscriptionsTable.channelId eq channelId) and (SubscriptionsTable.subscriptionName eq feedName)
|
||||
}.count() > 0
|
||||
}
|
||||
return exists
|
||||
}
|
||||
|
||||
|
||||
fun write(data: SubscriptionData) = transaction {
|
||||
SubscriptionsTable.insert {
|
||||
it[serverId] = data.serverId
|
||||
it[channelId] = data.channelId
|
||||
it[subscriptionColor] = data.feedColor.readableName
|
||||
it[subscriptionName] = data.feedName
|
||||
it[feedUrl] = data.feedUrl
|
||||
}
|
||||
}
|
||||
|
||||
fun delete(channelId: Long, feedName: String) = transaction {
|
||||
SubscriptionsTable.deleteWhere {
|
||||
(SubscriptionsTable.channelId eq channelId) and (SubscriptionsTable.subscriptionName eq feedName)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.data.database.tables
|
||||
|
||||
import org.jetbrains.exposed.sql.Table
|
||||
|
||||
|
||||
object RSSTable : Table(name = "rss") {
|
||||
val guid = text("guid").uniqueIndex()
|
||||
val subscriptionId = integer("subscription_id") references SubscriptionsTable.id
|
||||
override val primaryKey = PrimaryKey(guid, subscriptionId, name = "PK_RSS")
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.data.database.tables
|
||||
|
||||
import org.jetbrains.exposed.sql.Table
|
||||
|
||||
|
||||
object SubscriptionsTable : Table(name = "subscriptions") {
|
||||
val id = integer("id").autoIncrement().uniqueIndex()
|
||||
val serverId = long("server_id")
|
||||
val channelId = long("channel_id")
|
||||
val subscriptionColor = text("subscription_color")
|
||||
val subscriptionName = text("subscription_name")
|
||||
val feedUrl = text("feed_url")
|
||||
}
|
134
src/main/kotlin/net/moonleay/rssbot/extensions/FeedExtension.kt
Normal file
134
src/main/kotlin/net/moonleay/rssbot/extensions/FeedExtension.kt
Normal file
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.extensions
|
||||
|
||||
import com.kotlindiscord.kord.extensions.commands.Arguments
|
||||
import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.enumChoice
|
||||
import com.kotlindiscord.kord.extensions.commands.converters.impl.string
|
||||
import com.kotlindiscord.kord.extensions.extensions.Extension
|
||||
import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand
|
||||
import com.kotlindiscord.kord.extensions.types.respond
|
||||
import com.prof18.rssparser.RssParser
|
||||
import com.prof18.rssparser.model.RssItem
|
||||
import net.moonleay.rssbot.data.database.entry.RSSData
|
||||
import net.moonleay.rssbot.data.database.entry.SubscriptionData
|
||||
import net.moonleay.rssbot.data.database.repository.RSSRepository
|
||||
import net.moonleay.rssbot.data.database.repository.SubscriptionRepository
|
||||
import net.moonleay.rssbot.data.database.tables.SubscriptionsTable
|
||||
import net.moonleay.rssbot.extensions.components.FeedColor
|
||||
import net.moonleay.rssbot.util.EmbedColor
|
||||
import net.moonleay.rssbot.util.Logger
|
||||
import net.moonleay.rssbot.util.MessageUtil
|
||||
|
||||
class FeedExtension : Extension() {
|
||||
|
||||
override val name = "feed"
|
||||
override val allowApplicationCommandInDMs: Boolean
|
||||
get() = false
|
||||
|
||||
|
||||
override suspend fun setup() {
|
||||
ephemeralSlashCommand(::FeedArguments) {
|
||||
name = "feed"
|
||||
description = "Feed this channel a new feed"
|
||||
this.action {
|
||||
val feedName = arguments.feedName
|
||||
val feedColor = arguments.feedColor
|
||||
val feedUrl = arguments.feedUrl
|
||||
val user = this.user.asUser()
|
||||
|
||||
if (feedName == null || feedColor == null || feedUrl == null) {
|
||||
this.respond {
|
||||
this.embeds.add(
|
||||
MessageUtil.getEmbed(
|
||||
EmbedColor.ERROR,
|
||||
"Missing Arguments",
|
||||
"You are missing one or more arguments",
|
||||
user.username
|
||||
)
|
||||
)
|
||||
}
|
||||
return@action
|
||||
}
|
||||
val parser = RssParser()
|
||||
var title = "Not found"
|
||||
var items = listOf<RssItem>()
|
||||
runCatching { // this sucks
|
||||
val rss = parser.getRssChannel(feedUrl)
|
||||
title = rss.title!!
|
||||
items = rss.items
|
||||
}.onFailure {
|
||||
this.respond {
|
||||
this.embeds.add(
|
||||
MessageUtil.getEmbed(
|
||||
EmbedColor.ERROR,
|
||||
"Invalid Feed",
|
||||
"The feed you provided is invalid",
|
||||
user.username
|
||||
)
|
||||
)
|
||||
}
|
||||
return@action
|
||||
}
|
||||
val id = SubscriptionRepository.write(
|
||||
SubscriptionData(
|
||||
0,
|
||||
this.guild!!.id.value.toLong(),
|
||||
this.channel.id.value.toLong(),
|
||||
feedColor,
|
||||
feedName,
|
||||
feedUrl
|
||||
)
|
||||
)
|
||||
Logger.out("added feed as id ${id.resultedValues!!.first()[SubscriptionsTable.id]}")
|
||||
items.forEach {
|
||||
RSSRepository.write(RSSData(it.guid!!, id.resultedValues!!.first()[SubscriptionsTable.id]))
|
||||
}
|
||||
this.respond {
|
||||
this.embeds.add(
|
||||
MessageUtil.getEmbed(
|
||||
EmbedColor.INFO,
|
||||
"Added Feed \"${feedName}\"",
|
||||
"The feed \"${title}\" has been added to this channel",
|
||||
user.username
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inner class FeedArguments : Arguments() {
|
||||
val feedName by string {
|
||||
this.name = "name"
|
||||
this.description = "The name of the feed"
|
||||
}
|
||||
|
||||
val feedColor by enumChoice<FeedColor> {
|
||||
this.name = "color"
|
||||
this.description = "The color of the feed"
|
||||
this.typeName = "en_US"
|
||||
}
|
||||
|
||||
val feedUrl by string {
|
||||
this.name = "url"
|
||||
this.description = "The feed url"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.extensions
|
||||
|
||||
import com.kotlindiscord.kord.extensions.commands.Arguments
|
||||
import com.kotlindiscord.kord.extensions.commands.converters.impl.string
|
||||
import com.kotlindiscord.kord.extensions.extensions.Extension
|
||||
import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand
|
||||
import com.kotlindiscord.kord.extensions.types.respond
|
||||
import net.moonleay.rssbot.data.database.repository.RSSRepository
|
||||
import net.moonleay.rssbot.data.database.repository.SubscriptionRepository
|
||||
import net.moonleay.rssbot.util.EmbedColor
|
||||
import net.moonleay.rssbot.util.MessageUtil
|
||||
|
||||
class StuffExtension : Extension() {
|
||||
|
||||
override val name = "stuff"
|
||||
override val allowApplicationCommandInDMs: Boolean
|
||||
get() = false
|
||||
|
||||
|
||||
override suspend fun setup() {
|
||||
ephemeralSlashCommand(::StuffArguments) {
|
||||
name = "stuff"
|
||||
description = "Mark a feed for this channel as stuffed"
|
||||
this.action {
|
||||
val feedName = arguments.feedName
|
||||
val user = this.user.asUser()
|
||||
if (feedName == null) {
|
||||
this.respond {
|
||||
this.embeds.add(
|
||||
MessageUtil.getEmbed(
|
||||
EmbedColor.ERROR,
|
||||
"Missing Arguments",
|
||||
"You are missing one or more arguments",
|
||||
user.username
|
||||
)
|
||||
)
|
||||
}
|
||||
return@action
|
||||
}
|
||||
if (!SubscriptionRepository.exists(this.channel.id.value.toLong(), feedName)) {
|
||||
this.respond {
|
||||
this.embeds.add(
|
||||
MessageUtil.getEmbed(
|
||||
EmbedColor.ERROR,
|
||||
"Feed not found",
|
||||
"The feed \"${feedName}\" was not found",
|
||||
user.username
|
||||
)
|
||||
)
|
||||
}
|
||||
return@action
|
||||
}
|
||||
val subscription = SubscriptionRepository.get(this.channel.id.value.toLong(), feedName)
|
||||
RSSRepository.delete(subscription!!.id)
|
||||
SubscriptionRepository.delete(this.channel.id.value.toLong(), feedName)
|
||||
|
||||
this.respond {
|
||||
this.embeds.add(
|
||||
MessageUtil.getEmbed(
|
||||
EmbedColor.INFO,
|
||||
"Removed Feed \"${feedName}\"",
|
||||
"The feed \"${feedName}\" has been removed from this channel",
|
||||
user.username
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inner class StuffArguments : Arguments() {
|
||||
val feedName by string {
|
||||
this.name = "name"
|
||||
this.description = "The name of the feed"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.extensions.components
|
||||
|
||||
import com.kotlindiscord.kord.extensions.commands.application.slash.converters.ChoiceEnum
|
||||
import net.moonleay.rssbot.util.EmbedColor
|
||||
|
||||
enum class FeedColor(override val readableName: String, val color: EmbedColor) : ChoiceEnum {
|
||||
RED("Red", EmbedColor.RED),
|
||||
ORANGE("Orange", EmbedColor.ORANGE),
|
||||
YELLOW("Yellow", EmbedColor.YELLOW),
|
||||
GREEN("Green", EmbedColor.GREEN),
|
||||
CYAN("Cyan", EmbedColor.CYAN),
|
||||
LIGHTBLUE("Light Blue", EmbedColor.LIGHTBLUE),
|
||||
BLUE("Blue", EmbedColor.BLUE),
|
||||
PURPLE("Purple", EmbedColor.PURPLE),
|
||||
PINK("Pink", EmbedColor.PINK),
|
||||
}
|
||||
|
96
src/main/kotlin/net/moonleay/rssbot/jobs/FeedUpdater.kt
Normal file
96
src/main/kotlin/net/moonleay/rssbot/jobs/FeedUpdater.kt
Normal file
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.jobs
|
||||
|
||||
import com.prof18.rssparser.RssParser
|
||||
import com.prof18.rssparser.model.RssChannel
|
||||
import dev.inmo.krontab.KronScheduler
|
||||
import dev.kord.common.entity.Snowflake
|
||||
import dev.kord.core.behavior.channel.asChannelOf
|
||||
import dev.kord.core.behavior.channel.createMessage
|
||||
import dev.kord.core.entity.channel.MessageChannel
|
||||
import kotlinx.coroutines.Job
|
||||
import net.moonleay.rssbot.Bot
|
||||
import net.moonleay.rssbot.data.database.entry.RSSData
|
||||
import net.moonleay.rssbot.data.database.repository.RSSRepository
|
||||
import net.moonleay.rssbot.data.database.repository.SubscriptionRepository
|
||||
import net.moonleay.rssbot.jobs.component.CronjobType
|
||||
import net.moonleay.rssbot.jobs.component.ICronjob
|
||||
import net.moonleay.rssbot.util.EmbedUtil
|
||||
import net.moonleay.rssbot.util.Logger
|
||||
import net.moonleay.rssbot.util.MessageUtil
|
||||
|
||||
object FeedUpdater : ICronjob {
|
||||
override val jobName: String
|
||||
get() = "StatusUpdater"
|
||||
override val jobIncoming: String
|
||||
get() = "0 /20 * * * * 0o *" //Every 20 seconds
|
||||
override val jobType: CronjobType
|
||||
get() = CronjobType.INFINITE
|
||||
override val continueJob: Boolean
|
||||
get() = true
|
||||
override lateinit var cronjobJob: Job
|
||||
override lateinit var scheduler: KronScheduler
|
||||
|
||||
override suspend fun jobFunction() {
|
||||
Logger.out("Updating feeds...")
|
||||
update(true)
|
||||
}
|
||||
|
||||
suspend fun update(automated: Boolean = false) {
|
||||
Logger.out("Updating feeds [${if (automated) "AUTOMATED" else "MANUALLY"}]...")
|
||||
for (data in SubscriptionRepository.getAll()) {
|
||||
if (Bot.bot.kordRef.getChannel(Snowflake(data.channelId)) == null) {
|
||||
Logger.out("Channel ${data.channelId} is not available anymore ; deleting...")
|
||||
SubscriptionRepository.delete(data.channelId, data.feedUrl)
|
||||
}
|
||||
val parser = RssParser()
|
||||
var rss: RssChannel? = null
|
||||
runCatching { // this sucks
|
||||
rss = parser.getRssChannel(data.feedUrl)
|
||||
Logger.out("Updating feed ${data.feedUrl} for ${data.channelId} ...")
|
||||
}.onFailure {
|
||||
Logger.out("This feed is not valid: ${it.message}; deleting...")
|
||||
SubscriptionRepository.delete(data.channelId, data.feedUrl)
|
||||
}
|
||||
if (rss == null)
|
||||
continue
|
||||
for (rssData in rss!!.items) {
|
||||
if (rssData.guid == null || RSSRepository.exists(RSSData(rssData.guid!!, data.id)))
|
||||
continue
|
||||
val channel = Bot.bot.kordRef.getChannel(Snowflake(data.channelId))!!.asChannelOf<MessageChannel>()
|
||||
channel.createMessage {
|
||||
this.embeds.add(
|
||||
MessageUtil.getRSSEmbed(
|
||||
data.feedColor,
|
||||
rssData.author ?: "Anonymous",
|
||||
rssData.title ?: "Untitled",
|
||||
rssData.description ?: "No description",
|
||||
rssData.link ?: "https://moonleay.net/",
|
||||
rssData.image ?: "",
|
||||
if (rssData.link != null) EmbedUtil.getSiteLogo(rssData.link!!) else "https://static.moonleay.net/img/no-image.png",
|
||||
data.feedName,
|
||||
)
|
||||
)
|
||||
}
|
||||
RSSRepository.write(RSSData(rssData.guid!!, data.id))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.jobs.component
|
||||
|
||||
enum class CronjobType {
|
||||
INFINITE,
|
||||
ONCE,
|
||||
WHILE
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.jobs.component
|
||||
|
||||
import dev.inmo.krontab.KronScheduler
|
||||
import kotlinx.coroutines.Job
|
||||
|
||||
interface ICronjob {
|
||||
val jobName: String
|
||||
|
||||
/* /--------------- Seconds
|
||||
| /------------- Minutes
|
||||
| | /----------- Hours
|
||||
| | | /--------- Days of months
|
||||
| | | | /------- Months
|
||||
| | | | | /----- (optional) Year
|
||||
| | | | | | /--- (optional) Timezone offset
|
||||
| | | | | | | / (optional) Week days
|
||||
* * * * * * 0o *w
|
||||
*/
|
||||
|
||||
val jobIncoming: String
|
||||
val jobType: CronjobType
|
||||
|
||||
val continueJob: Boolean
|
||||
|
||||
var cronjobJob: Job
|
||||
var scheduler: KronScheduler
|
||||
suspend fun jobFunction()
|
||||
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.jobs.component
|
||||
|
||||
import dev.inmo.krontab.buildSchedule
|
||||
import dev.inmo.krontab.doInfinityTz
|
||||
import dev.inmo.krontab.doOnceTz
|
||||
import dev.inmo.krontab.doWhileTz
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import net.moonleay.rssbot.util.Logger
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
object JobManager {
|
||||
private val jobs: MutableList<ICronjob> = mutableListOf()
|
||||
|
||||
// Add a cronjob and register it
|
||||
fun addJob(job: ICronjob) {
|
||||
if (jobs.contains(job)) {
|
||||
killJob(job)
|
||||
}
|
||||
registerJob(job)
|
||||
jobs.add(job)
|
||||
Logger.out("Registered job \"${job.jobName}\" of type \"${job.javaClass.name}\".")
|
||||
}
|
||||
|
||||
// Register a cronjob
|
||||
private fun registerJob(job: ICronjob) {
|
||||
Logger.out(
|
||||
"INFO: Registering job \"${job.jobName}\"of type ${job.javaClass.name} at ${
|
||||
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
|
||||
}"
|
||||
)
|
||||
job.cronjobJob = CoroutineScope(Dispatchers.Default).launch {
|
||||
job.scheduler = buildSchedule(job.jobIncoming)
|
||||
Logger.out("Registered job \"${job.jobName}\"of type ${job.javaClass.name} to run at timer \"${job.jobIncoming}\".")
|
||||
when (job.jobType) {
|
||||
// Runs the job once at the specified time
|
||||
CronjobType.ONCE -> {
|
||||
job.scheduler.doOnceTz {
|
||||
job.jobFunction()
|
||||
}
|
||||
}
|
||||
// Runs the job whiles the variable is set to true
|
||||
CronjobType.WHILE -> {
|
||||
job.scheduler.doWhileTz {
|
||||
job.jobFunction()
|
||||
job.continueJob
|
||||
}
|
||||
}
|
||||
// Run this job until the programm stops
|
||||
CronjobType.INFINITE -> {
|
||||
job.scheduler.doInfinityTz {
|
||||
job.jobFunction()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Kill all cronjobs
|
||||
fun killAllJobs() {
|
||||
for (j in jobs) {
|
||||
killJob(j)
|
||||
}
|
||||
}
|
||||
|
||||
// Kill a cronjob
|
||||
fun killJob(j: ICronjob) {
|
||||
if (!jobs.contains(j)) {
|
||||
Logger.out("This job does not exist.")
|
||||
return
|
||||
}
|
||||
Logger.out("Killing job \"${j.jobName}\" of type ${j.javaClass.name}.")
|
||||
j.cronjobJob.cancel()
|
||||
jobs.remove(j)
|
||||
}
|
||||
}
|
39
src/main/kotlin/net/moonleay/rssbot/util/EmbedColor.kt
Normal file
39
src/main/kotlin/net/moonleay/rssbot/util/EmbedColor.kt
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.util
|
||||
|
||||
import dev.kord.common.Color
|
||||
|
||||
enum class EmbedColor(val color: Color) {
|
||||
ERROR(Color(0xE0311A)),
|
||||
WARNING(Color(0xFFA500)),
|
||||
SUCCESS(Color(0x52E01A)),
|
||||
INFO(Color(0x4C4645)),
|
||||
|
||||
RED(Color(0xe0311a)),
|
||||
ORANGE(Color(0xe0731a)),
|
||||
YELLOW(Color(0xe0c31a)),
|
||||
GREEN(Color(0x2ee01a)),
|
||||
CYAN(Color(0x1ae0b3)),
|
||||
LIGHTBLUE(Color(0x1ab2e0)),
|
||||
BLUE(Color(0x1a35e0)),
|
||||
PURPLE(Color(0x701ae0)),
|
||||
PINK(Color(0xb51ae0)),
|
||||
|
||||
}
|
144
src/main/kotlin/net/moonleay/rssbot/util/EmbedUtil.kt
Normal file
144
src/main/kotlin/net/moonleay/rssbot/util/EmbedUtil.kt
Normal file
|
@ -0,0 +1,144 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.util
|
||||
|
||||
import dev.kord.common.entity.ButtonStyle
|
||||
import dev.kord.core.entity.Embed
|
||||
import dev.kord.rest.builder.component.ActionRowBuilder
|
||||
import dev.kord.rest.builder.message.EmbedBuilder
|
||||
import io.ktor.util.*
|
||||
import net.moonleay.rssbot.extensions.components.FeedColor
|
||||
|
||||
object EmbedUtil {
|
||||
fun getTimePlannerButtons(): ActionRowBuilder {
|
||||
val ar = ActionRowBuilder()
|
||||
ar.interactionButton(ButtonStyle.Success, "public.edit.btn.timemanagement.available") {
|
||||
this.label = "Available"
|
||||
}
|
||||
ar.interactionButton(ButtonStyle.Primary, "public.edit.btn.timemanagement.maybeavailable") {
|
||||
this.label = "May be available"
|
||||
}
|
||||
ar.interactionButton(ButtonStyle.Danger, "public.edit.btn.timemanagement.notavailable") {
|
||||
this.label = "Not available"
|
||||
}
|
||||
return ar
|
||||
}
|
||||
|
||||
fun getMatchButtons(): ActionRowBuilder {
|
||||
val ar = ActionRowBuilder()
|
||||
ar.interactionButton(ButtonStyle.Success, "public.edit.btn.matchmanagement.accept") {
|
||||
this.label = "I'm in!"
|
||||
}
|
||||
ar.interactionButton(ButtonStyle.Danger, "public.edit.btn.matchmanagement.decline") {
|
||||
this.label = "I'm out!"
|
||||
}
|
||||
return ar
|
||||
}
|
||||
|
||||
fun replaceXWithYinValuesAtTable(x: String, y: String, e: Embed, table: Int): EmbedBuilder {
|
||||
return replaceXWithYinValuesAtTable(x, y, MessageUtil.getAClonedEmbed(e), table)
|
||||
}
|
||||
|
||||
fun replaceXWithYinValuesAtTable(x: String, y: String, ebb: EmbedBuilder, table: Int): EmbedBuilder {
|
||||
val ebbb = MessageUtil.getAClonedEmbed(ebb)
|
||||
ebbb.fields = mutableListOf()
|
||||
for ((i, f) in ebb.fields.withIndex()) {
|
||||
val fb = EmbedBuilder.Field()
|
||||
fb.name = f.name
|
||||
if (i == table - 1) {
|
||||
val v = f.value.split("\n").toMutableList()
|
||||
for ((j, l) in v.withIndex())
|
||||
if (l.contains(x))
|
||||
v[j] = y
|
||||
v.removeIf {
|
||||
!it.contains("@")
|
||||
}
|
||||
fb.value = v.joinToString("\n")
|
||||
} else
|
||||
fb.value = f.value
|
||||
fb.inline = true
|
||||
ebbb.fields.add(fb)
|
||||
}
|
||||
return ebbb
|
||||
}
|
||||
|
||||
fun getAllUsersInTheFirstXTables(amountOfTables: Int, e: Embed): List<String> {
|
||||
val users = mutableListOf<String>()
|
||||
for (i in 0 until amountOfTables - 1) {
|
||||
val f = e.fields[i]
|
||||
if (!f.value.contains("@"))
|
||||
continue // check next one. this one does not have any entries
|
||||
val v = f.value.split("\n").toMutableList()
|
||||
for (l in v) {
|
||||
Logger.out(l)
|
||||
users.add(l.subSequence(2, l.indexOf(">")).toString())
|
||||
}
|
||||
}
|
||||
return users
|
||||
}
|
||||
|
||||
fun addXToValuesAtTable(x: String, e: Embed, table: Int): EmbedBuilder {
|
||||
return addXToValuesAtTable(x, MessageUtil.getAClonedEmbed(e), table)
|
||||
}
|
||||
|
||||
fun addXToValuesAtTable(x: String, ebb: EmbedBuilder, table: Int): EmbedBuilder {
|
||||
val ebbb = MessageUtil.getAClonedEmbed(ebb)
|
||||
ebbb.fields = mutableListOf()
|
||||
ebb.fields.forEachIndexed { i, f ->
|
||||
val fb = EmbedBuilder.Field()
|
||||
fb.name = f.name
|
||||
if (i == table - 1)
|
||||
fb.value = f.value + "\n<@$x>"
|
||||
else {
|
||||
val v = f.value.split("\n").toMutableList()
|
||||
for ((j, l) in v.withIndex())
|
||||
if (l.contains(x))
|
||||
v[j] = ""
|
||||
v.removeIf {
|
||||
!it.contains("@")
|
||||
}
|
||||
fb.value = v.joinToString("\n")
|
||||
}
|
||||
|
||||
fb.inline = true
|
||||
ebbb.fields.add(fb)
|
||||
}
|
||||
return ebbb
|
||||
}
|
||||
|
||||
fun getColorFromString(str: String): FeedColor {
|
||||
return when (str.toLowerCasePreservingASCIIRules()) {
|
||||
"red" -> FeedColor.RED
|
||||
"blue" -> FeedColor.BLUE
|
||||
"green" -> FeedColor.GREEN
|
||||
"cyan" -> FeedColor.CYAN
|
||||
"light blue" -> FeedColor.LIGHTBLUE
|
||||
"yellow" -> FeedColor.YELLOW
|
||||
"purple" -> FeedColor.PURPLE
|
||||
"pink" -> FeedColor.PINK
|
||||
"orange" -> FeedColor.ORANGE
|
||||
else -> FeedColor.RED
|
||||
}
|
||||
}
|
||||
|
||||
fun getSiteLogo(str: String): String {
|
||||
return "https://www.google.com/s2/favicons?domain=${str.split("/")[2]}"
|
||||
}
|
||||
|
||||
}
|
42
src/main/kotlin/net/moonleay/rssbot/util/Logger.kt
Normal file
42
src/main/kotlin/net/moonleay/rssbot/util/Logger.kt
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.util
|
||||
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
object Logger {
|
||||
private val dtf: DateTimeFormatter = DateTimeFormatter.ofPattern("yy/MM/dd HH:mm:ss")
|
||||
|
||||
fun out(msg: String) {
|
||||
val caller = Thread.currentThread().stackTrace[2]
|
||||
val now: LocalDateTime = LocalDateTime.now()
|
||||
try {
|
||||
println(
|
||||
("[" + Class.forName(caller.className).simpleName + "." +
|
||||
caller.methodName + ":" + caller.lineNumber + "] [" + dtf.format(now)) + "] <" + msg + ">"
|
||||
)
|
||||
} catch (e: ClassNotFoundException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
// Ich kann nicht mehr
|
||||
// [Klasse.Funktion] [T/M HH:MM] <NACHRICHT>
|
||||
}
|
||||
}
|
||||
|
210
src/main/kotlin/net/moonleay/rssbot/util/MessageUtil.kt
Normal file
210
src/main/kotlin/net/moonleay/rssbot/util/MessageUtil.kt
Normal file
|
@ -0,0 +1,210 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.util
|
||||
|
||||
import com.kotlindiscord.kord.extensions.commands.Arguments
|
||||
import com.kotlindiscord.kord.extensions.commands.application.slash.PublicSlashCommandContext
|
||||
import com.kotlindiscord.kord.extensions.components.forms.ModalForm
|
||||
import com.kotlindiscord.kord.extensions.types.respond
|
||||
import dev.kord.core.entity.Embed
|
||||
import dev.kord.rest.builder.message.EmbedBuilder
|
||||
import net.moonleay.rssbot.extensions.components.FeedColor
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
|
||||
object MessageUtil {
|
||||
private val dtf: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy @ HH:mm:ss")
|
||||
|
||||
///Send an embedded message as a reply
|
||||
suspend fun sendEmbedForPublicSlashCommand(
|
||||
ctx: PublicSlashCommandContext<Arguments, ModalForm>,
|
||||
color: EmbedColor,
|
||||
title: String,
|
||||
description: String,
|
||||
) {
|
||||
ctx.respond {
|
||||
embeds.add(
|
||||
getEmbed(
|
||||
color,
|
||||
title,
|
||||
description,
|
||||
ctx.user.asUser().username + "#" + ctx.user.asUser().discriminator
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
///Send an embedded message with an image as a reply
|
||||
suspend fun sendEmbedForPublicSlashCommandWithImage(
|
||||
ctx: PublicSlashCommandContext<Arguments, ModalForm>,
|
||||
color: EmbedColor,
|
||||
title: String,
|
||||
description: String,
|
||||
thumbnailUrl: String,
|
||||
) {
|
||||
ctx.respond {
|
||||
embeds.add(
|
||||
getEmbedWithImage(
|
||||
color,
|
||||
title,
|
||||
description,
|
||||
ctx.user.asUser().username + "#" + ctx.user.asUser().discriminator,
|
||||
thumbnailUrl
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
///Get a cloned embedded message
|
||||
fun getAClonedEmbed(e: Embed): EmbedBuilder {
|
||||
val ebb = EmbedBuilder()
|
||||
ebb.color = e.color
|
||||
ebb.title = e.title
|
||||
e.fields.forEach {
|
||||
val fb = EmbedBuilder.Field()
|
||||
fb.name = it.name
|
||||
fb.value = it.value
|
||||
fb.inline = it.inline
|
||||
ebb.fields.add(fb)
|
||||
}
|
||||
ebb.description = e.description
|
||||
return ebb
|
||||
}
|
||||
|
||||
fun getAClonedEmbed(e: EmbedBuilder): EmbedBuilder {
|
||||
val ebb = EmbedBuilder()
|
||||
ebb.color = e.color
|
||||
ebb.title = e.title
|
||||
e.fields.forEach {
|
||||
val fb = EmbedBuilder.Field()
|
||||
fb.name = it.name
|
||||
fb.value = it.value
|
||||
fb.inline = it.inline
|
||||
ebb.fields.add(fb)
|
||||
}
|
||||
ebb.description = e.description
|
||||
return ebb
|
||||
}
|
||||
|
||||
fun getEmbedWithTableWithFooter(
|
||||
color: EmbedColor,
|
||||
title: String,
|
||||
description: String,
|
||||
values: Map<String, List<String>>?,
|
||||
footer: String,
|
||||
): EmbedBuilder {
|
||||
val ebb = getEmbedWithTable(color, title, description, values)
|
||||
ebb.footer = EmbedBuilder.Footer()
|
||||
ebb.footer!!.text = ">m.id/$footer"
|
||||
return ebb
|
||||
}
|
||||
|
||||
///Get an embedded msg with image, title and description
|
||||
fun getEmbedWithTable(
|
||||
color: EmbedColor,
|
||||
title: String,
|
||||
description: String,
|
||||
values: Map<String, List<String>>?,
|
||||
): EmbedBuilder {
|
||||
val ebb = getEmbedSmall(color, title, description)
|
||||
if (values != null)
|
||||
for (key in values.keys) {
|
||||
val fb = EmbedBuilder.Field()
|
||||
fb.name = key
|
||||
var s = ""
|
||||
for (value in values[key]!!)
|
||||
s += "$value\n"
|
||||
fb.value = s
|
||||
fb.inline = true
|
||||
ebb.fields.add(fb)
|
||||
}
|
||||
return ebb
|
||||
}
|
||||
|
||||
|
||||
///Get an embedded msg with title and description
|
||||
fun getEmbedSmall(
|
||||
color: EmbedColor,
|
||||
title: String,
|
||||
description: String,
|
||||
): EmbedBuilder {
|
||||
val ebb = EmbedBuilder()
|
||||
ebb.title = title
|
||||
ebb.description = description
|
||||
ebb.color = color.color
|
||||
return ebb
|
||||
}
|
||||
|
||||
///Get an embedded msg with title, description and a src
|
||||
fun getEmbed(
|
||||
color: EmbedColor,
|
||||
title: String,
|
||||
description: String,
|
||||
source: String,
|
||||
): EmbedBuilder {
|
||||
val ebb = getEmbedSmall(color, title, description)
|
||||
val now: LocalDateTime = LocalDateTime.now()
|
||||
ebb.footer = EmbedBuilder.Footer()
|
||||
ebb.footer!!.text = ">" + dtf.format(now) + " - $source"
|
||||
return ebb
|
||||
}
|
||||
|
||||
fun getRSSEmbed(
|
||||
color: FeedColor,
|
||||
author: String,
|
||||
title: String,
|
||||
description: String,
|
||||
url: String,
|
||||
imageUrl: String,
|
||||
logo: String,
|
||||
feedName: String,
|
||||
): EmbedBuilder {
|
||||
val ebb = EmbedBuilder()
|
||||
val now: LocalDateTime = LocalDateTime.now()
|
||||
ebb.title = title
|
||||
ebb.author {
|
||||
this.name = author
|
||||
this.icon = logo
|
||||
}
|
||||
ebb.description = "${if (description != "No description") description else ""}\n[[open full article]]($url)"
|
||||
ebb.color = color.color.color
|
||||
ebb.footer {
|
||||
this.text = "> rssbot, ($feedName) / sent at ${dtf.format(now)}"
|
||||
this.icon = "https://static.moonleay.net/img/rss.png"
|
||||
}
|
||||
ebb.thumbnail {
|
||||
this.url = imageUrl
|
||||
}
|
||||
return ebb
|
||||
}
|
||||
|
||||
///Get an embedded msg with image, title, description and a src
|
||||
fun getEmbedWithImage(
|
||||
color: EmbedColor,
|
||||
title: String,
|
||||
description: String,
|
||||
source: String,
|
||||
thumbnailUrl: String,
|
||||
): EmbedBuilder {
|
||||
val ebb = getEmbed(color, title, description, source)
|
||||
ebb.thumbnail = EmbedBuilder.Thumbnail()
|
||||
ebb.thumbnail!!.url = thumbnailUrl
|
||||
return ebb
|
||||
}
|
||||
}
|
64
src/main/kotlin/net/moonleay/rssbot/util/TimeUtil.kt
Normal file
64
src/main/kotlin/net/moonleay/rssbot/util/TimeUtil.kt
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* RSSBot
|
||||
* Copyright (C) 2023 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.rssbot.util
|
||||
|
||||
import kotlinx.datetime.DayOfWeek
|
||||
import java.time.ZonedDateTime
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
|
||||
object TimeUtil {
|
||||
|
||||
fun getTimeFormatedShortend(time2: Long, showS: Boolean): String {
|
||||
var time = time2
|
||||
val days: Long = TimeUnit.MILLISECONDS
|
||||
.toDays(time)
|
||||
time -= TimeUnit.DAYS.toMillis(days)
|
||||
val hours: Long = TimeUnit.MILLISECONDS
|
||||
.toHours(time)
|
||||
time -= TimeUnit.HOURS.toMillis(hours)
|
||||
val minutes: Long = TimeUnit.MILLISECONDS
|
||||
.toMinutes(time)
|
||||
time -= TimeUnit.MINUTES.toMillis(minutes)
|
||||
val seconds: Long = TimeUnit.MILLISECONDS
|
||||
.toSeconds(time)
|
||||
var s = ""
|
||||
if (days >= 1) {
|
||||
s += days.toString() + "d "
|
||||
}
|
||||
if (hours >= 1) {
|
||||
s += hours.toString() + "h "
|
||||
}
|
||||
if (minutes >= 1) {
|
||||
s += minutes.toString() + "m "
|
||||
}
|
||||
if (seconds >= 1 && hours < 1 && showS) {
|
||||
s += seconds.toString() + "s"
|
||||
}
|
||||
if (s.isEmpty() || s.isBlank()) {
|
||||
s = "None"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Returns the day of the month of the monday of this week
|
||||
fun getMondayDayOfMonth(): Int {
|
||||
return ZonedDateTime.now().with(DayOfWeek.MONDAY).dayOfMonth
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package net.moonleay.rssbot.build
|
||||
|
||||
internal object BuildConstants {
|
||||
const val version = "${version}"
|
||||
const val ownerID = "${ownerID}"
|
||||
const val kordVersion = "${kordversion}"
|
||||
const val coroutinesVersion = "${coroutinesversion}"
|
||||
const val ktorVersion = "${ktorversion}"
|
||||
const val exposedVersion = "${exposedversion}"
|
||||
const val postgresVersion = "${postgresversion}"
|
||||
const val krontabVersion = "${krontabversion}"
|
||||
const val rssparserVersion = "${rssparserver}"
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue