From 67aaf7f35068cbe2e2b6d8cd6e29b528079d70cf Mon Sep 17 00:00:00 2001 From: moonleay Date: Sun, 21 May 2023 15:07:36 +0200 Subject: [PATCH] feat: added button support Signed-off-by: limited_dev --- src/main/kotlin/net/moonleay/lilJudd/Bot.kt | 52 ++++++++++++++++--- .../lilJudd/buttons/component/EditButton.kt | 38 ++++++++++++++ .../buttons/component/EditButtonManager.kt | 31 +++++++++++ 3 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/net/moonleay/lilJudd/buttons/component/EditButton.kt create mode 100644 src/main/kotlin/net/moonleay/lilJudd/buttons/component/EditButtonManager.kt diff --git a/src/main/kotlin/net/moonleay/lilJudd/Bot.kt b/src/main/kotlin/net/moonleay/lilJudd/Bot.kt index ec55b96..727943a 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/Bot.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/Bot.kt @@ -19,16 +19,22 @@ package net.moonleay.lilJudd import com.kotlindiscord.kord.extensions.ExtensibleBot +import dev.kord.common.Color import dev.kord.common.entity.PresenceStatus +import dev.kord.core.behavior.interaction.response.respond +import dev.kord.core.event.interaction.ButtonInteractionCreateEvent +import dev.kord.core.on import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import net.moonleay.botendo.build.BuildConstants +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.VersionExtension import net.moonleay.lilJudd.features.TimePlanner import net.moonleay.lilJudd.util.Logger +import net.moonleay.lilJudd.util.MessageUtil import kotlin.system.exitProcess object Bot { @@ -37,22 +43,22 @@ object Bot { suspend fun start() { Logger.out("Starting Bot...") - //Load config + // Load config CredentialManager.load() - //Don't run the bot when there is no bot token in config + // Don't run the bot when there is no bot token in config if (CredentialManager.token == "empty") { Logger.out("The config does not contain a bot token.") exitProcess(3) } - //Check if the credentials for the Database are existent + // Check if the credentials for the Database are existent if (CredentialManager.dbDomain == "empty" || CredentialManager.dbName == "empty" || CredentialManager.dbUser == "empty" || CredentialManager.dbPassword == "empty") { Logger.out("The config does not contain the whole Database credentials.") exitProcess(3) } - //Connect to the database + // Connect to the database DB.connect( CredentialManager.dbDomain, CredentialManager.dbName, @@ -60,7 +66,7 @@ object Bot { CredentialManager.dbPassword ) - //Register the TimePlanner thread + // Register the TimePlanner thread val coroutineJob = GlobalScope.launch { TimePlanner.registerThread() } @@ -71,7 +77,7 @@ object Bot { }) - //Add Bot token to kord + // Add bot token to kord bot = ExtensibleBot(CredentialManager.token) { applicationCommands { enabled = true @@ -80,6 +86,7 @@ object Bot { extensions { add(::VersionExtension) add(::FeatureManageExtension) + //add(::TestExtension) // See comment in TestExtension.kt } this.presence { @@ -88,6 +95,39 @@ object Bot { } } + // Register button presses + bot.kordRef.on { + val inter = this.interaction + val u = inter.user + Logger.out("Button interaction: ${inter.componentId} fron ${u.asUser().username}#${u.asUser().discriminator}") + if (inter.componentId.startsWith("public.edit.")) { + val response = inter.deferPublicMessageUpdate() + val g = this.interaction.getOriginalInteractionResponse().getGuild() + for (b in EditButtonManager.buttons) { + if (b.id != inter.componentId) + continue + b.onInteraction(inter, response, g, u) + return@on + } + return@on + } + if (inter.componentId.startsWith("public.message.")) { + val response = inter.deferPublicResponse() + val g = this.interaction.getOriginalInteractionResponse().getGuild() + response.respond { + this.embeds = mutableListOf( + MessageUtil.getEmbed( + Color(0xE0311A), + "Error", + "Could not find button with id \"${inter.componentId}\".\nPlease report this.", + u.asUser().username + "#" + u.asUser().discriminator + ) + ) + } + } + } + + //Start the bot bot.start() } diff --git a/src/main/kotlin/net/moonleay/lilJudd/buttons/component/EditButton.kt b/src/main/kotlin/net/moonleay/lilJudd/buttons/component/EditButton.kt new file mode 100644 index 0000000..45b2611 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/buttons/component/EditButton.kt @@ -0,0 +1,38 @@ +/* + * 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.component + +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.interaction.ButtonInteraction + +open class EditButton(val id: String) { + + open suspend fun onInteraction( + interaction: ButtonInteraction, + response: PublicMessageInteractionResponseBehavior, + guild: Guild, + user: User + ) { + + } +} + + diff --git a/src/main/kotlin/net/moonleay/lilJudd/buttons/component/EditButtonManager.kt b/src/main/kotlin/net/moonleay/lilJudd/buttons/component/EditButtonManager.kt new file mode 100644 index 0000000..7f253f7 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/buttons/component/EditButtonManager.kt @@ -0,0 +1,31 @@ +/* + * 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.component + +import net.moonleay.lilJudd.buttons.timeplanner.IsAvailableEditButton +import net.moonleay.lilJudd.buttons.timeplanner.MaybeAvailableEditButton +import net.moonleay.lilJudd.buttons.timeplanner.NotAvailableEditButton + +object EditButtonManager { + val buttons = listOf( + IsAvailableEditButton(), + MaybeAvailableEditButton(), + NotAvailableEditButton() + ) +}