From af79090ca02bca2d93a2192cbfff421c7d52b7cf Mon Sep 17 00:00:00 2001 From: limited_dev Date: Sun, 21 May 2023 15:08:23 +0200 Subject: [PATCH] feat: added buttons for TimePlanner v2 Signed-off-by: limited_dev --- .../timeplanner/IsAvailableEditButton.kt | 64 +++++++++++++ .../timeplanner/MaybeAvailableEditButton.kt | 64 +++++++++++++ .../timeplanner/NotAvailableEditButton.kt | 65 +++++++++++++ .../net/moonleay/lilJudd/util/ButtonUtil.kt | 96 +++++++++++++++++++ .../net/moonleay/lilJudd/util/MessageUtil.kt | 40 +++++++- 5 files changed, 328 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/net/moonleay/lilJudd/buttons/timeplanner/IsAvailableEditButton.kt create mode 100644 src/main/kotlin/net/moonleay/lilJudd/buttons/timeplanner/MaybeAvailableEditButton.kt create mode 100644 src/main/kotlin/net/moonleay/lilJudd/buttons/timeplanner/NotAvailableEditButton.kt create mode 100644 src/main/kotlin/net/moonleay/lilJudd/util/ButtonUtil.kt diff --git a/src/main/kotlin/net/moonleay/lilJudd/buttons/timeplanner/IsAvailableEditButton.kt b/src/main/kotlin/net/moonleay/lilJudd/buttons/timeplanner/IsAvailableEditButton.kt new file mode 100644 index 0000000..aa9f5d6 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/buttons/timeplanner/IsAvailableEditButton.kt @@ -0,0 +1,64 @@ +/* + * 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.buttons.timeplanner + +import dev.kord.core.behavior.edit +import dev.kord.core.behavior.interaction.response.PublicMessageInteractionResponseBehavior +import dev.kord.core.entity.Guild +import dev.kord.core.entity.User +import dev.kord.core.entity.channel.MessageChannel +import dev.kord.core.entity.interaction.ButtonInteraction +import dev.kord.rest.builder.message.modify.embed +import net.moonleay.lilJudd.Bot +import net.moonleay.lilJudd.buttons.component.EditButton +import net.moonleay.lilJudd.util.ButtonUtil + +class IsAvailableEditButton : EditButton("public.edit.btn.timemanagement.available") { + override suspend fun onInteraction( + interaction: ButtonInteraction, + response: PublicMessageInteractionResponseBehavior, + guild: Guild, + user: User + ) { + val m = interaction.message + if (m.embeds[0].fields[0].value.contains(user.id.value.toString())) { + Bot.bot.kordRef.getChannelOf(interaction.channelId)!!.getMessage(m.id).edit { + this.embed { + val temp = ButtonUtil.replaceXWithYinValuesAtTable(user.id.value.toString(), "", m.embeds[0], 1) + this.color = temp.color + this.title = temp.title + this.description = temp.description + this.fields = temp.fields + this.footer = temp.footer + } + } + } else { + Bot.bot.kordRef.getChannelOf(interaction.channelId)!!.getMessage(m.id).edit { + this.embed { + val temp = ButtonUtil.addXToValuesAtTable(user.id.value.toString(), m.embeds[0], 1) + this.color = temp.color + this.title = temp.title + this.description = temp.description + this.fields = temp.fields + this.footer = temp.footer + } + } + } + } +} diff --git a/src/main/kotlin/net/moonleay/lilJudd/buttons/timeplanner/MaybeAvailableEditButton.kt b/src/main/kotlin/net/moonleay/lilJudd/buttons/timeplanner/MaybeAvailableEditButton.kt new file mode 100644 index 0000000..8c4c8ca --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/buttons/timeplanner/MaybeAvailableEditButton.kt @@ -0,0 +1,64 @@ +/* + * 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.buttons.timeplanner + +import dev.kord.core.behavior.edit +import dev.kord.core.behavior.interaction.response.PublicMessageInteractionResponseBehavior +import dev.kord.core.entity.Guild +import dev.kord.core.entity.User +import dev.kord.core.entity.channel.MessageChannel +import dev.kord.core.entity.interaction.ButtonInteraction +import dev.kord.rest.builder.message.modify.embed +import net.moonleay.lilJudd.Bot +import net.moonleay.lilJudd.buttons.component.EditButton +import net.moonleay.lilJudd.util.ButtonUtil + +class MaybeAvailableEditButton : EditButton("public.edit.btn.timemanagement.maybeavailable") { + override suspend fun onInteraction( + interaction: ButtonInteraction, + response: PublicMessageInteractionResponseBehavior, + guild: Guild, + user: User + ) { + val m = interaction.message + if (m.embeds[0].fields[1].value.contains(user.id.value.toString())) { + Bot.bot.kordRef.getChannelOf(interaction.channelId)!!.getMessage(m.id).edit { + this.embed { + val temp = ButtonUtil.replaceXWithYinValuesAtTable(user.id.value.toString(), "", m.embeds[0], 2) + this.color = temp.color + this.title = temp.title + this.description = temp.description + this.fields = temp.fields + this.footer = temp.footer + } + } + } else { + Bot.bot.kordRef.getChannelOf(interaction.channelId)!!.getMessage(m.id).edit { + this.embed { + val temp = ButtonUtil.addXToValuesAtTable(user.id.value.toString(), m.embeds[0], 2) + this.color = temp.color + this.title = temp.title + this.description = temp.description + this.fields = temp.fields + this.footer = temp.footer + } + } + } + } +} diff --git a/src/main/kotlin/net/moonleay/lilJudd/buttons/timeplanner/NotAvailableEditButton.kt b/src/main/kotlin/net/moonleay/lilJudd/buttons/timeplanner/NotAvailableEditButton.kt new file mode 100644 index 0000000..dddb720 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/buttons/timeplanner/NotAvailableEditButton.kt @@ -0,0 +1,65 @@ +/* + * 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.buttons.timeplanner + +import dev.kord.core.behavior.edit +import dev.kord.core.behavior.interaction.response.PublicMessageInteractionResponseBehavior +import dev.kord.core.entity.Guild +import dev.kord.core.entity.User +import dev.kord.core.entity.channel.MessageChannel +import dev.kord.core.entity.interaction.ButtonInteraction +import dev.kord.rest.builder.message.modify.embed +import net.moonleay.lilJudd.Bot +import net.moonleay.lilJudd.buttons.component.EditButton +import net.moonleay.lilJudd.util.ButtonUtil + +class NotAvailableEditButton : EditButton("public.edit.btn.timemanagement.notavailable") { + + override suspend fun onInteraction( + interaction: ButtonInteraction, + response: PublicMessageInteractionResponseBehavior, + guild: Guild, + user: User + ) { + val m = interaction.message + if (m.embeds[0].fields[2].value.contains(user.id.value.toString())) { + Bot.bot.kordRef.getChannelOf(interaction.channelId)!!.getMessage(m.id).edit { + this.embed { + val temp = ButtonUtil.replaceXWithYinValuesAtTable(user.id.value.toString(), "", m.embeds[0], 3) + this.color = temp.color + this.title = temp.title + this.description = temp.description + this.fields = temp.fields + this.footer = temp.footer + } + } + } else { + Bot.bot.kordRef.getChannelOf(interaction.channelId)!!.getMessage(m.id).edit { + this.embed { + val temp = ButtonUtil.addXToValuesAtTable(user.id.value.toString(), m.embeds[0], 3) + this.color = temp.color + this.title = temp.title + this.description = temp.description + this.fields = temp.fields + this.footer = temp.footer + } + } + } + } +} diff --git a/src/main/kotlin/net/moonleay/lilJudd/util/ButtonUtil.kt b/src/main/kotlin/net/moonleay/lilJudd/util/ButtonUtil.kt new file mode 100644 index 0000000..63d1703 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/util/ButtonUtil.kt @@ -0,0 +1,96 @@ +/* + * 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.util + +import dev.kord.common.entity.ButtonStyle +import dev.kord.core.entity.Embed +import dev.kord.rest.builder.component.ActionRowBuilder +import dev.kord.rest.builder.message.EmbedBuilder + +object ButtonUtil { + fun getTimePlannerButtons(): ActionRowBuilder { + val ar = ActionRowBuilder() + ar.interactionButton(ButtonStyle.Success, "public.edit.btn.timemanagement.available") { + this.label = "Available" + } + ar.interactionButton(ButtonStyle.Primary, "public.edit.btn.timemanagement.maybeavailable") { + this.label = "Maybe available" + } + ar.interactionButton(ButtonStyle.Danger, "public.edit.btn.timemanagement.notavailable") { + this.label = "Not available" + } + return ar + } + + /** + * @param x The string to replace + * @param y The string to replace with + * @param e The embed to replace the string in + * @param table The table to replace the string in + * @param maxTableSize The max table size + * @return The embed with the replaced string + */ + fun replaceXWithYinValuesAtTable(x: String, y: String, e: Embed, table: Int): EmbedBuilder { + val ebb = MessageUtil.getAClonedEmbedd(e) + for ((i, f) in e.fields.withIndex()) { + val fb = EmbedBuilder.Field() + fb.name = f.name + if (i == table - 1) { + val v = f.value.split("\n").toMutableList() + for ((i, l) in v.withIndex()) + if (l.contains(x)) + v[i] = "\n${y}" + fb.value = v.joinToString("\n") + } else + fb.value = f.value + fb.inline = true + ebb.fields.add(fb) + } + return ebb + } + + /** + * @param x The string to add + * @param e The embed to add the string to + * @param table The table to add the string to + * @param maxTableSize The max table size + * @return The embed with the added string + */ + fun addXToValuesAtTable(x: String, e: Embed, table: Int): EmbedBuilder { + val ebb = MessageUtil.getAClonedEmbedd(e) + for ((i, f) in e.fields.withIndex()) { + val fb = EmbedBuilder.Field() + fb.name = f.name + if (i == table - 1) + fb.value = f.value + "\n<@$x>" + else { + val v = f.value.split("\n").toMutableList() + for ((j, l) in v.withIndex()) + if (l.contains(x)) + v[j] = "\n" + fb.value = v.joinToString("\n") + } + + fb.inline = true + ebb.fields.add(fb) + } + return ebb + } + +} diff --git a/src/main/kotlin/net/moonleay/lilJudd/util/MessageUtil.kt b/src/main/kotlin/net/moonleay/lilJudd/util/MessageUtil.kt index 0544acb..c5ca2b8 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/util/MessageUtil.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/util/MessageUtil.kt @@ -23,6 +23,7 @@ import com.kotlindiscord.kord.extensions.commands.application.slash.PublicSlashC import com.kotlindiscord.kord.extensions.components.forms.ModalForm import com.kotlindiscord.kord.extensions.types.respond import dev.kord.common.Color +import dev.kord.core.entity.Embed import dev.kord.rest.builder.message.EmbedBuilder import java.time.LocalDateTime import java.time.format.DateTimeFormatter @@ -70,6 +71,43 @@ object MessageUtil { } } + ///Get a cloned embedded message, missing only the fields + fun getAClonedEmbedd(e: Embed): EmbedBuilder { + val ebb = EmbedBuilder() + ebb.color = e.color + ebb.title = e.title + ebb.description = e.description + ebb.footer { + icon = e.footer!!.iconUrl + text = e.footer!!.text + } + return ebb + } + + ///Get an embedded msg with image, title and description + fun getEmbedWithTable( + color: Color, + title: String, + description: String, + source: String, + values: Map>? + ): EmbedBuilder { + val ebb = getEmbed(color, title, description, source) + if (values != null) + for (key in values.keys) { + val fb = EmbedBuilder.Field() + fb.name = key + var s = "" + for (value in values[key]!!) + s += "$value\n" + fb.value = s + fb.inline = true + ebb.fields.add(fb) + } + return ebb + } + + ///Get an embedded msg with title and description fun getEmbedSmall( color: Color, @@ -79,6 +117,7 @@ object MessageUtil { val ebb = EmbedBuilder() ebb.title = title ebb.description = description + ebb.color = color return ebb } @@ -93,7 +132,6 @@ object MessageUtil { val now: LocalDateTime = LocalDateTime.now() ebb.footer = EmbedBuilder.Footer() ebb.footer!!.text = ">" + dtf.format(now) + " - $source" - ebb.color = color return ebb }