Skip to content

Commit cda8599

Browse files
committed
add timers
1 parent 01abe74 commit cda8599

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ dependencies {
2424
implementation("org.apache.logging.log4j:log4j-api:2.13.1")
2525
implementation("org.apache.logging.log4j:log4j-core:2.13.1")
2626
implementation("org.apache.logging.log4j:log4j-slf4j-impl:2.13.1")
27+
28+
implementation("org.jetbrains.exposed", "exposed-jodatime", "0.23.1")
2729
}
2830

2931

izumi_config.template.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ enabledCommands: [
2020
# 启用的 pipeline 列表
2121
enabledPipelines: [
2222
repeat
23+
]
24+
25+
# 启用的 timer 列表
26+
enabledTimers: [
27+
life
2328
]

src/main/kotlin/me/lightless/izumi/Constant.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ object Constant {
44

55
const val COMMAND_PACKAGE_NAME = "me.lightless.izumi.plugin.command.impl"
66
const val PIPELINE_PACKAGE_NAME = "me.lightless.izumi.plugin.pipeline.impl"
7+
const val TIMER_PACKAGE_NAME = "me.lightless.izumi.plugin.timer.impl"
78

89
}

src/main/kotlin/me/lightless/izumi/MainApp.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package me.lightless.izumi
33
import kotlinx.coroutines.runBlocking
44
import me.lightless.izumi.config.ConfigParser
55
import me.lightless.izumi.core.Dispatcher
6+
import me.lightless.izumi.plugin.timer.TimerLoader
67
import net.mamoe.mirai.BotFactory
78
import net.mamoe.mirai.alsoLogin
89
import net.mamoe.mirai.event.events.GroupMessageEvent
@@ -53,6 +54,7 @@ class MainApp {
5354
this.logger.info("MessageDispatcher done.")
5455

5556
// 开启定时器
57+
TimerLoader.loadAndStart()
5658

5759
logger.info("izumi start finished.")
5860
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package me.lightless.izumi.plugin.timer
2+
3+
interface ITimer {
4+
5+
val name: String
6+
7+
@Suppress("unused")
8+
val daemon: Boolean get() = true
9+
10+
val period: Long
11+
12+
suspend fun process()
13+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package me.lightless.izumi.plugin.timer
2+
3+
import kotlinx.coroutines.GlobalScope
4+
import kotlinx.coroutines.async
5+
import me.lightless.izumi.ApplicationContext
6+
import me.lightless.izumi.Constant
7+
import me.lightless.izumi.util.BotLoader
8+
import org.slf4j.LoggerFactory
9+
import kotlin.reflect.full.createInstance
10+
11+
object TimerLoader {
12+
13+
private val logger = LoggerFactory.getLogger(javaClass)
14+
private val timers = mutableListOf<ITimer>()
15+
16+
suspend fun loadAndStart() {
17+
val timerClass = BotLoader.load(Constant.TIMER_PACKAGE_NAME)
18+
logger.debug("timer class name: $timerClass")
19+
20+
var allowedTimers = ApplicationContext.botConfig?.enabledTimers
21+
if (allowedTimers == null) {
22+
allowedTimers = emptyList()
23+
}
24+
25+
timerClass.forEach {
26+
try {
27+
val p = Class.forName(it).kotlin.createInstance() as ITimer
28+
if (p.name in allowedTimers) {
29+
timers.add(p)
30+
}
31+
} catch (e: ClassNotFoundException) {
32+
logger.error("Can't register timer class: $it")
33+
}
34+
}
35+
logger.debug("load finished. count: ${timers.size}, " +
36+
"list: ${timers.joinToString { it.name }}")
37+
38+
// 启动所有的 timer,每个timer单独一个协程
39+
// TODO 在 global scope 上开启一堆协程不是一个好事,先跑起来,下次一定改
40+
timers.forEach {
41+
GlobalScope.async {
42+
try {
43+
it.process()
44+
} catch (e: Exception) {
45+
logger.error("timer error, name: ${it.name}")
46+
e.printStackTrace()
47+
}
48+
}
49+
}
50+
}
51+
52+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package me.lightless.izumi.plugin.timer.impl
2+
3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.launch
5+
import me.lightless.izumi.ApplicationContext
6+
import me.lightless.izumi.plugin.timer.ITimer
7+
import net.mamoe.mirai.message.data.buildMessageChain
8+
import org.joda.time.DateTime
9+
import org.slf4j.LoggerFactory
10+
import java.math.BigDecimal
11+
import java.math.RoundingMode
12+
13+
@Suppress("unused")
14+
class Life: ITimer {
15+
16+
private val logger = LoggerFactory.getLogger(javaClass)
17+
18+
override val name: String
19+
get() = "life"
20+
21+
// 每分钟运行一次
22+
override val period: Long
23+
get() = 60 * 1000
24+
25+
override suspend fun process() {
26+
logger.debug("$name start!")
27+
28+
val bot = ApplicationContext.bot
29+
if (bot == null) {
30+
logger.debug("[$name] bot instance is null!")
31+
return
32+
}
33+
val groupNumber = ApplicationContext.botConfig?.allowedGroups ?: listOf()
34+
logger.debug("groupNumber: $groupNumber")
35+
36+
while (true) {
37+
val datetime = DateTime()
38+
val h: Int = datetime.hourOfDay
39+
val m: Int = datetime.minuteOfHour
40+
41+
// 每天早上 9 点 30 分,发送今年的进度
42+
if (h == 9 && m == 30) {
43+
val days = datetime.dayOfYear
44+
val percent = if (datetime.year().isLeap) {
45+
BigDecimal(days / 366.00 * 100.00).setScale(2, RoundingMode.HALF_UP)
46+
} else {
47+
BigDecimal(days / 365.00 * 100.00).setScale(2, RoundingMode.HALF_UP)
48+
}
49+
50+
val message = "【虚度光阴小助手】 \n今天是 ${datetime.year} 年的第 $days 天,今年已经过了 $percent% 啦!"
51+
bot.launch {
52+
groupNumber.forEach {
53+
bot.getGroup(it)?.sendMessage(buildMessageChain {
54+
add(message)
55+
})
56+
}
57+
}
58+
// 多 sleep 5 秒,防止同一分钟内发两次消息
59+
delay(1000 * 65)
60+
} else {
61+
// 如果不是 9 点 30 分就直接 sleep
62+
delay(1000 * 60)
63+
}
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)