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

199 lines
8.0 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
import cn.rtast.fancybot.commands.misc.PastebinCommand
2024-10-11 19:28:22 +08:00
import cn.rtast.fancybot.commands.misc.ReactionCommand
2024-10-06 21:18:23 +08:00
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.reply.ImageBedCommand
2024-10-02 21:02:05 +08:00
import cn.rtast.fancybot.enums.WSType
import cn.rtast.fancybot.util.*
import cn.rtast.fancybot.util.misc.convertToDate
2024-10-09 13:06:43 +08:00
import cn.rtast.fancybot.util.misc.initCommandAndItem
import cn.rtast.fancybot.util.misc.initFilesDir
import cn.rtast.fancybot.util.misc.initSetuIndex
import cn.rtast.fancybot.util.misc.isValidUrl
2024-08-27 18:44:32 +08:00
import cn.rtast.rob.ROneBotFactory
2024-09-25 12:51:34 +08:00
import cn.rtast.rob.entity.*
2024-09-27 19:36:56 +08:00
import cn.rtast.rob.entity.lagrange.FileEvent
2024-09-28 12:37:18 +08:00
import cn.rtast.rob.entity.lagrange.PokeEvent
2024-09-06 10:58:55 +08:00
import cn.rtast.rob.enums.ArrayMessageType
2024-10-01 20:18:19 +08:00
import cn.rtast.rob.enums.QQFace
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
import sun.net.www.content.text.plain
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-08-27 18:44:32 +08:00
class FancyBot : OneBotListener {
2024-09-02 11:30:58 +08:00
private val logger = Logger.getLogger<FancyBot>()
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.sendPrivateMessage(configManager.noticeUser, "FancyBot启动完成~")
2024-09-22 20:51:39 +08:00
}
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
logger.info("$sender($senderId: $groupId >>> $messageId): $msg")
logger.trace("$sender($senderId: $groupId >>> $messageId: $json")
2024-09-18 13:06:23 +08:00
2024-09-28 16:31:55 +08:00
if (message.message.any { it.type == ArrayMessageType.face && it.data.id.toString() == "419" }) {
message.reply("你发牛魔的火车呢, 我直接就是打断")
}
2024-09-27 23:31:01 +08:00
2024-09-24 14:56:51 +08:00
if (message.rawMessage.contains("原神")) {
2024-10-01 20:20:46 +08:00
(0..10).forEach { _ ->
2024-10-01 20:18:19 +08:00
message.reaction(QQFace.entries.random())
}
2024-09-24 14:56:51 +08:00
message.reply("你原神牛魔呢")
}
GitHubParseCommand.parse(message)
2024-09-28 16:31:55 +08:00
ReverseGIFCommand.callback(message)
AsciiArtCommand.callback(message)
2024-10-06 21:18:23 +08:00
ScanQRCodeCommand.callback(message)
2024-10-12 18:35:16 +08:00
BiliVideoParseCommand.parse(this, message)
BiliUserParseCommand.parse(this, message)
2024-10-13 11:57:56 +08:00
DouyinVideoParseCommand.parse(message)
2024-09-28 16:31:55 +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-10-12 20:59:31 +08:00
if (message.message.any { it.type == ArrayMessageType.reply }) {
2024-09-24 00:48:23 +08:00
val command = message.message.reversed().find { it.type == ArrayMessageType.text }!!.data.text!!
val replyId = message.message.find { it.type == ArrayMessageType.reply }!!.data.id!!
2024-10-12 20:59:31 +08:00
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)
}
2024-10-12 20:59:31 +08:00
if (command.contains("/ascii") || command.contains("/asc")) {
// 使用回复消息的方式直接对一个图片进行生成Ascii art的操作
val url = AsciiArtCommand.getImageUrl(getMsg)
val image = AsciiArtCommand.generateAsciiArt(url)
message.reply(image)
}
2024-09-24 00:48:23 +08:00
if (command.contains("图来") || command.contains("图链")) {
2024-10-12 20:59:31 +08:00
// 获取一个或多个图片的链接, 并且生成一个或多个短链接
2024-09-24 00:48:23 +08:00
ImageURLCommand.callback(message, getMsg)
}
2024-10-11 19:36:22 +08:00
if (command.contains("reaction")) {
2024-10-12 20:59:31 +08:00
// 使用reaction刷屏回应一条消息
2024-10-11 19:36:22 +08:00
ReactionCommand.reaction(this, message, getMsg.messageId)
}
2024-10-09 13:06:43 +08:00
if (command.contains("图床")) {
2024-10-12 20:59:31 +08:00
// 将一个图片上传到图床
ImageBedCommand.execute(getMsg, message)
2024-10-09 13:06:43 +08:00
}
2024-09-24 00:48:23 +08:00
}
2024-09-27 23:31:01 +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 ->
2024-10-06 20:48:50 +08:00
File("$ROOT_PATH/caches/images/$filename").outputStream().use { output ->
2024-09-27 23:31:01 +08:00
output.write(input.readBytes())
}
}
}
}
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 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) {
2024-10-06 18:07:32 +08:00
event.saveTo("$ROOT_PATH/caches/")
2024-09-19 15:11:40 +08:00
}
2024-09-20 22:03:56 +08:00
override suspend fun onLeaveEvent(groupId: Long, userId: Long, operator: Long, time: Long) {
val msg = MessageChain.Builder()
2024-09-25 16:01:15 +08:00
.addText("有人坐飞船离开了本星系~")
2024-09-20 22:03:56 +08:00
.addNewLine()
.addText("QQ: $userId | 操作者: ${if (operator == 0L) "主动退出" else operator}")
.addNewLine()
.addText("下次再见吧~~~")
.build()
this.sendGroupMessage(groupId, msg)
}
2024-09-28 12:37:18 +08:00
2024-10-11 13:47:26 +08:00
override suspend fun onGroupPoke(event: PokeEvent) {
if (event.targetId == configManager.selfId) {
val msg = MessageChain.Builder()
.addAt(event.userId)
.addText("你${event.action.first()}牛魔呢")
.build()
this.sendGroupMessage(event.groupId!!, msg)
2024-09-28 12:37:18 +08:00
}
}
override suspend fun onBeKicked(groupId: Long, operator: Long, time: Long) {
blackListManager.insertGroup(groupId, operator, time)
this.sendPrivateMessage(
configManager.noticeUser,
"被: ${groupId}踢出! 操作人: $operator 时间: ${time.convertToDate()} 已将其拉入黑名单!"
)
}
2024-08-29 14:36:06 +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-10-06 11:28:56 +08:00
initCommandAndItem(rob)
2024-10-06 18:20:53 +08:00
initSetuIndex()
2024-08-27 18:44:32 +08:00
}