feat: support gen short link and create pastebin using reply

This commit is contained in:
2024-10-14 22:41:01 +08:00
parent bfbeee2880
commit e375f20e9c
3 files changed
+47 -6

No files matched your search

@@ -7,8 +7,10 @@
package cn.rtast.fancybot package cn.rtast.fancybot
import cn.rtast.fancybot.commands.misc.PastebinCommand
import cn.rtast.fancybot.commands.misc.ReactionCommand import cn.rtast.fancybot.commands.misc.ReactionCommand
import cn.rtast.fancybot.commands.misc.ScanQRCodeCommand import cn.rtast.fancybot.commands.misc.ScanQRCodeCommand
import cn.rtast.fancybot.commands.misc.ShortLinkCommand.Companion.makeShortLink
import cn.rtast.fancybot.commands.parse.* import cn.rtast.fancybot.commands.parse.*
import cn.rtast.fancybot.commands.reply.ImageBedCommand import cn.rtast.fancybot.commands.reply.ImageBedCommand
import cn.rtast.fancybot.enums.WSType import cn.rtast.fancybot.enums.WSType
@@ -17,6 +19,7 @@ import cn.rtast.fancybot.util.misc.convertToDate
import cn.rtast.fancybot.util.misc.initCommandAndItem import cn.rtast.fancybot.util.misc.initCommandAndItem
import cn.rtast.fancybot.util.misc.initFilesDir import cn.rtast.fancybot.util.misc.initFilesDir
import cn.rtast.fancybot.util.misc.initSetuIndex import cn.rtast.fancybot.util.misc.initSetuIndex
import cn.rtast.fancybot.util.misc.isValidUrl
import cn.rtast.rob.ROneBotFactory import cn.rtast.rob.ROneBotFactory
import cn.rtast.rob.entity.* import cn.rtast.rob.entity.*
import cn.rtast.rob.entity.lagrange.FileEvent import cn.rtast.rob.entity.lagrange.FileEvent
@@ -28,6 +31,7 @@ import cn.rtast.rob.util.ob.OneBotListener
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import sun.net.www.content.text.plain
import java.io.File import java.io.File
import java.net.URI import java.net.URI
@@ -77,6 +81,18 @@ class FancyBot : OneBotListener {
val command = message.message.reversed().find { it.type == ArrayMessageType.text }!!.data.text!! val command = message.message.reversed().find { it.type == ArrayMessageType.text }!!.data.text!!
val replyId = message.message.find { it.type == ArrayMessageType.reply }!!.data.id!! val replyId = message.message.find { it.type == ArrayMessageType.reply }!!.data.id!!
val getMsg = this.getMessage(replyId.toString().toLong()) val getMsg = this.getMessage(replyId.toString().toLong())
val plainTextContent = getMsg.message
.filter { it.type == ArrayMessageType.text }
.joinToString { it.data.text!! }
if (command.contains("/sl") || command.contains("/short")) {
// 使用回复消息的方式快速对一个url消息创建一个短链接
message.reply(plainTextContent.trim().makeShortLink())
}
if (command.contains("/pb") || command.contains("/pastebin")) {
// 使用回复消息的方式快速将创建一个pastebin
val shortUrl = PastebinCommand.createPastebin(plainTextContent).second
message.reply(shortUrl)
}
if (command.contains("/ascii") || command.contains("/asc")) { if (command.contains("/ascii") || command.contains("/asc")) {
// 使用回复消息的方式直接对一个图片进行生成Ascii art的操作 // 使用回复消息的方式直接对一个图片进行生成Ascii art的操作
val url = AsciiArtCommand.getImageUrl(getMsg) val url = AsciiArtCommand.getImageUrl(getMsg)
@@ -25,14 +25,25 @@ import cn.rtast.rob.util.ob.OneBotListener
class PastebinCommand : BaseCommand() { class PastebinCommand : BaseCommand() {
override val commandNames = listOf("/pastebin", "/pb") override val commandNames = listOf("/pastebin", "/pb")
companion object {
/**
* 创建一个pastebin返回一个pair 第一个是原始链接, 第二个是缩短url后的链接
*/
fun createPastebin(content: String): Pair<String, String> {
val pastebinPayload = PastebinPayload(content)
val response = Http.post<PastebinResponse>(
"$API_RTAST_URL/api/pastebin", pastebinPayload.toJson(),
params = mapOf("key" to configManager.apiRtastKey)
)
val url = "$API_RTAST_URL/api/pp/${response.id}?raw=true"
return url to url.makeShortLink()
}
}
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) { override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
val content = args.joinToString(" ") val content = args.joinToString(" ")
val pastebinPayload = PastebinPayload(content) val shortUrl = createPastebin(content).second
val response = Http.post<PastebinResponse>( message.reply(shortUrl)
"$API_RTAST_URL/api/pastebin", pastebinPayload.toJson(),
params = mapOf("key" to configManager.apiRtastKey)
)
message.reply("$API_RTAST_URL/api/pp/${response.id}?raw=true".makeShortLink())
insertActionRecord(CommandAction.Pastebin, message.sender.userId, content) insertActionRecord(CommandAction.Pastebin, message.sender.userId, content)
} }
} }
@@ -10,6 +10,16 @@ package cn.rtast.fancybot.util.misc
import java.net.URI import java.net.URI
import java.net.URL import java.net.URL
private val urlRegex = Regex(
"^(https?://)?" +
"([\\w\\-]+\\.)+[a-zA-Z]{2,6}" +
"|(https?://)?" +
"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}" +
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" +
"(:\\d{1,5})?" +
"(/.*)?$"
)
fun String.toURL(): URL { fun String.toURL(): URL {
return URI(this).toURL() return URI(this).toURL()
} }
@@ -17,3 +27,7 @@ fun String.toURL(): URL {
fun String.toURI(): URI { fun String.toURI(): URI {
return URI(this) return URI(this)
} }
fun String.isValidUrl(): Boolean {
return urlRegex.matches(this)
}