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

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ public List<OptionMapping> getOptions()
{
return commandInteraction.getOptions();
}

@Nonnull
@Override
public String getCommandString()
{
return commandInteraction.getCommandString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package net.dv8tion.jda.api.interactions.commands;

import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.interactions.Interaction;
import net.dv8tion.jda.internal.utils.Checks;

Expand Down Expand Up @@ -200,4 +200,52 @@ default OptionMapping getOption(@Nonnull String name)
List<OptionMapping> options = getOptionsByName(name);
return options.isEmpty() ? null : options.get(0);
}

/**
* Gets the slash command String for this slash command.
* <br>This is similar to the String you see when clicking the interaction name in the client.
*
* <p>Example return for an echo command: {@code /say echo phrase: Say this}
*
* @return The command String for this slash command
*/
@Nonnull
default String getCommandString()
{
//Get text like the text that appears when you hover over the interaction in discord
StringBuilder builder = new StringBuilder();
builder.append("/").append(getName());
if (getSubcommandGroup() != null)
builder.append(" ").append(getSubcommandGroup());
if (getSubcommandName() != null)
builder.append(" ").append(getSubcommandName());
for (OptionMapping o : getOptions())
{
builder.append(" ").append(o.getName()).append(":");
VixenKasai marked this conversation as resolved.
Show resolved Hide resolved
switch (o.getType())
{
case CHANNEL:
builder.append(" ").append("#").append(o.getAsGuildChannel().getName());
break;
case USER:
builder.append(" ").append("@").append(o.getAsUser().getName());
break;
case ROLE:
builder.append(" ").append("@").append(o.getAsRole().getName());
break;
case MENTIONABLE: //client only allows user or role mentionable
if (o instanceof Role)
builder.append(" ").append("@").append(o.getAsRole().getName());
else if (o instanceof User)
builder.append(" ").append("@").append(o.getAsUser().getName());
else
builder.append(" ").append("@").append(o.getAsMentionable().getIdLong());
break;
default:
VixenKasai marked this conversation as resolved.
Show resolved Hide resolved
builder.append(" ").append(o.getAsString());
break;
}
}
return builder.toString();
}
}