Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: お掃除bot #8

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
Loading