Skip to content

Commit

Permalink
Merge pull request #19 from Goksi/subcommands
Browse files Browse the repository at this point in the history
Adding support for subcommands
  • Loading branch information
Goksi authored Jan 27, 2023
2 parents 3cd4bc7 + 0928905 commit 47cdaa3
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 92 deletions.
21 changes: 11 additions & 10 deletions src/main/kotlin/tech/goksi/pterobot/PteroBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,17 @@ class PteroBot {
}
}.awaitReady()

val commandData = SimpleCommandData()
commandData.addCommands(
Link(),
NodeInfoCmd(),
Register(),
Servers(jda),
Unlink(),
NodeStatusCmd()
)
val guild = jda.getGuildById(guildPair.first!!) // what if wrong guild id ?
val commandData = SimpleCommandData().apply {
addCommands(
Link(),
NodeInfoCmd(),
Register(),
Servers(jda),
Unlink(),
NodeStatusCmd()
)
}
val guild = jda.getGuildById(guildPair.first!!) // what if wrong guild id, silent fail for now ?
guild?.updateCommands()?.addCommands(commandData.buildData())?.queue()
jda.addEventListener(NodeStatusDelete())
commandData.registerListeners(jda)
Expand Down
23 changes: 12 additions & 11 deletions src/main/kotlin/tech/goksi/pterobot/commands/Link.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ import java.sql.SQLException

private const val CONFIG_PREFIX = "Messages.Commands.Link."

