From 3d0b05cb1fd08c8956138e28f5ae8816c3fcfe8d Mon Sep 17 00:00:00 2001 From: limited_dev Date: Mon, 5 Jun 2023 12:50:50 +0200 Subject: [PATCH] feat: added SendPlannerExtension Signed-off-by: limited_dev --- src/main/kotlin/net/moonleay/lilJudd/Bot.kt | 2 + .../extensions/SendPlannerExtension.kt | 115 ++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/main/kotlin/net/moonleay/lilJudd/extensions/SendPlannerExtension.kt diff --git a/src/main/kotlin/net/moonleay/lilJudd/Bot.kt b/src/main/kotlin/net/moonleay/lilJudd/Bot.kt index eef040b..413e0bf 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/Bot.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/Bot.kt @@ -31,6 +31,7 @@ import net.moonleay.lilJudd.buttons.component.EditButtonManager import net.moonleay.lilJudd.data.CredentialManager import net.moonleay.lilJudd.data.DB import net.moonleay.lilJudd.extensions.FeatureManageExtension +import net.moonleay.lilJudd.extensions.SendPlannerExtension import net.moonleay.lilJudd.extensions.VersionExtension import net.moonleay.lilJudd.features.TimePlanner import net.moonleay.lilJudd.util.Logger @@ -86,6 +87,7 @@ object Bot { extensions { add(::VersionExtension) add(::FeatureManageExtension) + add(::SendPlannerExtension) //add(::TestExtension) // See comment in TestExtension.kt } diff --git a/src/main/kotlin/net/moonleay/lilJudd/extensions/SendPlannerExtension.kt b/src/main/kotlin/net/moonleay/lilJudd/extensions/SendPlannerExtension.kt new file mode 100644 index 0000000..ff926d5 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/extensions/SendPlannerExtension.kt @@ -0,0 +1,115 @@ +/* + * 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 . + */ + +package net.moonleay.lilJudd.extensions + +import com.kotlindiscord.kord.extensions.commands.Arguments +import com.kotlindiscord.kord.extensions.commands.converters.impl.int +import com.kotlindiscord.kord.extensions.extensions.Extension +import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand +import com.kotlindiscord.kord.extensions.types.respond +import dev.kord.common.Color +import dev.kord.core.behavior.channel.createMessage +import dev.kord.rest.builder.message.create.actionRow +import kotlinx.coroutines.delay +import kotlinx.datetime.DayOfWeek +import net.moonleay.lilJudd.util.ButtonUtil +import net.moonleay.lilJudd.util.Logger +import net.moonleay.lilJudd.util.MessageUtil +import java.time.ZoneId +import java.time.ZonedDateTime + +/* This extension has no proper use. + It is used in testing to test stuff, without having to wait for certain events to trigger. */ +class SendPlannerExtension : Extension() { + override val name = "sendplanner" + override val allowApplicationCommandInDMs: Boolean + get() = false + + override suspend fun setup() { + publicSlashCommand(::PlannerArgs) { + name = "sendplanner" + description = "Send the planner for the current and x next weeks" + this.action { + val res = this.respond { + this.content = "OK." + } + res.delete() + if (this.arguments.weeks == 0) { + return@action + } + val c = this.getChannel().asChannel() + var then = + ZonedDateTime.now(ZoneId.of("Europe/Berlin")).withDayOfMonth(getMondayDayOfMonth()).withHour(4) + .withMinute(0).withSecond(0) + repeat(this.arguments.weeks) { + c.createMessage { + this.embeds.add( + MessageUtil.getEmbed( + Color(0X4C4645), + "Time Planning Feature", + "Do you have time on the following Days?", + "Automated Message" + ) + ) + } + delay(1000) + repeat(7) { + c.createMessage { + this.embeds.add( + MessageUtil.getEmbedWithTable( + Color(0X4C4645), + "", + "${then.dayOfWeek.name}, ${then.dayOfMonth}.${then.monthValue}.${then.year} /${it + 1}. weekday", + mapOf( + "Is available" to listOf(), + "May be available" to listOf(), + "Is not available" to listOf() + ) + ) + ) + + this.actionRow { + this.components.addAll(ButtonUtil.getTimePlannerButtons().components) + } + } + then = then.plusDays(1).withHour(4).withMinute(0).withSecond(0) + Logger.out("Finished sending day $it") + delay(1000) + } + Logger.out("Finished week $it") + } + Logger.out("Finished with ${c.data.guildId.value}") + } + } + } + + fun getMondayDayOfMonth(): Int { + val now = ZonedDateTime.now() + val monday = now.with(DayOfWeek.MONDAY) + return monday.dayOfMonth + } + + inner class PlannerArgs : Arguments() { + val weeks by int { + name = "weeks" + description = "Amount of weeks to send. Needs to be at least 1." + minValue = 1 + } + } +}