user20810537
user20810537

Reputation:

Bancommand discord api (JDA5) gives an error

I'm in the process of programming a Discord bot with JDA. At the moment I'm in the process of programming a ban command. If I enter on the Discord server: /ban @user (reason) (deletedays) it works quite well but if I only enter: /ban @user (reason) or /ban @user I get an error. I'm not sure where this error can come from. I hope someone can help me with this. Many Thanks.

Main.java


public class main {
    public static void main(String[] args) throws LoginException, InterruptedException {
        String token = "MYTOKEN";

        JDA jda = JDABuilder.createDefault(
                        token,
                        GatewayIntent.MESSAGE_CONTENT,
                        GatewayIntent.DIRECT_MESSAGES,
                        GatewayIntent.GUILD_MEMBERS,
                        GatewayIntent.GUILD_MESSAGES,
                        GatewayIntent.GUILD_MESSAGE_REACTIONS,
                        GatewayIntent.GUILD_VOICE_STATES,
                        GatewayIntent.GUILD_BANS
                )

                .setActivity(Activity.playing("mit Sally!"))
                .setStatus(OnlineStatus.ONLINE)
                //EventListeners
                .addEventListeners(new BanCommand())
                .build().awaitReady();

        System.out.println("======START======");

        Guild server = jda.getGuildById("GuildID");
        server.updateCommands().addCommands(
                Commands.slash("ban", "Ban a user.")
                        .addOption(OptionType.USER, "user", "User who should be banned.", true)
                        .addOption(OptionType.STRING, "reason", "Reason why the user get banned.")
                        .addOption(OptionType.INTEGER, "deldays", "Delete the messages from the user.", false)
        ).queue();


    }
}

BanCommand.java

public class BanCommand extends ListenerAdapter {

    public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
        if (event.getName().equals("ban")) {
            Member member = event.getOption("user").getAsMember();
            String reason = event.getOption("reason").getAsString();
            int deldays = event.getOption("deldays").getAsInt();

            if (!member.hasPermission(Permission.ADMINISTRATOR)) {
                member.ban(deldays, TimeUnit.DAYS).reason("reason").queue();
                event.reply("You banned " + member.getEffectiveName() + "! "  + "Reason: " + reason).queue();
            } else {
                event.reply("You can't ban an Admin!").queue();
            }
        }
    }
}

I've been trying to solve this problem for a few hours but just can't figure it out.

ERROR

[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.NullPointerException
    at de.ogbot.Commands.Admin.BanCommand.onSlashCommandInteraction(BanCommand.java:14)
    at net.dv8tion.jda.api.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:424)
    at net.dv8tion.jda.api.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:96)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handleInternally(EventManagerProxy.java:88)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handle(EventManagerProxy.java:70)
    at net.dv8tion.jda.internal.JDAImpl.handleEvent(JDAImpl.java:171)
    at net.dv8tion.jda.internal.handle.InteractionCreateHandler.handleCommand(InteractionCreateHandler.java:112)
    at net.dv8tion.jda.internal.handle.InteractionCreateHandler.handleInternally(InteractionCreateHandler.java:83)
    at net.dv8tion.jda.internal.handle.SocketHandler.handle(SocketHandler.java:39)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onDispatch(WebSocketClient.java:984)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:870)
    at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:848)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:1023)
    at com.neovisionaries.ws.client.ListenerManager.callOnBinaryMessage(ListenerManager.java:385)
    at com.neovisionaries.ws.client.ReadingThread.callOnBinaryMessage(ReadingThread.java:276)
    at com.neovisionaries.ws.client.ReadingThread.handleBinaryFrame(ReadingThread.java:996)
    at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:755)
    at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
    at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
    at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)

I tried to give "user", "reason", "deldays" other names, I tried to output the text in an embed instead of reply and I created a new bot and copied the stuff in there, but that didn't work either. That was actually all.

Upvotes: 0

Views: 226

Answers (1)

Minn
Minn

Reputation: 6131

The getOption(name) method will return null when the option is not supplied. You need to properly check that instead of simply calling getAsString() or similar on it, since that is not possible on null.

You can also use getOption("reason", OptionMapping::getAsString) to avoid the null check, but then it will simply return null for the string.

Lastly, you can provide a fallback value to be used in the case that the option is not supplied:

String reason = event.getOption("reason", "No reason specified", OptionMapping::getAsString);

See the documentation for details:

Upvotes: 1

Related Questions