feat: added button support

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
moonleay 2023-05-21 15:07:36 +02:00 committed by limited_dev
parent 0e744135c0
commit 67aaf7f350
3 changed files with 115 additions and 6 deletions

View file

@ -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 {
@ -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<ButtonInteractionCreateEvent> {
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()
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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
) {
}
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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()
)
}