feat: finished TimePlanner, the bot now can send a planning message mondays 4AM UTC

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-05-18 20:50:43 +02:00
parent df3c732667
commit bbf08ba2d7
8 changed files with 279 additions and 9 deletions

View file

@ -39,6 +39,7 @@ val coroutinesver = "1.1.0"
val ktor_version = "2.3.0" val ktor_version = "2.3.0"
val exposedver = "0.40.1" val exposedver = "0.40.1"
val postgresver = "42.3.8" val postgresver = "42.3.8"
val krontabver = "1.0.0"
val mavenArtifact = "lilJudd" val mavenArtifact = "lilJudd"
project.base.archivesName.set(mavenArtifact) project.base.archivesName.set(mavenArtifact)
@ -93,6 +94,9 @@ dependencies {
shadow("org.jetbrains.exposed:exposed-dao:$exposedver") shadow("org.jetbrains.exposed:exposed-dao:$exposedver")
shadow("org.jetbrains.exposed:exposed-jdbc:$exposedver") shadow("org.jetbrains.exposed:exposed-jdbc:$exposedver")
shadow("org.postgresql:postgresql:$postgresver") shadow("org.postgresql:postgresql:$postgresver")
//Korntab
shadow("dev.inmo:krontab:$krontabver")
} }
@ -106,7 +110,8 @@ val templateProps = mapOf(
"coroutinesversion" to coroutinesver, "coroutinesversion" to coroutinesver,
"ktorversion" to ktor_version, "ktorversion" to ktor_version,
"exposedversion" to exposedver, "exposedversion" to exposedver,
"postgresversion" to postgresver "postgresversion" to postgresver,
"krontabversion" to krontabver
) )

View file

