From 3d0b05cb1fd08c8956138e28f5ae8816c3fcfe8d Mon Sep 17 00:00:00 2001 From: limited_dev Date: Mon, 5 Jun 2023 12:50:50 +0200 Subject: [PATCH 1/4] 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 + } + } +} From 13cf183fd56a140c18e1b3b8e4a9965666cc8f22 Mon Sep 17 00:00:00 2001 From: limited_dev Date: Mon, 5 Jun 2023 12:51:07 +0200 Subject: [PATCH 2/4] fix: fixed time in TimePlanner Signed-off-by: limited_dev --- src/main/kotlin/net/moonleay/lilJudd/features/TimePlanner.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/net/moonleay/lilJudd/features/TimePlanner.kt b/src/main/kotlin/net/moonleay/lilJudd/features/TimePlanner.kt index a8bb189..778673a 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/features/TimePlanner.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/features/TimePlanner.kt @@ -96,7 +96,7 @@ object TimePlanner { this.components.addAll(ButtonUtil.getTimePlannerButtons().components) } } - then = then.plusDays(1).withHour(20).withMinute(24).withSecond(0) + then = then.plusDays(1).withHour(4).withMinute(0).withSecond(0) Logger.out("Finished sending day $it") delay(1000) } From 6d99022bda1e755732968f60a7cdbd255748b77e Mon Sep 17 00:00:00 2001 From: limited_dev Date: Mon, 5 Jun 2023 12:51:46 +0200 Subject: [PATCH 3/4] !fix: removed the possibility of some Extensions running in DMs Signed-off-by: limited_dev --- .../net/moonleay/lilJudd/extensions/FeatureManageExtension.kt | 2 ++ .../kotlin/net/moonleay/lilJudd/extensions/TestExtension.kt | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/main/kotlin/net/moonleay/lilJudd/extensions/FeatureManageExtension.kt b/src/main/kotlin/net/moonleay/lilJudd/extensions/FeatureManageExtension.kt index 43cfbd4..373fcc7 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/extensions/FeatureManageExtension.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/extensions/FeatureManageExtension.kt @@ -42,6 +42,8 @@ import org.jetbrains.exposed.sql.transactions.transaction class FeatureManageExtension : Extension() { override val name = "feature" + override val allowApplicationCommandInDMs: Boolean + get() = false /* * Note: This has to be rewritten at some point to better support more features diff --git a/src/main/kotlin/net/moonleay/lilJudd/extensions/TestExtension.kt b/src/main/kotlin/net/moonleay/lilJudd/extensions/TestExtension.kt index c6d44c3..049b2e5 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/extensions/TestExtension.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/extensions/TestExtension.kt @@ -30,6 +30,9 @@ import net.moonleay.lilJudd.util.MessageUtil It is used in testing to test stuff, without having to wait for certain events to trigger. */ class TestExtension : Extension() { override val name = "test" + override val allowApplicationCommandInDMs: Boolean + get() = false + override suspend fun setup() { publicSlashCommand { name = "test" From 414c48708f76f82a071999d1c3ee27f9fa7d84c9 Mon Sep 17 00:00:00 2001 From: limited_dev Date: Mon, 5 Jun 2023 13:22:22 +0200 Subject: [PATCH 4/4] feat: added permission check to SendPlannerExtension Signed-off-by: limited_dev --- .../lilJudd/extensions/SendPlannerExtension.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/net/moonleay/lilJudd/extensions/SendPlannerExtension.kt b/src/main/kotlin/net/moonleay/lilJudd/extensions/SendPlannerExtension.kt index ff926d5..4d3defb 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/extensions/SendPlannerExtension.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/extensions/SendPlannerExtension.kt @@ -23,7 +23,9 @@ 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 com.kotlindiscord.kord.extensions.utils.hasPermission import dev.kord.common.Color +import dev.kord.common.entity.Permission import dev.kord.core.behavior.channel.createMessage import dev.kord.rest.builder.message.create.actionRow import kotlinx.coroutines.delay @@ -46,13 +48,19 @@ class SendPlannerExtension : Extension() { name = "sendplanner" description = "Send the planner for the current and x next weeks" this.action { + if (this.arguments.weeks == 0 || !this.member!!.asMember(this.guild!!.id) + .hasPermission(Permission.Administrator) + ) { + val res = this.respond { + this.content = "no." + } + res.delete() + return@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)