javadiscord / java-discord-api

A wrapper over the discord API to create bots using Java
GNU General Public License v3.0
7 stars 8 forks source link

Mega PR; Slash Commands, Rate limits, Code Clean up and more! #148

Closed surajkumar closed 4 months ago

surajkumar commented 4 months ago

closes #149 closes #28 closes #123 closes #110 closes #111

This PR a start on handling slash commands.

Implemented so far:

Example slash command:

package com.javadiscord.jdi.example;

import com.javadiscord.jdi.core.CommandOptionType;
import com.javadiscord.jdi.core.Guild;
import com.javadiscord.jdi.core.annotations.CommandOption;
import com.javadiscord.jdi.core.annotations.CommandOptionChoice;
import com.javadiscord.jdi.core.annotations.SlashCommand;
import com.javadiscord.jdi.core.interaction.SlashCommandEvent;
import com.javadiscord.jdi.core.models.application.ApplicationCommandOption;

public class ExampleSlashCommand {

    @SlashCommand(
        name = "quiz", description = "A fun Java quiz", options = {
            @CommandOption(
                name = "q1", description = "What is an Integer?", type = CommandOptionType.STRING, choices = {
                    @CommandOptionChoice(
                        name = "option1", value = "An object that represents a number"
                    ),
                    @CommandOptionChoice(name = "option2", value = "A class used to store objects"),
                    @CommandOptionChoice(name = "option3", value = "A primitive data type")
                }
            ),
            @CommandOption(
                name = "q2", description = "In which package is the List interface defined?", type = CommandOptionType.STRING, choices = {
                    @CommandOptionChoice(name = "option1", value = "java.util"),
                    @CommandOptionChoice(name = "option2", value = "java.lang"),
                    @CommandOptionChoice(name = "option3", value = "java.io")
                }
            ),
            @CommandOption(
                name = "q3", description = "What does JVM stand for?", type = CommandOptionType.STRING, choices = {
                    @CommandOptionChoice(name = "option1", value = "Java Virtual Machine"),
                    @CommandOptionChoice(name = "option2", value = "Java Verified Module"),
                    @CommandOptionChoice(name = "option3", value = "Java Variable Method")
                }
            ),
            @CommandOption(
                name = "q4", description = "Is a String a primitive data type?", type = CommandOptionType.STRING, choices = {
                    @CommandOptionChoice(name = "option1", value = "Yes"),
                    @CommandOptionChoice(name = "option2", value = "No")
                }
            ),
            @CommandOption(
                name = "q5", description = "Which of the following is not a Java keyword?", type = CommandOptionType.STRING, choices = {
                    @CommandOptionChoice(name = "option1", value = "static"),
                    @CommandOptionChoice(name = "option2", value = "void"),
                    @CommandOptionChoice(name = "option3", value = "main"),
                    @CommandOptionChoice(name = "option4", value = "private")
                }
            )
        }
    )

    public void handle(SlashCommandEvent event, Guild guild) {
        ApplicationCommandOption[] options = event.options();
        for (ApplicationCommandOption option : options) {
            System.out.println("Received " + option.name() + " value " + option.valueAsString());
        }
    }
}

or for a simple command with no inputs:

    @SlashCommand(name = "ping", description = "pong!" )
    public void handle(SlashCommandEvent event) {
        // ...
    }
surajkumar commented 4 months ago

@javadiscord/maintainers can I have some comments? <3

SquidXTV commented 4 months ago

@javadiscord/maintainers can I have some comments? <3

+4000 💀 Thanks going to check when I got time

sonarcloud[bot] commented 4 months ago

Quality Gate Passed Quality Gate passed

Issues
2 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud