Skip to content

Commit

Permalink
created tools command
Browse files Browse the repository at this point in the history
fixed private commands showing up outside of mainland guild
  • Loading branch information
hechfx committed Apr 26, 2024
1 parent c990cfc commit 37e3354
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dev.minn.jda.ktx.coroutines.await
import hub.nebula.pangea.PangeaInstance
import hub.nebula.pangea.command.structure.PangeaSlashCommandDeclarationWrapper
import hub.nebula.pangea.command.vanilla.admin.declaration.AdminCommand
import hub.nebula.pangea.command.vanilla.dev.declaration.ToolsCommand
import hub.nebula.pangea.command.vanilla.economy.declaration.CurrencyCommand
import hub.nebula.pangea.command.vanilla.misc.declaration.PangeaCommand
import hub.nebula.pangea.command.vanilla.music.declaration.MusicCommand
Expand Down Expand Up @@ -33,18 +34,21 @@ class PangeaCommandManager(private val pangea: PangeaInstance) {
command.create().build()
).await()
logger.info { "Registered /${command.create().name} private command!" }
} else {
action.addCommands(
command.create().build()
)
logger.info { "Registered /${command.create().name} command!" }
}

action.addCommands(
command.create().build()
)
logger.info { "Registered /${command.create().name} command!" }
}

return action.await()
}

init {
// ===[ Developer ]===
register(ToolsCommand())

// ===[ Miscellaneous ]===
register(PangeaCommand())

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package hub.nebula.pangea.command.vanilla.dev

import hub.nebula.pangea.command.PangeaInteractionContext
import hub.nebula.pangea.command.structure.PangeaSlashCommandExecutor
import hub.nebula.pangea.listener.MajorEventListener

class LavalinkForceReconnectCommandExecutor : PangeaSlashCommandExecutor() {
override suspend fun execute(context: PangeaInteractionContext) {
if (context.user.idLong != 236167700777271297L) {
context.reply(true) {
content = "Nope."
}
return
}

context.pangea.lavakord.nodes.forEach {
context.pangea.lavakord.removeNode(it)
}

MajorEventListener.connectLavalinkNode(context.pangea)

context.reply {
content = "Reconnected."
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package hub.nebula.pangea.command.vanilla.dev

import dev.minn.jda.ktx.coroutines.await
import hub.nebula.pangea.command.PangeaInteractionContext
import hub.nebula.pangea.command.structure.PangeaSlashCommandExecutor

class UnregisterAllCommandsCommandExecutor : PangeaSlashCommandExecutor() {
override suspend fun execute(context: PangeaInteractionContext) {
if (context.user.idLong != 236167700777271297L) {
context.reply(true) {
content = "Nope."
}
return
}

val allCommands = context.jda.retrieveCommands().await()

allCommands.forEach {
it.delete().await()
}

context.reply {
content = "Unregistered all ${allCommands.size} commands."
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hub.nebula.pangea.command.vanilla.dev.declaration

import hub.nebula.pangea.command.structure.PangeaSlashCommandDeclarationWrapper
import hub.nebula.pangea.command.vanilla.dev.LavalinkForceReconnectCommandExecutor
import hub.nebula.pangea.command.vanilla.dev.UnregisterAllCommandsCommandExecutor

class ToolsCommand : PangeaSlashCommandDeclarationWrapper {
override fun create() = command("tools", "bot dev tools", isPrivate = true) {
subCommandGroup("lavalink", "lavalink related", this@command.name) {
subCommand("force-reconnect", "reconnect lavalink's node", isPrivate = true) {
executor = LavalinkForceReconnectCommandExecutor()
}
}

subCommandGroup("commands", "commands related", this@command.name) {
subCommand("unregister", "unregister all commands", isPrivate = true) {
executor = UnregisterAllCommandsCommandExecutor()
}
}
}
}
24 changes: 18 additions & 6 deletions src/main/kotlin/hub/nebula/pangea/listener/MajorEventListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent
import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent
import net.dv8tion.jda.api.events.session.ReadyEvent
import net.dv8tion.jda.api.events.session.SessionResumeEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import java.util.concurrent.Executors
Expand All @@ -35,6 +36,18 @@ class MajorEventListener(val pangea: PangeaInstance) : ListenerAdapter() {
val coroutineScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
val logger = KotlinLogging.logger(this::class.jvmName)

companion object {
fun connectLavalinkNode(pangea: PangeaInstance) {
pangea.config.comet.nodes.forEach {
pangea.lavakord.addNode(
"ws://${it.host}:${it.port}",
it.password,
"pangea-lavalink-node"
)
}
}
}

override fun onGenericComponentInteractionCreate(event: GenericComponentInteractionCreateEvent) {
coroutineScope.launch {
if (event.isFromGuild) {
Expand Down Expand Up @@ -249,12 +262,7 @@ class MajorEventListener(val pangea: PangeaInstance) : ListenerAdapter() {

logger.info { "Logging in with ${event.jda.gatewayIntents.size} intents." }

pangea.config.comet.nodes.forEach {
pangea.lavakord.addNode(
"ws://${it.host}:${it.port}",
it.password
)
}
connectLavalinkNode(pangea)

logger.info { "Lavalink node connected." }

Expand All @@ -269,6 +277,10 @@ class MajorEventListener(val pangea: PangeaInstance) : ListenerAdapter() {
}
}

override fun onSessionResume(event: SessionResumeEvent) {
connectLavalinkNode(pangea)
}

private fun updateLavalinkStatus(event: ReadyEvent) {
val lavakord = pangea.lavakord
val channel = event.jda.getGuildById(pangea.config.mainLand.id)!!.getGuildChannelById(pangea.config.mainLand.lavalinkChannel) as TextChannel
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/hub/nebula/pangea/utils/JDAExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ import hub.nebula.pangea.command.PangeaInteractionContext
import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.entities.Message
import net.dv8tion.jda.api.interactions.InteractionHook
import net.dv8tion.jda.api.utils.messages.MessageEditData

fun InlineMessage<*>.pretty(content: String, prefix: String = Emojis.STAR) {
if (content.isNotBlank()) {
this.content = "$prefix **•** $content"
}
}

suspend fun Message.edit(block: InlineMessage<*>.() -> Unit): Message? {
val msg = MessageEditBuilder {
apply(block)
}

return this.editMessage(msg.build()).await()
}

fun prettyStr(content: String, prefix: String = Emojis.STAR): String {
return if (content.isNotBlank()) {
"$prefix **•** $content"
Expand Down

0 comments on commit 37e3354

Please sign in to comment.