feat: initial commit
Some checks failed
Release / release (push) Failing after 1m51s

This commit is contained in:
lucasdpt
2025-11-02 21:02:40 +01:00
commit c99eb92c97
9 changed files with 320 additions and 0 deletions

View 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)
}

View 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() })
})
}
}

View File

@@ -0,0 +1,7 @@
package fr.lucasdupont.gaytabot.command
data class SubCommandGroup(
val name: String,
val description: String,
val subCommands: List<Command>
)

View File

@@ -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
}

View 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") }
}
}
}