Skip to content

Commit

Permalink
fix some feature
Browse files Browse the repository at this point in the history
  • Loading branch information
guimc233 committed Jul 6, 2024
1 parent 1091537 commit a753e31
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 33 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/ltd/guimc/lgzbot/PluginMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ object PluginMain : KotlinPlugin(
) {
lateinit var bypassMute: Permission
lateinit var blocked: Permission
lateinit var quiet: Permission
lateinit var nudgeMute: Permission
lateinit var disableSpamCheck: Permission
lateinit var disableADCheck: Permission
Expand Down Expand Up @@ -164,6 +165,7 @@ object PluginMain : KotlinPlugin(
bypassMute = register(PermissionId("lgzbot", "bypassmute"), "消息过滤器禁言豁免", root)
blocked = register(PermissionId("lgzbot", "blocked"), "完全屏蔽", root)
nudgeMute = register(PermissionId("lgzbot", "nudgemute"), "戳一戳禁言", root)
quiet = register(PermissionId("lgzbot", "quiet"), "安静一些", root)

disableRoot = register(PermissionId("lgzbot.disable", "*"), "The root permission", root)
disableSpamCheck = register(PermissionId("lgzbot.disable", "spamcheck"), "关闭群聊刷屏检查", disableRoot)
Expand Down
65 changes: 33 additions & 32 deletions src/main/kotlin/ltd/guimc/lgzbot/listener/message/FunListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,48 @@ package ltd.guimc.lgzbot.listener.message

import ltd.guimc.lgzbot.files.ModuleStateConfig
import ltd.guimc.lgzbot.utils.HttpUtils
import ltd.guimc.lgzbot.utils.ImageUtils
import ltd.guimc.lgzbot.utils.MessageUtils.getPlainText
import ltd.guimc.lgzbot.utils.OverflowUtils
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.event.events.GroupMessageEvent
import net.mamoe.mirai.message.data.At
import net.mamoe.mirai.message.data.ForwardMessage
import net.mamoe.mirai.message.data.ForwardMessageBuilder
import net.mamoe.mirai.message.data.PlainText
import org.json.JSONObject
import org.jsoup.Jsoup
import top.mrxiaom.overflow.OverflowAPI

object FunListener {
val COMMAND_PREFIX = "!"

suspend fun onMessage(event: GroupMessageEvent) {
when(event.message.getPlainText()) {
"摸鱼", "摸鱼!", "摸鱼.",
"摸鱼!", "摸鱼。" -> {
"${COMMAND_PREFIX}摸鱼", "${COMMAND_PREFIX}摸鱼!", "${COMMAND_PREFIX}摸鱼.",
"${COMMAND_PREFIX}摸鱼!", "${COMMAND_PREFIX}摸鱼。" -> {
if (ModuleStateConfig.moyu) {
try {
event.subject.sendMessage(At(event.sender) + getMoyu())
event.subject.sendMessage(
At(event.sender) + if (!OverflowUtils.checkOverflowCore()) {
ImageUtils.url2imageMessage(
"https://api.j4u.ink/v1/store/redirect/moyu/calendar/today.png",
event.bot,
event.subject
)
} else {
OverflowAPI.get()
.imageFromFile("https://api.j4u.ink/v1/store/redirect/moyu/calendar/today.png")
}
)
} catch (e: Throwable) {
event.subject.sendMessage("处理时发生异常... \nStackTrace:")
event.subject.sendMessage(build2forwardMessage(e.stackTraceToString(), event.subject))
}
}
}

"历史上的今天" -> {
"${COMMAND_PREFIX}历史上的今天" -> {
if (ModuleStateConfig.historytoday) {
try {
event.subject.sendMessage(getHistoryToday(event.subject))
Expand All @@ -36,42 +54,25 @@ object FunListener {
}
}
}

"今日运势" -> {
if (ModuleStateConfig.fortune) {
try {
event.subject.sendMessage(At(event.sender) + getEveryLuck(event.sender.id))
} catch (e: Throwable) {
event.subject.sendMessage("处理时发生异常... \nStackTrace:")
event.subject.sendMessage(build2forwardMessage(e.stackTraceToString(), event.subject))
}
}
}
}
}

private fun getMoyu(): String {
return HttpUtils.getJsonObject("http://bjb.yunwj.top/php/mo-yu/php.php")
.getString("wb")
.replace("【换行】", "\n")
}

private fun getHistoryToday(contact: Contact): ForwardMessage {
val msg = ForwardMessageBuilder(contact)
msg.add(contact.bot, PlainText(HttpUtils.getResponse("https://www.ipip5.com/today/api.php?type=txt")))
val jsonObject = HttpUtils.getJsonObject("https://api.oioweb.cn/api/common/history")
jsonObject.getJSONArray("result").forEach {
if (it is JSONObject) {
msg.add(
contact.bot, PlainText(
"${it.getString("year")}: ${it.getString("title").replace("\n", "")}\n" +
"${Jsoup.parse(it.getString("desc")).text()}\n来源: ${it.getString("link")}"
)
)
}
}
return msg.build()
}

private fun getEveryLuck(qq: Long): String {
val data = HttpUtils.getJsonObject("https://api.fanlisky.cn/api/qr-fortune/get/$qq")
.getJSONObject("data")

return "今日运势: ${data.getString("fortuneSummary")}\n" +
"运势星级: ${data.getString("luckyStar")}\n" +
"签文: ${data.getString("signText")}\n" +
"解签: ${data.getString("unSignText")}"
}

private fun build2forwardMessage(string: String, contact: Contact): ForwardMessage {
return ForwardMessageBuilder(contact)
.add(contact.bot, PlainText(string))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@

package ltd.guimc.lgzbot.listener.message

import ltd.guimc.lgzbot.PluginMain.adRegex
import ltd.guimc.lgzbot.PluginMain
import ltd.guimc.lgzbot.files.ModuleStateConfig
import ltd.guimc.lgzbot.utils.GithubUtils
import ltd.guimc.lgzbot.utils.MessageUtils.getPlainText
import ltd.guimc.lgzbot.utils.RegexUtils
import net.mamoe.mirai.console.permission.PermissionService.Companion.hasPermission
import net.mamoe.mirai.console.permission.PermitteeId.Companion.permitteeId
import net.mamoe.mirai.event.events.GroupMessageEvent
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle

object GithubUrlListener {
suspend fun onMessage(event: GroupMessageEvent) {
if (event.group.permitteeId.hasPermission(PluginMain.quiet)) return
if (ModuleStateConfig.githubquery) {
val plain = event.message.getPlainText()
val gitLink = GithubUtils.findGitLink(plain) ?: return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ object BakaListener : ListenerHost {
suspend fun MemberLeaveEvent.kick() {
if (!ModuleStateConfig.grouplistener) return
if (this.member == this.bot) return
if (this.group.permitteeId.hasPermission(PluginMain.quiet)) return
if (this !is MemberLeaveEvent.Kick) {
this.group.sendMessage(format(QUIT.random(), this.member.id))
return
Expand All @@ -71,20 +72,23 @@ object BakaListener : ListenerHost {
@EventHandler
suspend fun MemberMuteEvent.mute() {
if (!ModuleStateConfig.grouplistener) return
if (this.group.permitteeId.hasPermission(PluginMain.quiet)) return
if (this.operator == null) return
this.group.sendMessage(format(MUTE.random(), this.member.id, this.operator!!.id))
}

@EventHandler
suspend fun MemberUnmuteEvent.unmute() {
if (!ModuleStateConfig.grouplistener) return
if (this.group.permitteeId.hasPermission(PluginMain.quiet)) return
if (this.operator == null) return
this.group.sendMessage(format(UNMUTE.random(), this.member.id, this.operator!!.id))
}

@EventHandler
suspend fun MemberJoinEvent.newMember() {
if (!ModuleStateConfig.grouplistener) return
if (this.group.permitteeId.hasPermission(PluginMain.quiet)) return
this.group.sendMessage(format(NEW_MEMBER.random(), this.member.id))
if (this.member.permitteeId.hasPermission(Permission.getRootPermission())) {
this.group.sendMessage("挖欧!这是一个拥有机器人根权限的人!")
Expand All @@ -94,12 +98,14 @@ object BakaListener : ListenerHost {
@EventHandler
suspend fun BotMuteEvent.muteBot() {
if (!ModuleStateConfig.grouplistener) return
if (this.group.permitteeId.hasPermission(PluginMain.quiet)) return
this.operator.sendMessage(format(MUTE_TO_BOT.random()))
}

@EventHandler
suspend fun BotUnmuteEvent.unmuteBot() {
if (!ModuleStateConfig.grouplistener) return
if (this.group.permitteeId.hasPermission(PluginMain.quiet)) return
this.group.sendMessage(format(UNMUTE_TO_BOT.random(), this.operator.id))
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/ltd/guimc/lgzbot/utils/HttpUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object HttpUtils {
val basicAuth = "Basic : " + String(Base64.getEncoder().encode(auth.toByteArray()))
connection.setRequestProperty("Authorization", basicAuth)
}
connection.instanceFollowRedirects = true
connection.connect()
// 转换到json对象
val raw = connection.inputStream.bufferedReader().readText()
Expand All @@ -35,6 +36,7 @@ object HttpUtils {
connection.setRequestProperty("Authorization", basicAuth)
}
connection.connect()
connection.instanceFollowRedirects = true
// 转换到json对象
val raw = connection.inputStream.bufferedReader().readText()
return JSONArray(raw)
Expand All @@ -43,15 +45,25 @@ object HttpUtils {
fun getResponse(url: String): String {
val connection = java.net.URL(url).openConnection() as java.net.HttpURLConnection
connection.requestMethod = "GET"
connection.instanceFollowRedirects = true
connection.connect()
return connection.inputStream.bufferedReader().readText()
}

fun getBytesResponse(url: String): ByteArray? {
val connection = java.net.URL(url).openConnection() as java.net.HttpURLConnection
connection.requestMethod = "GET"
connection.instanceFollowRedirects = true
connection.connect()
return connection.inputStream.readAllBytes()
}

fun pushJson(url: String, json: String): String {
val connection = java.net.URL(url).openConnection() as java.net.HttpURLConnection
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty("Content-Length", json.length.toString())
connection.instanceFollowRedirects = true
connection.doOutput = true
connection.connect()
connection.outputStream.write(json.toByteArray())
Expand Down

0 comments on commit a753e31

Please sign in to comment.