add defined[T] for EventEnv

- Now OnQuestionMarkReply skips expired event will use this feature
- OnUserRandom.RandomSelect capability patch for OnQuestionMarkReply will use this feature
This commit is contained in:
A.C.Sukazyo Eyre 2025-04-09 15:46:30 +08:00
parent 2b2b93ddaa
commit 666607b430
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
4 changed files with 25 additions and 9 deletions

View File

@ -51,6 +51,20 @@ class EventEnv (
case None => ConsumeResult(false)
}
// fixme: defined() series is not tested yet
def defined [T: ClassTag]: Boolean = {
variables get classTag[T].runtimeClass match
case Some(_) => true
case None => false
}
def defined [T] (t: Class[T]): Boolean = {
variables get t match
case Some(_) => true
case None => false
}
def consume [T: ClassTag] (consumer: T => Unit): ConsumeResult =
variables get classTag[T].runtimeClass match
case Some(i) => consumer(i.asInstanceOf[T]); ConsumeResult(true)

View File

@ -11,10 +11,10 @@ class MornyEventListeners (using manager: EventListenerManager) (using coeur: Mo
// KUOHUANHUAN_NEED_SLEEP
OnOnAlias(),
OnUniMeowTrigger(using coeur.commands),
OnQuestionMarkReply(),
$OnUserRandom.RandomSelect,
//noinspection NonAsciiCharacters
$OnUserRandom.尊嘟假嘟,
OnQuestionMarkReply(),
OnUserSlashAction(),
OnCallMe(),
OnCallMsgSend(),

View File

@ -16,12 +16,7 @@ class OnQuestionMarkReply (using coeur: MornyCoeur) extends EventListener {
import event.update
// FIXME: not tested yet, due to cannot connect to test bot
if boundary[Boolean] {
event.consume[MornyOnUpdateTimestampOffsetLock.ExpiredEvent.type] { _ =>
boundary.break(true)
}
false
} then {
if event.defined[MornyOnUpdateTimestampOffsetLock.ExpiredEvent.type] then {
logger.debug("OnQuestionMarkReply: expired event, skipped")
return
} else {
@ -32,8 +27,12 @@ class OnQuestionMarkReply (using coeur: MornyCoeur) extends EventListener {
import cc.sukazyo.cono.morny.util.UseMath.over
import cc.sukazyo.cono.morny.util.UseRandom.chance_is
if (1 over 8) chance_is false then return;
if !isAllMessageMark(using update.message.text) then return;
// provide a mark to notify the following event
// that the current event is all made of question marks
event.provide(OnQuestionMarkReply.Marked)
if (1 over 8) chance_is false then return;
if (update.message.hasProtectedContent) {
// Copy the message
@ -77,4 +76,6 @@ object OnQuestionMarkReply {
}
}
object Marked
}

View File

@ -29,7 +29,8 @@ class OnUserRandom (using coeur: MornyCoeur) {
if rand_half then _con1 else _con2
case USER_IF_QUERY(_con) =>
// for capability with [[OnQuestionMarkReply]]
if OnQuestionMarkReply.isAllMessageMark(using _con) then return;
// fixme: not tested yet
if event.defined[OnQuestionMarkReply.Marked.type] then return;
(if rand_half then "不" else "") + _con
case _ => null