Compare commits

..

8 commits

8 changed files with 93 additions and 4 deletions

View file

@ -14,7 +14,7 @@ val ownerID = 372703841151614976L
group = "net.moonleay.rssbot" group = "net.moonleay.rssbot"
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" }
?: "0.1.0" ?: "0.2.1"
val kordver = "1.5.10-SNAPSHOT" val kordver = "1.5.10-SNAPSHOT"
val coroutinesver = "1.7.3" val coroutinesver = "1.7.3"

View file

@ -25,6 +25,7 @@ import dev.kord.core.on
import net.moonleay.rssbot.data.CredentialManager import net.moonleay.rssbot.data.CredentialManager
import net.moonleay.rssbot.data.database.DB import net.moonleay.rssbot.data.database.DB
import net.moonleay.rssbot.extensions.FeedExtension import net.moonleay.rssbot.extensions.FeedExtension
import net.moonleay.rssbot.extensions.FeedsExtension
import net.moonleay.rssbot.extensions.StuffExtension import net.moonleay.rssbot.extensions.StuffExtension
import net.moonleay.rssbot.jobs.FeedUpdater import net.moonleay.rssbot.jobs.FeedUpdater
import net.moonleay.rssbot.jobs.component.JobManager import net.moonleay.rssbot.jobs.component.JobManager
@ -75,6 +76,7 @@ object Bot {
extensions { extensions {
add(::FeedExtension) add(::FeedExtension)
add(::StuffExtension) add(::StuffExtension)
add(::FeedsExtension)
} }
this.presence { this.presence {

View file

@ -46,6 +46,27 @@ object SubscriptionRepository {
return dataList return dataList
} }
fun getAllChannel(channelId: Long): List<SubscriptionData> {
val dataList = mutableListOf<SubscriptionData>()
transaction {
SubscriptionsTable.select {
SubscriptionsTable.channelId eq channelId
}.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? { fun get(channelId: Long, feedName: String): SubscriptionData? {
var data: SubscriptionData? = null var data: SubscriptionData? = null
@ -66,6 +87,16 @@ object SubscriptionRepository {
return data return data
} }
fun existsWithUrl(channelId: Long, feedUrl: String): Boolean {
var exists = false
transaction {
exists = SubscriptionsTable.select {
(SubscriptionsTable.channelId eq channelId) and (SubscriptionsTable.feedUrl eq feedUrl)
}.count() > 0
}
return exists
}
fun exists(channelId: Long, feedName: String): Boolean { fun exists(channelId: Long, feedName: String): Boolean {
var exists = false var exists = false
transaction { transaction {

View file

@ -22,7 +22,7 @@ import org.jetbrains.exposed.sql.Table
object RSSTable : Table(name = "rss") { object RSSTable : Table(name = "rss") {
val guid = text("guid").uniqueIndex() val guid = text("guid")
val subscriptionId = integer("subscription_id") references SubscriptionsTable.id val subscriptionId = integer("subscription_id") references SubscriptionsTable.id
override val primaryKey = PrimaryKey(guid, subscriptionId, name = "PK_RSS") override val primaryKey = PrimaryKey(guid, subscriptionId, name = "PK_RSS")
} }

View file

@ -86,6 +86,19 @@ class FeedExtension : Extension() {
} }
return@action return@action
} }
if (SubscriptionRepository.exists(this.channel.id.value.toLong(), feedName) || SubscriptionRepository.existsWithUrl(this.channel.id.value.toLong(), feedUrl)) {
this.respond {
this.embeds.add(
MessageUtil.getEmbed(
EmbedColor.ERROR,
"Feed already exists",
"The feed \"${feedName}\" already exists",
user.username
)
)
}
return@action
}
val id = SubscriptionRepository.write( val id = SubscriptionRepository.write(
SubscriptionData( SubscriptionData(
0, 0,

View file

@ -0,0 +1,40 @@
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 FeedsExtension : Extension() {
override val name = "feeds"
override val allowApplicationCommandInDMs: Boolean
get() = false
override suspend fun setup() {
ephemeralSlashCommand() {
name = "feeds"
description = "Show all active feeds for this channel"
this.action {
val user = this.user.asUser()
val subscriptions = SubscriptionRepository.getAllChannel(this.channel.id.value.toLong())
this.respond {
this.embeds.add(
MessageUtil.getEmbed(
EmbedColor.INFO,
"Feeds",
subscriptions.joinToString("\n") { it.feedName + " [[feed url](" + it.feedUrl + ")]" },
user.username
)
)
}
}
}
}
}

View file

@ -38,7 +38,7 @@ import net.moonleay.rssbot.util.MessageUtil
object FeedUpdater : ICronjob { object FeedUpdater : ICronjob {
override val jobName: String override val jobName: String
get() = "StatusUpdater" get() = "FeedUpdater"
override val jobIncoming: String override val jobIncoming: String
get() = "0 /20 * * * * 0o *" //Every 20 seconds get() = "0 /20 * * * * 0o *" //Every 20 seconds
override val jobType: CronjobType override val jobType: CronjobType

View file

@ -177,7 +177,10 @@ object MessageUtil {
): EmbedBuilder { ): EmbedBuilder {
val ebb = EmbedBuilder() val ebb = EmbedBuilder()
val now: LocalDateTime = LocalDateTime.now() val now: LocalDateTime = LocalDateTime.now()
ebb.title = title if (title.length > 250)
ebb.title = title.substring(0, 250) + "[...]"
else
ebb.title = title
ebb.author { ebb.author {
this.name = author this.name = author
this.icon = logo this.icon = logo