00ll00 5 anos atrás
pai
commit
c18a2e56a7

+ 1 - 1
src/main/kotlin/bot_luo_core/bot/BotLuo.kt

@@ -37,7 +37,7 @@ object BotLuo {
     /**
     /**
      * ## 所有机器人实例列表
      * ## 所有机器人实例列表
      */
      */
-    private val bots get() =  Bot.instances
+    val bots get() =  Bot.instances
 
 
     /**
     /**
      * ## 机器人与群组映射关系
      * ## 机器人与群组映射关系

+ 4 - 4
src/main/kotlin/bot_luo_core/cli/Cmd.kt

@@ -76,8 +76,8 @@ abstract class Cmd(val context: CmdContext) {
     ) = CmdReceipt(
     ) = CmdReceipt(
         time = context.time,
         time = context.time,
         success = false,
         success = false,
-        addCount = true,
-        setTime = true,
+        addCount = false,
+        setTime = false,
         addSpecialCount = addSpecialCount,
         addSpecialCount = addSpecialCount,
         uploadOutputFile = uploadFile
         uploadOutputFile = uploadFile
     )
     )
@@ -95,7 +95,7 @@ abstract class Cmd(val context: CmdContext) {
         time = context.time,
         time = context.time,
         success = false,
         success = false,
         addCount = true,
         addCount = true,
-        setTime = true,
+        setTime = false,
         addSpecialCount = addSpecialCount,
         addSpecialCount = addSpecialCount,
         uploadOutputFile = uploadFile
         uploadOutputFile = uploadFile
     )
     )
@@ -112,7 +112,7 @@ abstract class Cmd(val context: CmdContext) {
     ) = CmdReceipt(
     ) = CmdReceipt(
         time = context.time,
         time = context.time,
         success = false,
         success = false,
-        addCount = true,
+        addCount = false,
         setTime = true,
         setTime = true,
         addSpecialCount = addSpecialCount,
         addSpecialCount = addSpecialCount,
         uploadOutputFile = uploadFile
         uploadOutputFile = uploadFile

+ 5 - 2
src/main/kotlin/bot_luo_core/cli/CmdExecutable.kt

@@ -7,6 +7,7 @@ import bot_luo_core.cli.annotation.Method
 import bot_luo_core.cli.exceptions.CliInternalError
 import bot_luo_core.cli.exceptions.CliInternalError
 import bot_luo_core.data.Config.CMD_PREFIX
 import bot_luo_core.data.Config.CMD_PREFIX
 import kotlinx.coroutines.runBlocking
 import kotlinx.coroutines.runBlocking
+import org.apache.logging.log4j.Level
 import kotlin.jvm.Throws
 import kotlin.jvm.Throws
 import kotlin.reflect.KClass
 import kotlin.reflect.KClass
 import kotlin.reflect.KFunction
 import kotlin.reflect.KFunction
@@ -30,8 +31,8 @@ import kotlin.reflect.full.primaryConstructor
  */
  */
 class CmdExecutable(private val cmd: KClass<out Cmd>, private val meth: KFunction<CmdReceipt>) {
 class CmdExecutable(private val cmd: KClass<out Cmd>, private val meth: KFunction<CmdReceipt>) {
 
 
-    val cmdAnn: Command by lazy { cmd.findAnnotation()?:throw CliInternalError(NullPointerException ("命令类 $cmd 未被注解")) }
-    val methAnn: Method by lazy { meth.findAnnotation()?:throw CliInternalError(NullPointerException ("命令类 $cmd 中方法 ${meth.name} 未被注解")) }
+    val cmdAnn: Command = cmd.findAnnotation()?:throw CliInternalError(NullPointerException ("命令类 $cmd 未被注解"))
+    val methAnn: Method = meth.findAnnotation()?:throw CliInternalError(NullPointerException ("命令类 $cmd 中方法 ${meth.name} 未被注解"))
     val paramsAnn: Map<KParameter, Argument> by lazy { meth.parameters.filter { it.kind == KParameter.Kind.VALUE }.associateWith { it.findAnnotation()?: throw CliInternalError(NullPointerException ("命令类 $cmd 方法 ${meth.name} 参数 ${it.name} 未被注解")) } }
     val paramsAnn: Map<KParameter, Argument> by lazy { meth.parameters.filter { it.kind == KParameter.Kind.VALUE }.associateWith { it.findAnnotation()?: throw CliInternalError(NullPointerException ("命令类 $cmd 方法 ${meth.name} 参数 ${it.name} 未被注解")) } }
 
 
     val cmdName = cmdAnn.name
     val cmdName = cmdAnn.name
@@ -41,6 +42,8 @@ class CmdExecutable(private val cmd: KClass<out Cmd>, private val meth: KFunctio
     val order = methAnn.order
     val order = methAnn.order
     val pmsLevel = methAnn.pmsLevel
     val pmsLevel = methAnn.pmsLevel
 
 
+    val logLevel = Level.getLevel(methAnn.defaultLogLevel) ?: Level.INFO
+
     val display = cmdAnn.display
     val display = cmdAnn.display
     val id get() = "$cmdName-$methName"
     val id get() = "$cmdName-$methName"
     val idFixed get() = if (methName == "") cmdName else this.id
     val idFixed get() = if (methName == "") cmdName else this.id

+ 3 - 3
src/main/kotlin/bot_luo_core/cli/CmdHandler.kt

@@ -152,11 +152,11 @@ object CmdHandler {
                     context.user.cmdOnExecute(cmd)
                     context.user.cmdOnExecute(cmd)
                     context.group.cmdOnExecute(cmd)
                     context.group.cmdOnExecute(cmd)
 
 
-                    Logger.log(cmd, context, "开始执行")
+                    Logger.log(cmd, context, "开始执行", cmd.logLevel)
 
 
                     val res = cmd.executeWith(context, args)
                     val res = cmd.executeWith(context, args)
 
 
-                    Logger.log(cmd, context, "执行完成:${res}")
+                    Logger.log(cmd, context, "执行完成:${res}", cmd.logLevel)
 
 
                     runBlocking { withLockedAccessing(context.user, context.group, Cmds) {
                     runBlocking { withLockedAccessing(context.user, context.group, Cmds) {
                         if (res.addCount) {
                         if (res.addCount) {
@@ -207,7 +207,7 @@ object CmdHandler {
                 } finally {
                 } finally {
                     context.user.cmdFinished(cmd)
                     context.user.cmdFinished(cmd)
                     context.group.cmdFinished(cmd)
                     context.group.cmdFinished(cmd)
-                    Logger.log(cmd, context, "命令退出")
+                    Logger.log(cmd, context, "命令退出", cmd.logLevel)
                 }
                 }
                 return
                 return
             } catch (e: InvocationTargetException) {    //此时目标异常一定是Cli异常
             } catch (e: InvocationTargetException) {    //此时目标异常一定是Cli异常

+ 9 - 1
src/main/kotlin/bot_luo_core/cli/annotation/Method.kt

@@ -2,6 +2,7 @@ package bot_luo_core.cli.annotation
 
 
 import bot_luo_core.cli.Checker
 import bot_luo_core.cli.Checker
 import bot_luo_core.cli.CmdPermissionLevel
 import bot_luo_core.cli.CmdPermissionLevel
+import org.apache.logging.log4j.Level
 import kotlin.reflect.KClass
 import kotlin.reflect.KClass
 
 
 annotation class Method(
 annotation class Method(
@@ -47,5 +48,12 @@ annotation class Method(
 
 
     val usage: String = "",
     val usage: String = "",
     val title: String = "",
     val title: String = "",
-    val simples: Array<String> = []
+    val simples: Array<String> = [],
+
+    /**
+     * 默认的日志级别
+     *
+     * 除发生错误外的日志级别,为[Level]之一
+     */
+    val defaultLogLevel: String = "INFO"
 )
 )

+ 4 - 2
src/main/kotlin/bot_luo_core/cli/commands/StatCmd.kt

@@ -45,7 +45,7 @@ class StatCmd(context: CmdContext) : Cmd(context) {
                     "${k}:${map.values.sumOf { carrier.readCmdData(it).totalCount }}次",
                     "${k}:${map.values.sumOf { carrier.readCmdData(it).totalCount }}次",
                     map.entries
                     map.entries
                 ) { (key, value), builder ->
                 ) { (key, value), builder ->
-                    builder.td("-$key:")
+                        builder.td("-$key:")
                         .td("${carrier.readCmdData(value).totalCount}次")
                         .td("${carrier.readCmdData(value).totalCount}次")
                         .td(carrier.readCmdData(value).lastTime relativeToE context.time)
                         .td(carrier.readCmdData(value).lastTime relativeToE context.time)
                 }
                 }
@@ -73,8 +73,10 @@ class StatCmd(context: CmdContext) : Cmd(context) {
             table.th("${name}统计 —— $id").br()
             table.th("${name}统计 —— $id").br()
             filtered.groupBy { it.cmdName }.forEach { (k, v) ->
             filtered.groupBy { it.cmdName }.forEach { (k, v) ->
                 val map = v.associateBy { it.methName }
                 val map = v.associateBy { it.methName }
+                val total = map.values.sumOf { carrier.readCmdData(it).totalCount }
+                if (total > 0)
                 table.tr("$k:")
                 table.tr("$k:")
-                    .td("${map.values.sumOf { carrier.readCmdData(it).totalCount }}次")
+                    .td("${total}次")
                     .td(map.values.maxOf { carrier.readCmdData(it).lastTime } relativeToE context.time)
                     .td(map.values.maxOf { carrier.readCmdData(it).lastTime } relativeToE context.time)
             }
             }
             context.print(table.toString())
             context.print(table.toString())

+ 6 - 0
src/main/kotlin/bot_luo_core/data/Notice.kt

@@ -1,5 +1,6 @@
 package bot_luo_core.data
 package bot_luo_core.data
 
 
+import bot_luo_core.bot.BotLuo
 import bot_luo_core.bot.BotLuo.isMainBotOf
 import bot_luo_core.bot.BotLuo.isMainBotOf
 import bot_luo_core.bot.VirtualMessageEvent
 import bot_luo_core.bot.VirtualMessageEvent
 import bot_luo_core.util.Logger.sendMessageWithLog
 import bot_luo_core.util.Logger.sendMessageWithLog
@@ -9,12 +10,14 @@ import bot_luo_core.util.Text.limitMid
 import bot_luo_core.util.Time
 import bot_luo_core.util.Time
 import bot_luo_core.util.Time.relativeToE
 import bot_luo_core.util.Time.relativeToE
 import net.mamoe.mirai.Bot
 import net.mamoe.mirai.Bot
+import net.mamoe.mirai.contact.AnonymousMember
 import net.mamoe.mirai.contact.Contact
 import net.mamoe.mirai.contact.Contact
 import net.mamoe.mirai.event.EventPriority
 import net.mamoe.mirai.event.EventPriority
 import net.mamoe.mirai.event.GlobalEventChannel
 import net.mamoe.mirai.event.GlobalEventChannel
 import net.mamoe.mirai.event.events.GroupAwareMessageEvent
 import net.mamoe.mirai.event.events.GroupAwareMessageEvent
 import net.mamoe.mirai.event.events.GroupMessageEvent
 import net.mamoe.mirai.event.events.GroupMessageEvent
 import net.mamoe.mirai.event.events.MessageEvent
 import net.mamoe.mirai.event.events.MessageEvent
+import net.mamoe.mirai.event.events.MessageSyncEvent
 import net.mamoe.mirai.message.code.MiraiCode.deserializeMiraiCode
 import net.mamoe.mirai.message.code.MiraiCode.deserializeMiraiCode
 import net.mamoe.mirai.message.data.At
 import net.mamoe.mirai.message.data.At
 import net.mamoe.mirai.message.data.MessageChainBuilder
 import net.mamoe.mirai.message.data.MessageChainBuilder
@@ -23,7 +26,10 @@ object Notice: DataObject("data/notice.json", 10000, false)
 {
 {
     init {
     init {
         GlobalEventChannel.subscribeAlways<MessageEvent>(priority = EventPriority.HIGH) {
         GlobalEventChannel.subscribeAlways<MessageEvent>(priority = EventPriority.HIGH) {
+            if (this.sender is AnonymousMember) return@subscribeAlways
+            if (this is MessageSyncEvent) return@subscribeAlways
             if (this is GroupMessageEvent && !bot.isMainBotOf(this.group.id)) return@subscribeAlways
             if (this is GroupMessageEvent && !bot.isMainBotOf(this.group.id)) return@subscribeAlways
+            if (BotLuo.bots.find { it.id == this.sender.id } != null) return@subscribeAlways
             check(
             check(
                 Users.readUser(this.sender.id),
                 Users.readUser(this.sender.id),
                 if (this is GroupAwareMessageEvent) Groups.readGroup(this.group.id) else Groups.virtualGroup,
                 if (this is GroupAwareMessageEvent) Groups.readGroup(this.group.id) else Groups.virtualGroup,