@ -20,17 +20,20 @@ package net.moonleay.lilJudd
import com.kotlindiscord.kord.extensions.ExtensibleBot import com.kotlindiscord.kord.extensions.ExtensibleBot
import dev.kord.common.entity.PresenceStatus import dev.kord.common.entity.PresenceStatus
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import net.moonleay.botendo.build.BuildConstants import net.moonleay.botendo.build.BuildConstants
import net.moonleay.lilJudd.data.CredentialManager import net.moonleay.lilJudd.data.CredentialManager
import net.moonleay.lilJudd.data.DB import net.moonleay.lilJudd.data.DB
import net.moonleay.lilJudd.extensions.FeatureManageExtension import net.moonleay.lilJudd.extensions.FeatureManageExtension
import net.moonleay.lilJudd.extensions.VersionExtension import net.moonleay.lilJudd.extensions.VersionExtension
import net.moonleay.lilJudd.features.TimePlanner
import net.moonleay.lilJudd.util.Logger import net.moonleay.lilJudd.util.Logger
import kotlin.system.exitProcess import kotlin.system.exitProcess
object Bot { object Bot {
//The kord object gets set at app launch //The kord object gets set at app launch
private lateinit var bot: ExtensibleBot lateinit var bot: ExtensibleBot
suspend fun start() { suspend fun start() {
Logger.out("Starting Bot...") Logger.out("Starting Bot...")
@ -49,7 +52,7 @@ object Bot {
exitProcess(3) exitProcess(3)
} }
//Connect to the Database //Connect to the database
DB.connect( DB.connect(
CredentialManager.dbDomain, CredentialManager.dbDomain,
CredentialManager.dbName, CredentialManager.dbName,
@ -57,6 +60,17 @@ object Bot {
CredentialManager.dbPassword CredentialManager.dbPassword
) )
//Register the TimePlanner thread
val coroutineJob = GlobalScope.launch {
TimePlanner.registerThread()
}
// Add a shutdown hook to cancel the coroutine when the application is terminated
Runtime.getRuntime().addShutdownHook(Thread {
coroutineJob.cancel()
})
//Add Bot token to kord //Add Bot token to kord
bot = ExtensibleBot(CredentialManager.token) { bot = ExtensibleBot(CredentialManager.token) {
applicationCommands { applicationCommands {

View file

@ -92,7 +92,7 @@ class FeatureManageExtension : Extension() {
MessageUtil.getEmbed( MessageUtil.getEmbed(
Color(0x52E01A), Color(0x52E01A),
"200: Success", "200: Success",
"The feature was enabled in channel ${args.channel.data.name}", "The feature was enabled in channel ${args.channel.data.name.value}",
u.asUser().username + "#" + u.asUser().discriminator u.asUser().username + "#" + u.asUser().discriminator
) )
) )

View file

@ -37,7 +37,8 @@ class VersionExtension : Extension() {
"Lil' Judd", "Lil' Judd",
"Lil' Judd ***v." + BuildConstants.version + "***\n" + "Lil' Judd ***v." + BuildConstants.version + "***\n" +
"Kord-Extensions ***v." + BuildConstants.kordVersion + "***\n" + "Kord-Extensions ***v." + BuildConstants.kordVersion + "***\n" +
"Coroutines ***v." + BuildConstants.coroutinesVersion + "***" "Coroutines ***v." + BuildConstants.coroutinesVersion + "***\n" +
"Krontab ***v." + BuildConstants.krontabVersion + "***"
) )
} }
} }

View file

@ -0,0 +1,110 @@
/*
* lilJudd
* Copyright (C) 2023 moonleay
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.moonleay.lilJudd.features
import com.kotlindiscord.kord.extensions.utils.addReaction
import dev.inmo.krontab.buildSchedule
import dev.inmo.krontab.doInfinity
import dev.kord.common.Color
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.channel.createMessage
import dev.kord.core.entity.Message
import dev.kord.core.entity.channel.MessageChannel
import kotlinx.coroutines.delay
import net.moonleay.lilJudd.Bot
import net.moonleay.lilJudd.data.tables.TimePlanningChannels
import net.moonleay.lilJudd.util.Logger
import net.moonleay.lilJudd.util.MessageUtil
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import java.time.ZoneId
import java.time.ZonedDateTime
object TimePlanner {
/* /--------------- Seconds
| /------------- Minutes
| | /----------- Hours
| | | /--------- Days of months
| | | | /------- Months
| | | | | /----- (optional) Year
| | | | | | /--- (optional) Timezone offset
| | | | | | | / (optional) Week days
* * * * * * 0o *w*/
suspend fun registerThread() {
Logger.out("Adding scheduler...")
val scheduler = buildSchedule("0 0 4 * * * 0o 1") // 0 0 4 * * * 0o 1 // 0o is UTC
scheduler.doInfinity {
Logger.out("Starting to notify...")
val channelList = mutableListOf<Snowflake>()
transaction {
for (tp in TimePlanningChannels.selectAll()) {
channelList.add(Snowflake(tp[TimePlanningChannels.channelid]))
Logger.out("Have to notify channel with ID ${tp[TimePlanningChannels.channelid]}.")
}
}
Logger.out("${channelList.count()} Channels to notify!")
for (ch in channelList) {
if (Bot.bot.kordRef.getChannel(ch)!!.asChannelOrNull() == null)
return@doInfinity
val c = Bot.bot.kordRef.getChannelOf<MessageChannel>(ch)!!
c.createMessage {
this.embeds.add(
MessageUtil.getEmbed(
Color(0X4C4645),
"Time Planning Feature",
"Do you have time on the following Days?",
"Automated Message"
)
)
}
delay(3000)
var then = ZonedDateTime.now(ZoneId.of("Europe/Berlin")).withHour(4).withMinute(0).withSecond(0)
repeat(6) {
val m = c.createMessage {
this.embeds.add(
MessageUtil.getEmbedSmall(
Color(0X4C4645),
"",
"${then.dayOfWeek.name}, ${then.dayOfMonth}.${then.monthValue}.${then.year} /${it + 1}. weekday"
)
)
}
then = then.plusDays(1).withHour(20).withMinute(24).withSecond(0)
delay(500)
addReactions(m)
delay(1000)
Logger.out("Finished sending day $it")
}
Logger.out("Finished with ${c.data.guildId.value}")
}
Logger.out("Done! Until next Monday! <3 ")
}
}
private suspend fun addReactions(msg: Message) {
msg.addReaction("")
delay(300)
msg.addReaction("")
delay(300)
msg.addReaction("")
delay(300)
}
}

View file

@ -70,6 +70,18 @@ object MessageUtil {
} }
} }
///Get an embedded msg with title and description
fun getEmbedSmall(
color: Color,
title: String,
description: String
): EmbedBuilder {
val ebb = EmbedBuilder()
ebb.title = title
ebb.description = description
return ebb
}
///Get an embedded msg with title, description and a src ///Get an embedded msg with title, description and a src
fun getEmbed( fun getEmbed(
color: Color, color: Color,
@ -77,10 +89,8 @@ object MessageUtil {
description: String, description: String,
source: String source: String
): EmbedBuilder { ): EmbedBuilder {
val ebb = getEmbedSmall(color, title, description)
val now: LocalDateTime = LocalDateTime.now() val now: LocalDateTime = LocalDateTime.now()
val ebb = EmbedBuilder()
ebb.title = title
ebb.description = description
ebb.footer = EmbedBuilder.Footer() ebb.footer = EmbedBuilder.Footer()
ebb.footer!!.text = ">" + dtf.format(now) + " - $source" ebb.footer!!.text = ">" + dtf.format(now) + " - $source"
ebb.color = color ebb.color = color

View file

@ -0,0 +1,129 @@
/*
* lilJudd
* Copyright (C) 2023 moonleay
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.moonleay.lilJudd.util
import java.time.Duration
import java.util.concurrent.TimeUnit
object TimeUtil {
fun getTimeFormatedShortend(time2: Long): String {
var time = time2
val days: Long = TimeUnit.MILLISECONDS
.toDays(time)
time -= TimeUnit.DAYS.toMillis(days)
val hours: Long = TimeUnit.MILLISECONDS
.toHours(time)
time -= TimeUnit.HOURS.toMillis(hours)
val minutes: Long = TimeUnit.MILLISECONDS
.toMinutes(time)
time -= TimeUnit.MINUTES.toMillis(minutes)
val seconds: Long = TimeUnit.MILLISECONDS
.toSeconds(time)
var s = ""
if (days >= 1) {
s += days.toString() + "d "
}
if (hours >= 1) {
s += hours.toString() + "h "
}
if (minutes >= 1) {
s += minutes.toString() + "m "
}
if (seconds >= 1 && hours < 1) {
s += seconds.toString() + "s"
}
if (s.isEmpty() || s.isBlank()) {
s = "None"
}
return s
}
fun getTimeFormatedRaw(time2: Long): String {
var time = time2
val days: Long = TimeUnit.MILLISECONDS
.toDays(time)
time -= TimeUnit.DAYS.toMillis(days)
val hours: Long = TimeUnit.MILLISECONDS
.toHours(time)
time -= TimeUnit.HOURS.toMillis(hours)
val minutes: Long = TimeUnit.MILLISECONDS
.toMinutes(time)
time -= TimeUnit.MINUTES.toMillis(minutes)
val seconds: Long = TimeUnit.MILLISECONDS
.toSeconds(time)
var s = ""
if (days >= 1) {
s += days.toString() + "d "
}
if (hours >= 1) {
s += hours.toString() + "h "
}
if (minutes >= 1) {
s += minutes.toString() + "m "
}
if (seconds >= 1) {
s += seconds.toString() + "s"
}
if (s.isEmpty() || s.isBlank()) {
s = "None"
}
return s
}
//This 100000%ly can be improved, I wrote this at 2am
fun getTimeUnformated(timeStr: String): Long {
var days: Long = 0
var hours: Long = 0
var minutes: Long = 0
var seconds: Long = 0
val timeArr = timeStr.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
for (s in timeArr) {
if (s.endsWith("d")) {
days = s.split("d".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0].toLong()
} else if (s.endsWith("h")) {
hours = s.split("h".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0].toLong()
} else if (s.endsWith("m")) {
minutes = s.split("m".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0].toLong()
} else if (s.endsWith("s")) {
seconds = s.split("s".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0].toLong()
}
}
return Duration.ofSeconds(seconds).plus(Duration.ofMinutes(minutes)).plus(Duration.ofHours(hours))
.plus(Duration.ofDays(days)).toMillis()
}
private val DaysUntilMonday: Map<String, Int> = object : HashMap<String, Int>() {
init {
put("MONDAY", 0)
put("TUESDAY", 6)
put("WEDNESDAY", 5)
put("THURSDAY", 4)
put("FRIDAY", 3)
put("SATURDAY", 2)
put("SUNDAY", 1)
}
}
fun getDelay(day: String): Int {
return DaysUntilMonday[day]!!
}
}

View file

@ -1,6 +1,6 @@
/* /*
* lilJudd * lilJudd
* Copyright (C) 2023 limited_dev * Copyright (C) 2023 moonleay
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -26,4 +26,5 @@ internal object BuildConstants {
const val ktorVersion = "${ktorversion}" const val ktorVersion = "${ktorversion}"
const val exposedVersion = "${exposedversion}" const val exposedVersion = "${exposedversion}"
const val postgresVersion = "${postgresversion}" const val postgresVersion = "${postgresversion}"
const val krontabVersion = "${krontabversion}"
} }