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 package net.moonleay.lilJudd
import com.kotlindiscord.kord.extensions.ExtensibleBot import com.kotlindiscord.kord.extensions.ExtensibleBot
import dev.kord.common.Color
import dev.kord.common.entity.PresenceStatus 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.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.moonleay.botendo.build.BuildConstants import net.moonleay.botendo.build.BuildConstants
import net.moonleay.lilJudd.buttons.component.EditButtonManager
import net.moonleay.lilJudd.data.CredentialManager import net.moonleay.lilJudd.data.CredentialManager
import net.moonleay.lilJudd.data.DB import net.moonleay.lilJudd.data.DB
import net.moonleay.lilJudd.extensions.FeatureManageExtension import net.moonleay.lilJudd.extensions.FeatureManageExtension
import net.moonleay.lilJudd.extensions.VersionExtension import net.moonleay.lilJudd.extensions.VersionExtension
import net.moonleay.lilJudd.features.TimePlanner import net.moonleay.lilJudd.features.TimePlanner
import net.moonleay.lilJudd.util.Logger import net.moonleay.lilJudd.util.Logger
import net.moonleay.lilJudd.util.MessageUtil
import kotlin.system.exitProcess import kotlin.system.exitProcess
object Bot { object Bot {
@ -37,22 +43,22 @@ object Bot {
suspend fun start() { suspend fun start() {
Logger.out("Starting Bot...") Logger.out("Starting Bot...")
//Load config // Load config
CredentialManager.load() 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") { if (CredentialManager.token == "empty") {
Logger.out("The config does not contain a bot token.") Logger.out("The config does not contain a bot token.")
exitProcess(3) 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") { if (CredentialManager.dbDomain == "empty" || CredentialManager.dbName == "empty" || CredentialManager.dbUser == "empty" || CredentialManager.dbPassword == "empty") {
Logger.out("The config does not contain the whole Database credentials.") Logger.out("The config does not contain the whole Database credentials.")
exitProcess(3) exitProcess(3)
} }
//Connect to the database // Connect to the database
DB.connect( DB.connect(
CredentialManager.dbDomain, CredentialManager.dbDomain,
CredentialManager.dbName, CredentialManager.dbName,
@ -60,7 +66,7 @@ object Bot {
CredentialManager.dbPassword CredentialManager.dbPassword
) )
//Register the TimePlanner thread // Register the TimePlanner thread
val coroutineJob = GlobalScope.launch { val coroutineJob = GlobalScope.launch {
TimePlanner.registerThread() TimePlanner.registerThread()
} }
@ -71,7 +77,7 @@ object Bot {
}) })
//Add Bot token to kord // Add bot token to kord
bot = ExtensibleBot(CredentialManager.token) { bot = ExtensibleBot(CredentialManager.token) {
applicationCommands { applicationCommands {
enabled = true enabled = true
@ -80,6 +86,7 @@ object Bot {
extensions { extensions {
add(::VersionExtension) add(::VersionExtension)
add(::FeatureManageExtension) add(::FeatureManageExtension)
//add(::TestExtension) // See comment in TestExtension.kt
} }
this.presence { 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 //Start the bot
bot.start() 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()
)
}