Files
FancyBot/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt
T

239 lines
9.3 KiB
Kotlin
Raw Normal View History

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-23 16:57:59 +08:00
import cn.rtast.fancybot.commands.lookup.*
2024-09-23 13:25:14 +08:00
import cn.rtast.fancybot.commands.misc.*
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
2024-09-21 14:12:08 +08:00
import cn.rtast.fancybot.commands.parse.CalculateCommand
2024-09-13 12:44:10 +08:00
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
2024-09-22 14:14:24 +08:00
import cn.rtast.fancybot.commands.record.ShortLinkCommand
2024-09-13 12:44:10 +08:00
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
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
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
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
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-22 20:51:39 +08:00
override suspend fun onWebsocketOpenEvent() {
this.broadcastMessage("FancyBot启动成功了呢~")
}
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-23 17:18:25 +08:00
if (message.rawMessage.toList().any { it in arrayListOf('*', '-', '/', '+', '=') }) {
2024-09-21 16:03:28 +08:00
val calculateResult = CalculateCommand.parse(message.rawMessage)
calculateResult?.let { message.reply(calculateResult) }
}
2024-09-21 13:36:21 +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
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
) {
// parse bilibili video with a link, bvid or b23.tv link
val bvid = if (message.rawMessage.startsWith("BV")) {
message.rawMessage
} else if (message.rawMessage.split(" ").last().startsWith("https://b23.tv/")) {
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>()
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)
} else {
2024-09-14 22:00:18 +08:00
message.rawMessage.split("?").first().split("/").filter { it.isNotEmpty() && it.isNotBlank() }.last()
}
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("图链")) {
// get image url
2024-09-06 10:58:55 +08:00
this.getMessage(replyId.toString().toLong(), "imageUrl", message.groupId)
}
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-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-22 21:45:26 +08:00
"revoke" -> AntiRevokeCommand.callback(this, message)
2024-09-06 10:58:55 +08:00
"imageUrl" -> ImageURLCommand.callback(this, message)
"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()
}
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-21 21:32:54 +08:00
CompilerCommand(), AICommand(),
GithubUserCommand(), AboutCommand(),
2024-09-17 22:37:42 +08:00
MusicPlayUrlCommand(), DomainPriceCommand(),
SendMailCommand(), ZiBiCommand(),
2024-09-20 13:30:52 +08:00
UnsetZiBiCommand(), WikipediaCommand(),
2024-09-21 16:03:28 +08:00
AsciiArtCommand(), StatusCommand(),
2024-09-23 13:25:14 +08:00
JueCommand(), ShortLinkCommand(),
2024-09-23 16:57:59 +08:00
TenSetuCommand(), ShotSelfCommand(),
ShotOtherCommand()
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
}
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)
2024-09-22 21:45:26 +08:00
.also { it.addListeningGroups(*configManager.listeningGroups.toLongArray()) }
2024-08-30 15:05:51 +08:00
} else {
val port = configManager.wsPort
ROneBotFactory.createServer(port, accessToken, fancyBot)
2024-09-22 21:45:26 +08:00
.also { it.addListeningGroups(*configManager.listeningGroups.toLongArray()) }
2024-08-30 15:05:51 +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-08-27 18:44:32 +08:00
}