discordcommanddiscord4j

How to register Slash Command in Discord4j core 3.2.3?


I am trying to implement slash commands to my discord bot using discord4j core but I cant find a working way to register the commands.

I looked up the example at the discord4j page. They use

GuildCommandRegistrar.create(client.getRestClient(), guildId, Collections.singletonList(randomCommand))
                .registerCommands()
                .doOnError(e -> log.warn("Unable to create guild command", e))
                .onErrorResume(e -> Mono.empty())
                .blockLast();

But I can't get this to work for me. In the example project they use this import

import discord4j.core.support.GuildCommandRegistrar;

But I can't use this for myself. Does anyone know how I can register my Slash command with discord4j?

Thanks in advance


Solution

  • One of the maintainers here, The .support... classpath is our test/demo classes.

    Our example projects show a better source to reference for how we recommend registering global slash commands.

    With vanilla java: https://github.com/Discord4J/example-projects/blob/master/gradle-simple-bot/src/main/java/com/novamaday/d4j/gradle/simplebot/GlobalCommandRegistrar.java

    With Spring Boot: https://github.com/Discord4J/example-projects/blob/master/gradle-spring-bot/src/main/java/com/novamaday/d4j/gradle/springbot/GlobalCommandRegistrar.java

    A real world example with Spring in Kotlin from my own bot: https://github.com/DreamExposure/TicketBird-Discord-Bot/blob/master/src/main/kotlin/org/dreamexposure/ticketbird/service/GlobalCommandRegistrar.kt