Skip to content

Commit

Permalink
Add CommandInteractionPayload#isGuildCommand (#2091)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Apr 18, 2022
1 parent d0ca572 commit cd57b4c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
Expand Up @@ -99,6 +99,12 @@ public long getCommandIdLong()
return interaction.getCommandIdLong();
}

@Override
public boolean isGuildCommand()
{
return interaction.isGuildCommand();
}

@Nonnull
@Override
public List<OptionMapping> getOptions()
Expand Down
Expand Up @@ -83,6 +83,12 @@ public long getCommandIdLong()
return getInteraction().getCommandIdLong();
}

@Override
public boolean isGuildCommand()
{
return getInteraction().isGuildCommand();
}

@Nonnull
@Override
public List<OptionMapping> getOptions()
Expand Down
Expand Up @@ -16,11 +16,13 @@

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

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.interactions.Interaction;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.internal.utils.Checks;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -200,6 +202,27 @@ default String getCommandId()
return Long.toUnsignedString(getCommandIdLong());
}

/**
* Whether the used command is a guild command.
*
* <p>Guild commands can be created with {@link Guild#upsertCommand(CommandData)}.
*
* @return True, if the used command is a guild command
*/
boolean isGuildCommand();

/**
* Whether the used command is a global command.
*
* <p>Global commands can be created with {@link JDA#upsertCommand(CommandData)}.
*
* @return True, if the used command is a global command
*/
default boolean isGlobalCommand()
{
return !isGuildCommand();
}

/**
* The options provided by the user when this command was executed.
* <br>Each option has a name and value.
Expand Down
Expand Up @@ -45,6 +45,7 @@ public class CommandInteractionPayloadImpl extends InteractionImpl implements Co
private final List<OptionMapping> options = new ArrayList<>();
private final TLongObjectMap<Object> resolved = new TLongObjectHashMap<>();
private final String name;
private final boolean isGuildCommand;
private String subcommand;
private String group;
private final Command.Type type;
Expand All @@ -56,6 +57,7 @@ public CommandInteractionPayloadImpl(JDAImpl jda, DataObject data)
this.commandId = commandData.getUnsignedLong("id");
this.name = commandData.getString("name");
this.type = Command.Type.fromId(commandData.getInt("type", 1));
this.isGuildCommand = !commandData.isNull("guild_id"); // guild_id is always either null or the owner guild (same as interaction guild_id)

DataArray options = commandData.optArray("options").orElseGet(DataArray::empty);
DataObject resolveJson = commandData.optObject("resolved").orElseGet(DataObject::empty);
Expand Down Expand Up @@ -176,6 +178,12 @@ public long getCommandIdLong()
return commandId;
}

@Override
public boolean isGuildCommand()
{
return isGuildCommand;
}

@Nonnull
@Override
public List<OptionMapping> getOptions()
Expand Down
Expand Up @@ -68,4 +68,10 @@ default List<OptionMapping> getOptions()
{
return getCommandPayload().getOptions();
}

@Override
default boolean isGuildCommand()
{
return getCommandPayload().isGuildCommand();
}
}

0 comments on commit cd57b4c

Please sign in to comment.