class Link : SimpleCommand() {
class Link : SimpleCommand(
name = "link",
description = ConfigManager.config.getString(CONFIG_PREFIX + "Description"),
options = listOf(
OptionData(
OptionType.STRING,
"apikey",
ConfigManager.config.getString(CONFIG_PREFIX + "OptionDescription"),
true
)
)
) {
private val logger by SLF4J

init {
this.name = "link"
this.description = ConfigManager.config.getString(CONFIG_PREFIX + "Description")
this.options = listOf(
OptionData(
OptionType.STRING,
"apikey",
ConfigManager.config.getString(CONFIG_PREFIX + "OptionDescription"),
true
)
)
SendDefaults.ephemeral = true
}

Expand Down
42 changes: 20 additions & 22 deletions src/main/kotlin/tech/goksi/pterobot/commands/NodeInfoCmd.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,32 @@ import kotlin.concurrent.fixedRateTimer

private const val CONFIG_PREFIX = "Messages.Commands.NodeInfo."

class NodeInfoCmd : SimpleCommand() {
/*TODO: probably different coroutine scope and error handling*/
class NodeInfoCmd : SimpleCommand(
name = "nodeinfo",
description = ConfigManager.config.getString(CONFIG_PREFIX + "Description"),
enabledPermissions = listOf(Permission.ADMINISTRATOR),
options = listOf(
OptionData(
OptionType.INTEGER,
"id",
ConfigManager.config.getString(CONFIG_PREFIX + "OptionDescription"),
true
),
OptionData(
OptionType.BOOLEAN,
"update",
ConfigManager.config.getString(CONFIG_PREFIX + "OptionUpdateDescription"),
false
)
)
) {
private val logger by SLF4J

companion object TaskMapping {
val mapping: MutableMap<Long, Timer> = HashMap() // message id and timer
}

init {
this.name = "nodeinfo"
this.description = ConfigManager.config.getString(CONFIG_PREFIX + "Description")
this.enableDefault = false
this.enabledPermissions = listOf(Permission.ADMINISTRATOR)
this.options = listOf(
OptionData(
OptionType.INTEGER,
"id",
ConfigManager.config.getString(CONFIG_PREFIX + "OptionDescription"),
true
),
OptionData(
OptionType.BOOLEAN,
"update",
ConfigManager.config.getString(CONFIG_PREFIX + "OptionUpdateDescription"),
false
)
)
}

override suspend fun execute(event: SlashCommandInteractionEvent) {
event.deferReply().queue()
val nodeId = event.getOption("id")!!.asInt
Expand Down
29 changes: 13 additions & 16 deletions src/main/kotlin/tech/goksi/pterobot/commands/NodeStatusCmd.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,24 @@ import kotlin.concurrent.fixedRateTimer

private const val CONFIG_PREFIX = "Messages.Commands.NodeStatus."

class NodeStatusCmd : SimpleCommand() {
class NodeStatusCmd : SimpleCommand(
name = "nodestatus",
description = ConfigManager.config.getString(CONFIG_PREFIX + "Description"),
enabledPermissions = listOf(Permission.ADMINISTRATOR),
options = listOf(
OptionData(
OptionType.BOOLEAN,
"update",
ConfigManager.config.getString(CONFIG_PREFIX + "OptionUpdateDescription"),
false
)
)
) {

companion object TaskMapping {
val mapping: MutableMap<Long, Timer> = HashMap()
}

init {
this.name = "nodestatus"
this.description = ConfigManager.config.getString(CONFIG_PREFIX + "Description")
this.enableDefault = false
this.enabledPermissions = listOf(Permission.ADMINISTRATOR)
this.options = listOf(
OptionData(
OptionType.BOOLEAN,
"update",
ConfigManager.config.getString(CONFIG_PREFIX + "OptionUpdateDescription"),
false
)
)
}

override suspend fun execute(event: SlashCommandInteractionEvent) {
val pteroMember = PteroMember(event.member!!)
if (event.member!!.hasPermission(Permission.ADMINISTRATOR) || pteroMember.isPteroAdmin()) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/tech/goksi/pterobot/commands/Register.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import tech.goksi.pterobot.util.Common
private const val CONFIG_PREFIX = "Messages.Commands.Register."

/*TODO: account deletion*/
/*TODO: maybe commands rework etc /account delete /account link...*/
class Register : SimpleCommand() {
/*TODO: maybe commands rework etc /account delete /account link... (in progress)*/
class Register : SimpleCommand(
name = "register",
description = ConfigManager.config.getString(CONFIG_PREFIX + "Description"),
enabledPermissions = listOf(Permission.ADMINISTRATOR)
) {
private val logger by SLF4J
private val modal: Modal

init {
this.name = "register"
this.description = ConfigManager.config.getString(CONFIG_PREFIX + "Description")
this.enableDefault = false
this.enabledPermissions = listOf(Permission.ADMINISTRATOR)
SendDefaults.ephemeral = true
}

Expand Down
10 changes: 4 additions & 6 deletions src/main/kotlin/tech/goksi/pterobot/commands/Servers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ private const val CONFIG_PREFIX = "Messages.Commands.Servers."
private const val SELECTION_ID = "pterobot:servers-selector"

/*TODO single event for every close btn*/
class Servers(jda: JDA) : SimpleCommand() {
class Servers(jda: JDA) : SimpleCommand(
name = "servers",
description = ConfigManager.config.getString(CONFIG_PREFIX + "Description")
) {
private val logger by SLF4J
private val serverMapping: MutableMap<String, ClientServer> = HashMap()

init {
this.name = "servers"
this.description = ConfigManager.config.getString(CONFIG_PREFIX + "Description")
}

/*LISTENER FOR COMMAND*/
init {
jda.listener<ModalInteractionEvent>(timeout = 2.minutes) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/tech/goksi/pterobot/commands/Unlink.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import tech.goksi.pterobot.manager.EmbedManager.toEmbed

private const val CONFIG_PREFIX = "Messages.Commands.Unlink."

class Unlink : SimpleCommand() {
class Unlink : SimpleCommand(
name = "unlink",
description = ConfigManager.config.getString(CONFIG_PREFIX + "Description")
) {

init {
this.name = "unlink"
this.description = ConfigManager.config.getString(CONFIG_PREFIX + "Description")
SendDefaults.ephemeral = true
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package tech.goksi.pterobot.commands.manager

import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions
import net.dv8tion.jda.api.interactions.commands.build.Commands
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData
import tech.goksi.pterobot.commands.manager.abs.SimpleCommand
import tech.goksi.pterobot.util.Checks

class SimpleCommandData {
private val commands: MutableList<SimpleCommand>

init {
commands = ArrayList()
}
Expand All @@ -20,14 +18,7 @@ class SimpleCommandData {
fun buildData(): List<SlashCommandData> {
val data: MutableList<SlashCommandData> = ArrayList()
for (command in this.commands) {
Checks.arguments(command.options.size <= 25, "Slash command can have max 25 options !")
val cmdData = Commands.slash(command.name, command.description)
cmdData.isGuildOnly = true
if (!command.enableDefault) {
cmdData.defaultPermissions = DefaultMemberPermissions.enabledFor(command.enabledPermissions)
}
if (command.options.isNotEmpty()) cmdData.addOptions(command.options)
data.add(cmdData)
data.add(command.buildCommand())
}
return data
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,53 @@ package tech.goksi.pterobot.commands.manager.abs

import net.dv8tion.jda.api.Permission
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions
import net.dv8tion.jda.api.interactions.commands.build.Commands
import net.dv8tion.jda.api.interactions.commands.build.OptionData
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData
import tech.goksi.pterobot.events.hook.CoroutineListenerAdapter
import tech.goksi.pterobot.util.Checks

abstract class SimpleCommand : CoroutineListenerAdapter() {
lateinit var name: String
lateinit var description: String
var options: List<OptionData> = emptyList()
var enabledPermissions: List<Permission> = emptyList()
var enableDefault: Boolean = true
abstract class SimpleCommand(
val name: String,
val description: String,
private val subcommands: List<SimpleCommand> = emptyList(),
private val options: List<OptionData> = emptyList(),
private val enabledPermissions: List<Permission> = emptyList()
) : CoroutineListenerAdapter() {

abstract suspend fun execute(event: SlashCommandInteractionEvent)

override suspend fun onSlashCommandInteraction(event: SlashCommandInteractionEvent) {
if (name == event.name) {
execute(event)
if (subcommands.isEmpty()) execute(event)
else {
val subcommand = subcommands.filter { it.name == event.subcommandName }[0]
subcommand.execute(event)
}
}
}

fun buildCommand(): SlashCommandData {
val commandData = Commands.slash(name, description)
commandData.isGuildOnly = true
if (enabledPermissions.isNotEmpty()) {
commandData.defaultPermissions = DefaultMemberPermissions.enabledFor(enabledPermissions)
}
if (options.isNotEmpty()) {
Checks.arguments(options.size <= 25, "Slash command can have max 25 options !")
commandData.addOptions(options)
} else { // slash command can't have both subcommands and options
for (subcommand in subcommands) {
val subcommandData = SubcommandData(subcommand.name, subcommand.description)
if (subcommand.options.isNotEmpty()) {
Checks.arguments(subcommand.options.size <= 25, "Slash command can have max 25 options !")
subcommandData.addOptions(subcommand.options)
}
commandData.addSubcommands(subcommandData)
}
}
return commandData
}
}

0 comments on commit 47cdaa3

Please sign in to comment.