feat: added SendFeedExtension

This commit is contained in:
moonleay 2023-10-15 15:18:02 +02:00
parent 39802598b2
commit f217e4ff07

View file

@ -0,0 +1,113 @@
/*
* 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 com.overzealous.remark.Options
import com.overzealous.remark.Remark
import com.prof18.rssparser.RssParser
import com.prof18.rssparser.model.RssChannel
import com.prof18.rssparser.model.RssItem
import dev.kord.rest.builder.message.EmbedBuilder.Limits.title
import net.moonleay.rssbot.data.database.repository.RSSRepository
import net.moonleay.rssbot.data.database.repository.SubscriptionRepository
import net.moonleay.rssbot.extensions.components.FeedColor
import net.moonleay.rssbot.util.EmbedColor
import net.moonleay.rssbot.util.EmbedUtil
import net.moonleay.rssbot.util.MessageUtil
class SendFeedExtension : Extension() {
override val name = "sendfeed"
override val allowApplicationCommandInDMs: Boolean
get() = false
override suspend fun setup() {
ephemeralSlashCommand(::StuffArguments) {
name = "sendfeed"
description = "Send a feed to this channel"
this.action {
val feedUrl = arguments.feedUrl
val user = this.user.asUser()
if (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 rss: RssChannel? = null
runCatching { // this sucks
rss = parser.getRssChannel(feedUrl)
}.onFailure {
this.respond {
this.embeds.add(
MessageUtil.getEmbed(
EmbedColor.ERROR,
"Invalid Feed",
"The feed you provided is invalid",
user.username
)
)
}
return@action
}
if (rss == null)
return@action
rss!!.items.forEach {
this.respond {
this.embeds.add(
MessageUtil.getRSSEmbed(
FeedColor.GREEN,
it.author ?: "Anonymous",
it.title ?: "Untitled",
if (it.description == null) "No description" else Remark(EmbedUtil.getHTMLtoMarkdownOptions()).convertFragment(it.description!!),
it.link ?: "https://moonleay.net/",
it.image ?: "",
if (it.link != null) EmbedUtil.getSiteLogo(it.link!!) else "https://static.moonleay.net/img/no-image.png",
"Manual Feed",
it.pubDate?: "unknown"
)
)
}
}
}
}
}
inner class StuffArguments : Arguments() {
val feedUrl by string {
this.name = "url"
this.description = "The url of the feed to send"
}
}
}