2024-08-27 18:44:32 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2024 RTAkland
|
|
|
|
|
* Author: RTAkland
|
|
|
|
|
* Date: 2024/8/26
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package cn.rtast.fancybot
|
|
|
|
|
|
2024-09-20 16:53:38 +08:00
|
|
|
import cn.rtast.fancybot.commands.AboutCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.EchoCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.HelpCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.StatusCommand
|
2024-09-13 12:44:10 +08:00
|
|
|
import cn.rtast.fancybot.commands.lookup.CigaretteCommand
|
2024-09-17 11:27:20 +08:00
|
|
|
import cn.rtast.fancybot.commands.lookup.DomainPriceCommand
|
2024-09-15 13:27:14 +08:00
|
|
|
import cn.rtast.fancybot.commands.lookup.GPTCommand
|
2024-09-15 22:11:55 +08:00
|
|
|
import cn.rtast.fancybot.commands.lookup.GithubUserCommand
|
2024-09-13 12:44:10 +08:00
|
|
|
import cn.rtast.fancybot.commands.lookup.MCPingCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.lookup.MusicCommand
|
2024-09-16 23:52:40 +08:00
|
|
|
import cn.rtast.fancybot.commands.lookup.MusicPlayUrlCommand
|
2024-09-13 12:44:10 +08:00
|
|
|
import cn.rtast.fancybot.commands.lookup.NslookupCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.lookup.PixivCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.lookup.QRCodeCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.lookup.WeatherCommand
|
2024-09-19 22:34:40 +08:00
|
|
|
import cn.rtast.fancybot.commands.lookup.WikipediaCommand
|
2024-09-20 16:30:33 +08:00
|
|
|
import cn.rtast.fancybot.commands.misc.AntiRevokeCommand
|
2024-09-20 16:53:38 +08:00
|
|
|
import cn.rtast.fancybot.commands.misc.CompilerCommand
|
2024-09-20 16:30:33 +08:00
|
|
|
import cn.rtast.fancybot.commands.misc.FKXQSCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.misc.HitokotoCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.misc.LikeMeCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.misc.RUACommand
|
|
|
|
|
import cn.rtast.fancybot.commands.misc.RemakeCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.misc.SendMailCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.misc.UnsetZiBiCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.misc.ZiBiCommand
|
2024-09-20 13:30:52 +08:00
|
|
|
import cn.rtast.fancybot.commands.parse.AsciiArtCommand
|
2024-09-13 12:44:10 +08:00
|
|
|
import cn.rtast.fancybot.commands.parse.BVParseCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.parse.GitHubParseCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.parse.ImageURLCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.parse.ReverseGIFCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.record.JiJianCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.record.JrrpCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.record.MyNiuziCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.record.MyPointCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.record.NiuziSignCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.record.RedeemCommand
|
|
|
|
|
import cn.rtast.fancybot.commands.record.SignCommand
|
2024-09-12 13:06:13 +08:00
|
|
|
import cn.rtast.fancybot.entity.bili.CardShare
|
2024-08-30 15:05:51 +08:00
|
|
|
import cn.rtast.fancybot.entity.enums.WSType
|
2024-09-03 14:37:00 +08:00
|
|
|
import cn.rtast.fancybot.items.BaisiItem
|
|
|
|
|
import cn.rtast.fancybot.items.HeisiItem
|
|
|
|
|
import cn.rtast.fancybot.items.SetuItem
|
2024-08-30 14:24:46 +08:00
|
|
|
import cn.rtast.fancybot.util.file.ConfigManager
|
2024-09-08 15:07:43 +08:00
|
|
|
import cn.rtast.fancybot.util.file.NiuziManager
|
2024-09-04 11:36:41 +08:00
|
|
|
import cn.rtast.fancybot.util.file.SignManager
|
2024-08-30 17:50:01 +08:00
|
|
|
import cn.rtast.fancybot.util.initDatabase
|
2024-09-03 16:15:26 +08:00
|
|
|
import cn.rtast.fancybot.util.item.ItemManager
|
2024-09-12 13:06:13 +08:00
|
|
|
import cn.rtast.fancybot.util.str.fromJson
|
2024-08-27 18:44:32 +08:00
|
|
|
import cn.rtast.rob.ROneBotFactory
|
2024-09-19 11:06:40 +08:00
|
|
|
import cn.rtast.rob.entity.AddFriendRequest
|
2024-09-19 15:11:40 +08:00
|
|
|
import cn.rtast.rob.entity.FileEvent
|
2024-09-02 11:30:58 +08:00
|
|
|
import cn.rtast.rob.entity.GetMessage
|
2024-08-27 18:44:32 +08:00
|
|
|
import cn.rtast.rob.entity.GroupMessage
|
2024-09-02 11:30:58 +08:00
|
|
|
import cn.rtast.rob.entity.GroupRevokeMessage
|
2024-09-06 10:58:55 +08:00
|
|
|
import cn.rtast.rob.enums.ArrayMessageType
|
2024-09-02 11:30:58 +08:00
|
|
|
import cn.rtast.rob.util.ob.MessageChain
|
2024-09-19 11:06:40 +08:00
|
|
|
import cn.rtast.rob.util.ob.OneBotListener
|
2024-09-20 22:23:43 +08:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.launch
|
2024-09-19 15:11:40 +08:00
|
|
|
import java.io.File
|
2024-09-20 22:23:43 +08:00
|
|
|
import java.net.URI
|
2024-09-20 16:53:38 +08:00
|
|
|
import java.time.Instant
|
2024-08-27 18:44:32 +08:00
|
|
|
|
2024-09-19 11:06:40 +08:00
|
|
|
class FancyBot : OneBotListener {
|
2024-09-02 11:30:58 +08:00
|
|
|
|
2024-09-20 22:23:43 +08:00
|
|
|
private val coroutineScope = CoroutineScope(Dispatchers.IO)
|
|
|
|
|
|
2024-09-07 18:07:56 +08:00
|
|
|
override suspend fun onGroupMessage(message: GroupMessage, json: String) {
|
2024-09-03 12:32:52 +08:00
|
|
|
val sender = message.sender.nickname
|
|
|
|
|
val senderId = message.sender.userId
|
|
|
|
|
val msg = message.rawMessage
|
|
|
|
|
val groupId = message.groupId
|
2024-09-19 18:15:42 +08:00
|
|
|
val messageId = message.messageId
|
|
|
|
|
println("$sender($senderId: $groupId >>> $messageId): $msg")
|
2024-09-18 13:06:23 +08:00
|
|
|
|
2024-09-20 22:23:43 +08:00
|
|
|
coroutineScope.launch {
|
|
|
|
|
message.message.forEach {
|
|
|
|
|
if (it.type == ArrayMessageType.image) {
|
|
|
|
|
val filename = it.data.file!!.split("=").last() + ".png"
|
|
|
|
|
URI(it.data.file!!).toURL().openConnection().inputStream.use { input ->
|
|
|
|
|
File("./files/images/$filename").outputStream().use { output ->
|
|
|
|
|
output.write(input.readBytes())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 14:05:20 +08:00
|
|
|
AsciiArtCommand.callback(message)
|
|
|
|
|
|
2024-09-15 22:50:19 +08:00
|
|
|
if (message.rawMessage.startsWith("https://github.com/") || message.rawMessage.startsWith("git@github.com:")) {
|
|
|
|
|
GitHubParseCommand.parse(this, message)
|
|
|
|
|
}
|
2024-09-12 15:43:44 +08:00
|
|
|
|
2024-09-12 11:18:57 +08:00
|
|
|
if (message.rawMessage.startsWith("BV") ||
|
|
|
|
|
message.rawMessage.startsWith("https://www.bilibili.com") ||
|
2024-09-12 13:06:13 +08:00
|
|
|
message.rawMessage.contains("https://b23.tv/") ||
|
|
|
|
|
message.message.find { it.type == ArrayMessageType.json } != null
|
2024-09-12 11:18:57 +08:00
|
|
|
) {
|
|
|
|
|
// parse bilibili video with a link, bvid or b23.tv link
|
|
|
|
|
val bvid = if (message.rawMessage.startsWith("BV")) {
|
|
|
|
|
message.rawMessage
|
2024-09-13 16:19:54 +08:00
|
|
|
} else if (message.rawMessage.split(" ").last().startsWith("https://b23.tv/")) {
|
2024-09-12 11:18:57 +08:00
|
|
|
val shortUrl = message.rawMessage.split(" ").last()
|
2024-09-12 13:06:13 +08:00
|
|
|
BVParseCommand.getShortUrlBVID(shortUrl)
|
|
|
|
|
} else if (message.message.find { it.type == ArrayMessageType.json } != null) {
|
|
|
|
|
val card = message.message.find { it.type == ArrayMessageType.json }!!
|
|
|
|
|
.data.data!!.toString().fromJson<CardShare>()
|
2024-09-13 16:19:54 +08:00
|
|
|
val shortUrl = if (card.meta.detail == null) {
|
|
|
|
|
if (!card.meta.news?.tag!!.contains("哔哩哔哩")) return
|
|
|
|
|
card.meta.news.jumpUrl
|
|
|
|
|
} else {
|
|
|
|
|
if (!card.meta.detail.title.contains("哔哩哔哩")) return
|
|
|
|
|
card.meta.detail.qqDocUrl
|
|
|
|
|
}
|
2024-09-12 13:06:13 +08:00
|
|
|
BVParseCommand.getShortUrlBVID(shortUrl)
|
2024-09-12 11:18:57 +08:00
|
|
|
} else {
|
2024-09-14 22:00:18 +08:00
|
|
|
message.rawMessage.split("?").first().split("/").filter { it.isNotEmpty() && it.isNotBlank() }.last()
|
2024-09-12 11:18:57 +08:00
|
|
|
}
|
|
|
|
|
BVParseCommand.parse(this, bvid, message)
|
2024-09-06 10:58:55 +08:00
|
|
|
}
|
|
|
|
|
if (message.message.any { it.type == ArrayMessageType.reply }) { // Image url parse
|
|
|
|
|
val command = message.message.reversed().find { it.type == ArrayMessageType.text }!!.data.text!!
|
|
|
|
|
val replyId = message.message.find { it.type == ArrayMessageType.reply }!!.data.id!!
|
2024-09-06 11:18:14 +08:00
|
|
|
if (command.contains("图来") || command.contains("图链")) {
|
2024-09-08 17:59:06 +08:00
|
|
|
// get image url
|
2024-09-06 10:58:55 +08:00
|
|
|
this.getMessage(replyId.toString().toLong(), "imageUrl", message.groupId)
|
|
|
|
|
}
|
2024-09-08 17:59:06 +08:00
|
|
|
if (command.contains("倒放") || command.contains("df")) {
|
|
|
|
|
// reverse gif
|
|
|
|
|
this.getMessage(replyId.toString().toLong(), "reverseGif", message.groupId)
|
|
|
|
|
}
|
2024-09-06 10:58:55 +08:00
|
|
|
}
|
2024-08-27 18:44:32 +08:00
|
|
|
}
|
2024-08-30 17:50:01 +08:00
|
|
|
|
2024-09-07 18:07:56 +08:00
|
|
|
override suspend fun onGroupMessageRevoke(message: GroupRevokeMessage) {
|
2024-09-18 13:06:23 +08:00
|
|
|
if (!configManager.enableAntiRevoke) return
|
2024-09-02 11:30:58 +08:00
|
|
|
val msg = MessageChain.Builder()
|
|
|
|
|
.addText("用户: ${message.userId} 被: ${message.operatorId} 撤回了一条消息")
|
|
|
|
|
.addNewLine()
|
|
|
|
|
.addText("使用/revoke ${message.messageId} 来获取被撤回的消息")
|
|
|
|
|
.build()
|
|
|
|
|
this.sendGroupMessage(message.groupId, msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-07 18:07:56 +08:00
|
|
|
override suspend fun onGetGroupMessageResponse(message: GetMessage) {
|
2024-09-06 10:58:55 +08:00
|
|
|
when (message.data.id) {
|
2024-09-20 16:30:33 +08:00
|
|
|
"revoke" -> AntiRevokeCommand.Companion.callback(this, message)
|
2024-09-06 10:58:55 +08:00
|
|
|
"imageUrl" -> ImageURLCommand.callback(this, message)
|
2024-09-08 17:59:06 +08:00
|
|
|
"reverseGif" -> ReverseGIFCommand.callback(this, message)
|
2024-09-02 11:30:58 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-07 18:07:56 +08:00
|
|
|
override suspend fun onWebsocketErrorEvent(ex: Exception) {
|
2024-09-02 11:30:58 +08:00
|
|
|
ex.printStackTrace()
|
2024-08-30 17:50:01 +08:00
|
|
|
}
|
2024-09-19 11:06:40 +08:00
|
|
|
|
|
|
|
|
override suspend fun onAddFriendRequest(event: AddFriendRequest) {
|
|
|
|
|
event.approve()
|
|
|
|
|
}
|
2024-09-19 15:11:40 +08:00
|
|
|
|
|
|
|
|
override suspend fun onGroupFileUpload(event: FileEvent) {
|
|
|
|
|
event.saveTo("./files")
|
|
|
|
|
}
|
2024-09-20 22:03:56 +08:00
|
|
|
|
|
|
|
|
override suspend fun onLeaveEvent(groupId: Long, userId: Long, operator: Long, time: Long) {
|
|
|
|
|
val msg = MessageChain.Builder()
|
|
|
|
|
.addText("有人退群了")
|
|
|
|
|
.addNewLine()
|
|
|
|
|
.addText("QQ: $userId | 操作者: ${if (operator == 0L) "主动退出" else operator}")
|
|
|
|
|
.addNewLine()
|
|
|
|
|
.addText("下次再见吧~~~")
|
|
|
|
|
.build()
|
|
|
|
|
this.sendGroupMessage(groupId, msg)
|
|
|
|
|
}
|
2024-08-29 14:36:06 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-04 20:01:09 +08:00
|
|
|
val configManager = ConfigManager()
|
|
|
|
|
val itemManager = ItemManager()
|
|
|
|
|
val signManager = SignManager()
|
2024-09-08 15:07:43 +08:00
|
|
|
val niuziManager = NiuziManager()
|
2024-08-30 14:24:46 +08:00
|
|
|
|
2024-09-03 14:37:00 +08:00
|
|
|
val items = listOf(
|
|
|
|
|
HeisiItem(),
|
|
|
|
|
BaisiItem(),
|
|
|
|
|
SetuItem()
|
|
|
|
|
)
|
|
|
|
|
|
2024-09-04 20:01:09 +08:00
|
|
|
val commands = listOf(
|
|
|
|
|
EchoCommand(), JrrpCommand(),
|
|
|
|
|
MusicCommand(), SignCommand(),
|
|
|
|
|
RedeemCommand(), MyPointCommand(),
|
|
|
|
|
HitokotoCommand(), FKXQSCommand(),
|
|
|
|
|
QRCodeCommand(), AntiRevokeCommand(),
|
|
|
|
|
MCPingCommand(), HelpCommand(),
|
2024-09-05 13:40:41 +08:00
|
|
|
WeatherCommand(), CigaretteCommand(),
|
2024-09-06 10:58:55 +08:00
|
|
|
RemakeCommand(), PixivCommand(),
|
2024-09-08 15:07:43 +08:00
|
|
|
RUACommand(), NslookupCommand(),
|
2024-09-09 17:52:41 +08:00
|
|
|
NiuziSignCommand(), MyNiuziCommand(),
|
2024-09-12 22:45:07 +08:00
|
|
|
JiJianCommand(), LikeMeCommand(),
|
2024-09-15 22:11:55 +08:00
|
|
|
CompilerCommand(), GPTCommand(),
|
2024-09-16 23:52:40 +08:00
|
|
|
GithubUserCommand(), AboutCommand(),
|
2024-09-17 22:37:42 +08:00
|
|
|
MusicPlayUrlCommand(), DomainPriceCommand(),
|
2024-09-19 11:06:40 +08:00
|
|
|
SendMailCommand(), ZiBiCommand(),
|
2024-09-20 13:30:52 +08:00
|
|
|
UnsetZiBiCommand(), WikipediaCommand(),
|
2024-09-20 16:53:38 +08:00
|
|
|
AsciiArtCommand(), StatusCommand()
|
2024-09-04 20:01:09 +08:00
|
|
|
)
|
2024-08-29 14:36:06 +08:00
|
|
|
|
2024-09-20 16:53:38 +08:00
|
|
|
val START_UP_TIME = Instant.now().epochSecond
|
|
|
|
|
|
2024-09-19 15:11:40 +08:00
|
|
|
fun initFilesDir() {
|
2024-09-20 22:23:43 +08:00
|
|
|
File("./files/images").also { it.mkdirs() }
|
2024-09-19 15:11:40 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-30 17:50:01 +08:00
|
|
|
suspend fun main() {
|
2024-08-27 18:44:32 +08:00
|
|
|
val fancyBot = FancyBot()
|
2024-08-30 15:05:51 +08:00
|
|
|
val workType = configManager.wsType
|
|
|
|
|
val accessToken = configManager.accessToken
|
|
|
|
|
val rob = if (workType == WSType.Client) {
|
|
|
|
|
val address = configManager.wsAddress
|
|
|
|
|
ROneBotFactory.createClient(address, accessToken, fancyBot)
|
|
|
|
|
} else {
|
|
|
|
|
val port = configManager.wsPort
|
|
|
|
|
ROneBotFactory.createServer(port, accessToken, fancyBot)
|
|
|
|
|
}
|
2024-08-30 17:50:01 +08:00
|
|
|
initDatabase()
|
2024-09-19 15:11:40 +08:00
|
|
|
initFilesDir()
|
2024-09-03 21:38:50 +08:00
|
|
|
val commandManager = rob.commandManager
|
2024-09-03 14:37:00 +08:00
|
|
|
commands.forEach { commandManager.register(it) }
|
|
|
|
|
items.forEach { itemManager.register(it) }
|
2024-09-03 21:38:50 +08:00
|
|
|
rob.addListeningGroups(*configManager.listeningGroups.toLongArray())
|
2024-08-27 18:44:32 +08:00
|
|
|
}
|