Skip to content

Commit

Permalink
Add Command#getVersion (#1741)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xirado committed Sep 5, 2021
1 parent b1b5c8c commit 6ea728b
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.ISnowflake;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.CommandEditAction;
import net.dv8tion.jda.api.utils.TimeUtil;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.api.utils.data.DataType;
Expand All @@ -33,6 +35,7 @@

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
Expand All @@ -58,7 +61,7 @@ public class Command implements ISnowflake
private final List<Option> options;
private final List<SubcommandGroup> groups;
private final List<Subcommand> subcommands;
private final long id, guildId, applicationId;
private final long id, guildId, applicationId, version;
private final boolean defaultEnabled;

public Command(JDAImpl api, Guild guild, DataObject json)
Expand All @@ -74,6 +77,7 @@ public Command(JDAImpl api, Guild guild, DataObject json)
this.options = parseOptions(json, OPTION_TEST, Option::new);
this.groups = parseOptions(json, GROUP_TEST, SubcommandGroup::new);
this.subcommands = parseOptions(json, SUBCOMMAND_TEST, Subcommand::new);
this.version = json.getUnsignedLong("version");
}

protected static <T> List<T> parseOptions(DataObject json, Predicate<DataObject> test, Function<DataObject, T> transform)
Expand Down Expand Up @@ -310,6 +314,33 @@ public String getApplicationId()
return Long.toUnsignedString(applicationId);
}

/**
* The version of this command.
* <br>This changes when a command is updated through {@link net.dv8tion.jda.api.JDA#upsertCommand(CommandData) upsertCommand}, {@link net.dv8tion.jda.api.JDA#updateCommands() updateCommands}, or {@link net.dv8tion.jda.api.JDA#editCommandById(String) editCommandById}
* <br>Useful for checking if command cache is outdated
*
* @return The version of the command as a snowflake id.
*
* @see #getTimeModified()
*/
public long getVersion()
{
return version;
}

/**
* The time this command was updated last.
*
* @return Time this command was updated last.
*
* @see #getVersion()
*/
@Nonnull
public OffsetDateTime getTimeModified()
{
return TimeUtil.getTimeCreated(getVersion());
}

@Override
public long getIdLong()
{
Expand Down

0 comments on commit 6ea728b

Please sign in to comment.