diff --git a/src/main/kotlin/dev/t7e/mechatechkt/Main.kt b/src/main/kotlin/dev/t7e/mechatechkt/Main.kt index 7211d62..95cc07a 100644 --- a/src/main/kotlin/dev/t7e/mechatechkt/Main.kt +++ b/src/main/kotlin/dev/t7e/mechatechkt/Main.kt @@ -13,6 +13,7 @@ import dev.t7e.mechatechkt.commands.ProgressReportCommand import dev.t7e.mechatechkt.commands.WhereOJTChannelCommand import dev.t7e.mechatechkt.config.BotConfig import dev.t7e.mechatechkt.config.BotStatus +import dev.t7e.mechatechkt.unit.CleanUp import dev.t7e.mechatechkt.unit.ProgressReport lateinit var client: Kord @@ -52,6 +53,8 @@ suspend fun main(args: Array) { // schedule progress report ProgressReport.scheduleProgressReport() + // schedule clean up + CleanUp.scheduleCleanUp() // start bot client.login { diff --git a/src/main/kotlin/dev/t7e/mechatechkt/config/BotStatus.kt b/src/main/kotlin/dev/t7e/mechatechkt/config/BotStatus.kt index 1ca4675..b0006be 100644 --- a/src/main/kotlin/dev/t7e/mechatechkt/config/BotStatus.kt +++ b/src/main/kotlin/dev/t7e/mechatechkt/config/BotStatus.kt @@ -13,6 +13,7 @@ import java.io.File @Serializable data class BotStatus( var progressReportChannel: ULong = "0".toULong(), + var cleanUpChannel: ULong = "0".toULong(), var enabledProgressReport: Boolean = "false".toBoolean() ) { companion object { diff --git a/src/main/kotlin/dev/t7e/mechatechkt/unit/CleanUp.kt b/src/main/kotlin/dev/t7e/mechatechkt/unit/CleanUp.kt new file mode 100644 index 0000000..297c1aa --- /dev/null +++ b/src/main/kotlin/dev/t7e/mechatechkt/unit/CleanUp.kt @@ -0,0 +1,64 @@ +package dev.t7e.mechatechkt.unit + +import dev.kord.common.entity.Snowflake +import dev.kord.core.behavior.channel.asChannelOf +import dev.kord.core.behavior.channel.asChannelOfOrNull +import dev.kord.core.entity.channel.TextChannel +import dev.t7e.mechatechkt.client +import dev.t7e.mechatechkt.config.BotStatus +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import java.time.LocalDate +import java.time.format.DateTimeFormatter +import java.util.* +import java.util.concurrent.TimeUnit +import kotlin.concurrent.timerTask + +/** + * Created by testusuke on 2023/08/19 + * @author testusuke + */ +object CleanUp { + + private suspend fun TextChannel.postCleanUp() { + val message = this.createMessage("ちゃんと掃除してる?^^\n誰か写真あげてよ :eyes:") + } + + @OptIn(DelicateCoroutinesApi::class) + fun scheduleCleanUp() { + val timer = Timer() + + val task = timerTask { + // check enabled + if (!BotStatus.config.enabledProgressReport) return@timerTask + + GlobalScope.launch { + // check today is not weekend + val today = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) + if (today != Calendar.SATURDAY && today != Calendar.SUNDAY) { + // get channel + val channel = client.getChannel(Snowflake(BotStatus.config.cleanUpChannel)) + // post + channel?.asChannelOf()?.postCleanUp() + } + } + } + + val now = Calendar.getInstance() + val runTime = Calendar.getInstance().apply { + set(Calendar.HOUR_OF_DAY, 17) + set(Calendar.MINUTE, 50) + set(Calendar.SECOND, 0) + } + + // if now is after runTime, add 1 day + if (now.after(runTime)) { + runTime.add(Calendar.DAY_OF_YEAR, 1) + } + + timer.scheduleAtFixedRate(task, runTime.time, TimeUnit.DAYS.toMillis(1)) + + println("set clean up timer") + } +} \ No newline at end of file