Skip to content

Commit

Permalink
fix: GroupCurfewTimer
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Apr 17, 2022
1 parent 5ca7d03 commit 5c1fd12
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/xyz/cssxsh/mirai/plugin/MiraiAdministrator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,12 @@ public object MiraiAdministrator : SimpleListenerHost() {

@EventHandler
internal suspend fun GroupMuteAllEvent.mark() {
this.operator ?: return
val operator = this.operator ?: return
for (timer in ComparableService<GroupCurfewTimer>()) {
timer.sleep(group)
timer.ignore(contact = group)
}

group.sendMessage("${group.render()} 的定时宵禁状态已改变")
group.sendMessage("${operator.render()} 操作,忽略当前宵禁定时器 ")
}

// endregion
Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/xyz/cssxsh/mirai/plugin/MiraiCurfewTimer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import java.time.*
public object MiraiCurfewTimer : GroupCurfewTimer, MiraiCurfewTimerConfig by AdminTimerData {
override val level: Int = 0
override val id: String = "curfew-timer"
private val sleep: MutableMap<Long, Boolean> = HashMap()

override fun moment(contact: Group): LocalTime? {
val now: LocalTime = LocalTime.now()
Expand All @@ -18,12 +17,15 @@ public object MiraiCurfewTimer : GroupCurfewTimer, MiraiCurfewTimerConfig by Adm
}

override suspend fun run(contact: Group): Boolean? {
if (sleep[contact.id] ?: false) return null
if (System.currentTimeMillis() < (sleep[contact.id] ?: 0)) return null
val range: LocalTimeRange = muted[contact.id] ?: return null
return LocalTime.now() in range
}

override fun sleep(contact: Group, state: Boolean?) {
sleep[contact.id] = state ?: !(sleep[contact.id] ?: false)
override fun ignore(contact: Group) {
val now: LocalTime = LocalTime.now()
val range: LocalTimeRange = muted[contact.id] ?: return
val millis = listOf(range.start, range.endInclusive).minOf { wait(now, it) }
sleep[contact.id] = System.currentTimeMillis() + millis
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ public interface MiraiCurfewTimerConfig {
public val check: Long

public val muted: Map<Long, LocalTimeRange>

public val sleep: MutableMap<Long, Long>
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public object AdminTimerData : AutoSavePluginData("AdminTimerData"),
override val last: MutableMap<Long, Long> by value()

override val muted: MutableMap<Long, LocalTimeRange> by value()

override val sleep: MutableMap<Long, Long> by value()
}
4 changes: 2 additions & 2 deletions src/main/kotlin/xyz/cssxsh/mirai/spi/GroupCurfewTimer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import net.mamoe.mirai.contact.*
public interface GroupCurfewTimer : GroupTimerService<Boolean?> {

/**
* 睡眠状态,为真时,定期器不工作
* 设置睡眠,定期器不工作一段时间
*/
public fun sleep(contact: Group, state: Boolean? = null)
public fun ignore(contact: Group)
}

0 comments on commit 5c1fd12

Please sign in to comment.