11package me.lightless.izumi.plugin.timer.impl
22
3+ import kotlinx.coroutines.delay
4+ import me.lightless.izumi.ApplicationContext
5+ import me.lightless.izumi.dao.ChatMessage
6+ import me.lightless.izumi.dao.ChatMessageDAO
37import me.lightless.izumi.plugin.timer.ITimer
8+ import net.mamoe.mirai.Bot
9+ import org.jetbrains.exposed.sql.and
10+ import org.jetbrains.exposed.sql.transactions.transaction
11+ import org.joda.time.DateTime
12+ import org.joda.time.DateTimeZone
13+ import org.joda.time.LocalDate
414import org.slf4j.LoggerFactory
515
616@Suppress(" unused" )
@@ -13,8 +23,75 @@ class Ryuo : ITimer {
1323 override val period: Long
1424 get() = 60 * 1000 // 每分钟运行一次
1525
26+ private fun getBotInstance (): Bot ? {
27+ return ApplicationContext .bot
28+ }
29+
1630 override suspend fun process () {
1731 this .logger.debug(" $name start!" )
32+ val bot = this .getBotInstance()
33+ if (bot == null ) {
34+ logger.debug(" [${this .name} ] bot instance is null!" )
35+ return
36+ }
37+ val allowedGroups = ApplicationContext .botConfig?.allowedGroups ? : listOf ()
38+ logger.debug(" groupNumber: $allowedGroups " )
39+
40+ while (true ) {
41+ val datetime = DateTime ()
42+ val h = datetime.hourOfDay
43+ val m = datetime.minuteOfHour
44+
45+ // 每天早上 9 点 30 分,发送龙王数据
46+ if (h == 9 && m == 30 ) {
47+ // TODO()
48+ val today = LocalDate .now(DateTimeZone .forOffsetHours(8 ))
49+ val yesterday = today.minusDays(1 )
50+ val ryuoMap = mutableMapOf<Long , MutableMap <Long , Long >>()
51+
52+ // 从数据库里查出来昨天的全部信息
53+ val messageList = transaction {
54+ ChatMessageDAO .find {
55+ ChatMessage .createdTime less today and
56+ (ChatMessage .createdTime greaterEq yesterday)
57+ }.toList()
58+ }
1859
60+ // 计算龙王信息
61+ for (msgDAO in messageList) {
62+ if (msgDAO.groupId !in ryuoMap.keys) {
63+ ryuoMap[msgDAO.groupId] = mutableMapOf ()
64+ }
65+ val innerMap = ryuoMap[msgDAO.groupId] ? : continue
66+ if (msgDAO.qq !in innerMap.keys) {
67+ innerMap[msgDAO.qq] = 0
68+ } else {
69+ innerMap[msgDAO.qq] = innerMap[msgDAO.qq] as Long + 1
70+ }
71+ }
72+ this .logger.debug(" ryuoMap: $ryuoMap " )
73+
74+ // 发消息
75+ // for (allowedGroupId in allowedGroups) {
76+ // val innerMap = ryuoMap.get(allowedGroupId) ?: continue
77+ //
78+ // }
79+ // var message = "[龙王通知] 恭喜 xxx 成为今天的龙王,快来给大家表演个喷水吧!\n"
80+
81+
82+ // 多 sleep 5 秒,防止同一分钟内发两次消息
83+ delay(65 * 1000 )
84+ } else {
85+ delay(60 * 1000 )
86+ }
87+ }
1988 }
89+ }
90+
91+ fun main () {
92+ val today = LocalDate .now(DateTimeZone .forOffsetHours(8 ))
93+ println (today.toString())
94+
95+ val yesterday = today.minusDays(1 )
96+ println (yesterday.toString())
2097}
0 commit comments