Skip to content
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

Add SlashCommandEvent.getCommandString #1693

Conversation

VixenKasai
Copy link
Contributor

@VixenKasai VixenKasai commented Jun 27, 2021

(Updated Files from last PR, branch corrupted)

Pull Request Etiquette

Changes

  • Internal code
  • Library interface (affecting end-user code)
  • Documentation
  • Other: _____

Closes Issue: NaN

Description

Adds SlashCommandEvent#getCommandString() which will provide the text the user typed in to activate the command:

Ex:
/say phrase: Hello World! mention: @ Johnny

(Updated Files from last PR, branch corrupted)
@MinnDevelopment MinnDevelopment added this to the v4.3.1 milestone Jul 4, 2021
@DV8FromTheWorld
Copy link
Member

I really wish this had some tests around it

@VixenKasai
Copy link
Contributor Author

VixenKasai commented Aug 4, 2021

I really wish this had some tests around it

https://hastebin.com/enozukomah.typescript

unless I'm misunderstanding what you're wanting; could you then clarify?

@DV8FromTheWorld
Copy link
Member

Please post your snippets here on Github because hastebin will eventually time out and we will lose that code.

This line seems like it's wrong
/command4 option1: opt1 option2: 100 option3: true option4: #chat option5: @867602586257195028 option6: @Vixen V2 option7: @Chop
Namely the mentionable output. Is that how it shows up in the client?

@VixenKasai
Copy link
Contributor Author

VixenKasai commented Aug 4, 2021

package me.vixen.testing;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.*;
import org.jetbrains.annotations.NotNull;

import javax.security.auth.login.LoginException;

public class Entry extends ListenerAdapter {
    public static void main(String[] args) throws LoginException, InterruptedException {
        JDA jda = JDABuilder.createLight(token)
            .addEventListeners(new Entry())
            .build()
            .awaitReady();
        jda.getGuildById("761703507546996786").updateCommands().addCommands(
            new CommandData("command1", "command1").addSubcommandGroups( //name
                new SubcommandGroupData("subgroup1", "subgroup1").addSubcommands( //subcommand group
                    new SubcommandData("subcommand1", "subcommand1").addOptions( //subcommand
                        new OptionData(OptionType.STRING, "option1", "Option 1", true), //option1
                        new OptionData(OptionType.STRING, "option2", "Option 2", true) //option2
                    )
                )
            ),
            new CommandData("command2", "Command 2").addSubcommands( //name
                new SubcommandData("subcommand2", "sub command 2").addOptions( //subcommand
                    new OptionData(OptionType.STRING, "option1", "Option 1", true), //option1
                    new OptionData(OptionType.STRING, "option2", "Option 2", true) //option2
                )
            ),
            new CommandData("command3", "Command 3").addOptions( //name
                new OptionData(OptionType.STRING, "option1", "Option 1", true), //option1
                new OptionData(OptionType.STRING, "option2", "Option 2", true) //option2
            ),
            new CommandData("command4", "Command 4").addOptions( //name
                new OptionData(OptionType.STRING, "option1", "Option 1", true), //option1
                new OptionData(OptionType.INTEGER, "option2", "Option 2", true), //option2
                new OptionData(OptionType.BOOLEAN, "option3", "Option 3", true), //option2
                new OptionData(OptionType.CHANNEL, "option4", "Option 4", true), //option2
                new OptionData(OptionType.MENTIONABLE, "option5", "Option 5", true), //option2
                new OptionData(OptionType.ROLE, "option6", "Option 6", true), //option2
                new OptionData(OptionType.USER, "option7", "Option 7", true) //option2
            ),
            new CommandData("command5", "command 5") //name
        ).queue();
    }

    @Override
    public void onReady(@NotNull ReadyEvent event) {
        System.out.println("JDA Ready");
    }

    @Override
    public void onSlashCommand(@NotNull SlashCommandEvent event) {
        System.out.println(event.getCommandString());
        event.reply("reply").queue();
    }
}

//TEST INPUTS

/command1 subgroup1 subcommand1 option1: opt1 option2: opt2
/command2 subcommand2 option1: opt1 option2: opt2
/command3 option1: opt1 option2: opt2
/command4 option1: opt1 option2: 100 option3: true option4: #chat option5: @Vixen option6: @Vixen V2 option7: @Chop
/command5

// OUTPUTS

/command1 subgroup1 subcommand1 option1: opt1 option2: opt2
/command2 subcommand2 option1: opt1 option2: opt2
/command3 option1: opt1 option2: opt2
/command4 option1: opt1 option2: 100 option3: true option4: #chat option5: @867602586257195028 option6: @Vixen V2 option7: @Chop
/command5

@VixenKasai
Copy link
Contributor Author

VixenKasai commented Aug 4, 2021

Please post your snippets here on Github because hastebin will eventually time out and we will lose that code.

This line seems like it's wrong
/command4 option1: opt1 option2: 100 option3: true option4: #chat option5: @867602586257195028 option6: @Vixen V2 option7: @Chop
Namely the mentionable output. Is that how it shows up in the client?

The 'input' section is how it appears on the client.

Through some more testing, (unless I'm missing something), It does not seem that a MENTIONABLE option will ever be an instanceof something else. It always seems to just to the default else of builder.append("@").append(o.getAsMentionable().getIdLong());

o.getAsRole() or o.getAsUser() will always result in an error throw due to being of OptionType.MENTIONABLE

Any ideas? Unless the default should be o.getAsMentionable().getAsMention()? It may be as accurate as we can get? (Again unless I'm missing something key)

Did some more testing, realized that the MENTIONABLES are of the Impl types and will allow exact recreation of the strings on the client, repeated the same tests (+ one for role in the mentionable slot) and had exact matches.

See updated files below

@VixenKasai
Copy link
Contributor Author

New Comparison

Co-authored-by: Florian Spieß <business@minnced.club>
@VixenKasai VixenKasai force-pushed the feature/SlashCommandEventGetCommandText branch from 5f0e86a to f35deec Compare August 11, 2021 02:17
@DV8FromTheWorld DV8FromTheWorld merged commit 84f2cc3 into discord-jda:development Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants