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 all 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,8 +16,11 @@

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.entities.MemberImpl;
import net.dv8tion.jda.internal.entities.RoleImpl;
import net.dv8tion.jda.internal.entities.UserImpl;
import net.dv8tion.jda.internal.utils.Checks;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -200,4 +203,54 @@ 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(": ");
switch (o.getType())
{
case CHANNEL:
builder.append("#").append(o.getAsGuildChannel().getName());
break;
case USER:
builder.append("@").append(o.getAsUser().getName());
break;
case ROLE:
builder.append("@").append(o.getAsRole().getName());
break;
case MENTIONABLE: //client only allows user or role mentionable as of Aug 4, 2021
if (o.getAsMentionable() instanceof Role)
builder.append("@").append(o.getAsRole().getName());
else if (o.getAsMentionable() instanceof Member)
builder.append("@").append(o.getAsUser().getName());
else if (o.getAsMentionable() instanceof User)
builder.append("@").append(o.getAsUser().getName());
else
builder.append("@").append(o.getAsMentionable().getIdLong());
break;
default:
VixenKasai marked this conversation as resolved.
Show resolved Hide resolved
builder.append(o.getAsString());
break;
}
}
return builder.toString();
}
}