Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 59 commits into from
May 30, 2024

Conversation

surajkumar
Copy link
Contributor

@surajkumar surajkumar commented May 26, 2024

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

This PR a start on handling slash commands.

Implemented so far:

  • Annotation based slash command loading
  • Registering a slash command
  • Receiving the event when a slash command is invoked
  • Fixed perf issue with annotation loading, before it was taking several mins and now it takes a few seconds
  • Ported lj-discord-bot over to this into examples

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) {
        // ...
    }

* Fix performance with dynamic loading
* General cleanup
* Added support for embeds in reply
@surajkumar surajkumar marked this pull request as ready for review May 27, 2024 20:02
Copy link

sonarqubecloud bot commented May 30, 2024

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

Copy link

@notyourhabibti notyourhabibti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved <3 We spoke and anything else with cleanups, bug fixes etc that are not captured in this PR should be fixed seperately

@surajkumar surajkumar merged commit 55d793e into main May 30, 2024
4 of 5 checks passed
@surajkumar surajkumar deleted the slash-commands branch May 30, 2024 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants