Skip to content

Commit

Permalink
Merge pull request #8 from TNCT-Mechatech/feature/clean-up
Browse files Browse the repository at this point in the history
feat: お掃除bot
  • Loading branch information
testusuke authored Feb 27, 2024
2 parents 9e2366d + 1ee4813 commit d0924da
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/dev/t7e/mechatechkt/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,6 +53,8 @@ suspend fun main(args: Array<String>) {

// schedule progress report
ProgressReport.scheduleProgressReport()
// schedule clean up
CleanUp.scheduleCleanUp()

// start bot
client.login {
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/dev/t7e/mechatechkt/config/BotStatus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
64 changes: 64 additions & 0 deletions src/main/kotlin/dev/t7e/mechatechkt/unit/CleanUp.kt
Original file line number Diff line number Diff line change
@@ -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<TextChannel>()?.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")
}
}

0 comments on commit d0924da

Please sign in to comment.