This commit is contained in:
31
src/main/kotlin/fr/lucasdupont/command/Command.kt
Normal file
31
src/main/kotlin/fr/lucasdupont/command/Command.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
package fr.lucasdupont.gaytabot.command
|
||||
|
||||
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.OptionData
|
||||
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData
|
||||
|
||||
interface Command {
|
||||
|
||||
val name: String
|
||||
val description: String
|
||||
|
||||
val options: List<OptionData>
|
||||
get() = emptyList()
|
||||
|
||||
val neededPermissions: DefaultMemberPermissions?
|
||||
get() = null
|
||||
|
||||
val subcommands: List<Command>
|
||||
get() = emptyList()
|
||||
|
||||
val subcommandGroups: List<SubCommandGroup>
|
||||
get() = emptyList()
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun execute(event: SlashCommandInteractionEvent)
|
||||
|
||||
fun toSubcommandData(): SubcommandData =
|
||||
SubcommandData(name, description).addOptions(options)
|
||||
|
||||
}
|
||||
23
src/main/kotlin/fr/lucasdupont/command/CommandMapper.kt
Normal file
23
src/main/kotlin/fr/lucasdupont/command/CommandMapper.kt
Normal file
@@ -0,0 +1,23 @@
|
||||
package fr.lucasdupont.gaytabot.command
|
||||
|
||||
import net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData
|
||||
import net.dv8tion.jda.internal.interactions.CommandDataImpl
|
||||
|
||||
fun Command.toCommandData(): CommandDataImpl = CommandDataImpl(name, description).apply {
|
||||
addOptions(options)
|
||||
neededPermissions?.let { setDefaultPermissions(it) }
|
||||
|
||||
if (subcommands.isNotEmpty()) {
|
||||
addSubcommands(subcommands.map { toSubcommandData() })
|
||||
}
|
||||
|
||||
if (subcommandGroups.isNotEmpty()) {
|
||||
addSubcommandGroups(
|
||||
subcommandGroups.map { group ->
|
||||
SubcommandGroupData(
|
||||
group.name,
|
||||
group.description
|
||||
).addSubcommands(group.subcommands.map { toSubcommandData() })
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package fr.lucasdupont.gaytabot.command
|
||||
|
||||
data class SubCommandGroup(
|
||||
val name: String,
|
||||
val description: String,
|
||||
val subCommands: List<Command>
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
package fr.lucasdupont.configuration
|
||||
|
||||
import net.dv8tion.jda.api.OnlineStatus
|
||||
import net.dv8tion.jda.api.utils.cache.CacheFlag
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties
|
||||
|
||||
@ConfigurationProperties("jda")
|
||||
class JdaConfiguration {
|
||||
var token: String? = null
|
||||
var onlineStatus: OnlineStatus = OnlineStatus.ONLINE
|
||||
var cacheFlags: Set<CacheFlag> = emptySet()
|
||||
var activity: String? = null
|
||||
}
|
||||
75
src/main/kotlin/fr/lucasdupont/service/JdaService.kt
Normal file
75
src/main/kotlin/fr/lucasdupont/service/JdaService.kt
Normal file
@@ -0,0 +1,75 @@
|
||||
package fr.lucasdupont.service
|
||||
|
||||
import fr.lucasdupont.configuration.JdaConfiguration
|
||||
import fr.lucasdupont.gaytabot.command.Command
|
||||
import fr.lucasdupont.gaytabot.command.toCommandData
|
||||
import net.dv8tion.jda.api.JDA
|
||||
import net.dv8tion.jda.api.JDABuilder
|
||||
import net.dv8tion.jda.api.entities.Activity
|
||||
import net.dv8tion.jda.api.events.GenericEvent
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent
|
||||
import net.dv8tion.jda.api.utils.cache.CacheFlag
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.context.ApplicationContext
|
||||
import org.springframework.context.ApplicationEventPublisher
|
||||
import org.springframework.context.PayloadApplicationEvent
|
||||
import org.springframework.context.event.EventListener
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
@EnableConfigurationProperties(JdaConfiguration::class)
|
||||
class JdaService(
|
||||
applicationContext: ApplicationContext,
|
||||
jdaConfiguration: JdaConfiguration,
|
||||
publisher: ApplicationEventPublisher
|
||||
) {
|
||||
|
||||
private final val jda: JDA
|
||||
private final val commands: Map<String, Command>
|
||||
|
||||
init {
|
||||
val requiredIntents: Set<GatewayIntent> =
|
||||
jdaConfiguration.cacheFlags
|
||||
.mapNotNull(CacheFlag::getRequiredIntent)
|
||||
.toSet()
|
||||
|
||||
val builder = JDABuilder.createDefault(jdaConfiguration.token)
|
||||
.setStatus(jdaConfiguration.onlineStatus)
|
||||
.enableCache(jdaConfiguration.cacheFlags)
|
||||
.enableIntents(requiredIntents)
|
||||
if (jdaConfiguration.activity != null) {
|
||||
builder.setActivity(Activity.playing(jdaConfiguration.activity!!))
|
||||
}
|
||||
|
||||
jda = builder
|
||||
.build()
|
||||
.awaitReady()
|
||||
.also { jda ->
|
||||
jda.addEventListener(object : ListenerAdapter() {
|
||||
override fun onGenericEvent(event: GenericEvent) {
|
||||
publisher.publishEvent(PayloadApplicationEvent(jda, event))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
val contextCommands = applicationContext.getBeansOfType(Command::class.java).values
|
||||
commands = contextCommands.associateBy { it.name }
|
||||
|
||||
val commandDataList = contextCommands.map { it.toCommandData() }
|
||||
|
||||
jda.updateCommands()
|
||||
.addCommands(commandDataList)
|
||||
.queue()
|
||||
}
|
||||
|
||||
@EventListener(SlashCommandInteractionEvent::class)
|
||||
fun onSlashCommand(event: SlashCommandInteractionEvent) {
|
||||
commands[event.name]?.let { command ->
|
||||
runCatching { command.execute(event) }
|
||||
.onFailure { e -> error("Error while executing command '${event.name}', $e") }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user