discord-jda / JDA

Java wrapper for the popular chat & VOIP service: Discord https://discord.com
Apache License 2.0
4.28k stars 737 forks source link

Can't register Guild command #2644

Closed BalthasarBH closed 5 months ago

BalthasarBH commented 5 months ago

General Troubleshooting

Version of JDA

5.0.0-beta.21

Expected Behaviour

Register command for a special guild

Code Example for Reproduction Steps

Guild guild = jda.getGuildById("1225815657883373578");
        guild.updateCommands().addCommands(Commands.slash("test", "Ja ist halt ein Test näh?"));

Code for JDABuilder or DefaultShardManagerBuilder used

JDA jda = JDABuilder.createDefault("Why")

Exception or Error

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "net.dv8tion.jda.api.entities.Guild.updateCommands()" because "guild" is null
    at de.balthasarbh.discordbot.DiscordBot.main(DiscordBot.java:21)
MCausc78 commented 5 months ago

guild is null. You're probably using jda when it has empty cache. If you will do jda.awaitReady() it will block and wait until JDA populates cache. Then jda.getGuildById(id) wont return null. Also you forgot .queue() on CommandListUpdateAction :) More info: https://docs.jda.wiki/net/dv8tion/jda/api/entities/Guild.html#updateCommands()