Skip to content
Open
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
14 changes: 14 additions & 0 deletions src/main/java/net/dv8tion/jda/api/JDA.java
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,20 @@ default RestAction<ApplicationEmoji> retrieveApplicationEmojiById(long emojiId)
@CheckReturnValue
RestAction<ApplicationInfo> retrieveApplicationInfo();

/**
* Retrieves all {@link SKU SKUs} for
* the application that owns the logged in Bot-Account.
*
* <br>Because of how SKUs and subscription systems work, you will see two SKUs for a subscription offering.
* For integration and testing entitlements for Subscriptions, you should use the SKU with type: {@link net.dv8tion.jda.api.entities.SKUType#SUBSCRIPTION}.
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link List of SKUs}
* <br>The {@link SKU SKUs} of the bot's application.
*/
@Nonnull
@CheckReturnValue
RestAction<List<SKU>> retrieveSKUList();

/**
* A {@link net.dv8tion.jda.api.requests.restaction.pagination.PaginationAction PaginationAction} implementation
* which allows you to {@link Iterable iterate} over {@link Entitlement}s that are applicable to the logged in application.
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/SKU.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.entities;

import javax.annotation.Nonnull;
import java.util.Set;

/**
* SKUs (stock-keeping units) in Discord represent premium offerings that can be made available to application users or guilds.
*/
public interface SKU extends SkuSnowflake
{
/**
* Type of the SKU
* @return SKU type
*/
@Nonnull
SKUType getType();

/**
* Customer-facing name of your premium offering
* @return SKU name
*/
@Nonnull
String getName();

/**
* System-generated URL slug based on the SKU's name
* @return SKU slug
*/
@Nonnull
String getSlug();

/**
* Flags can be used to differentiate user and server subscriptions
* @return set of flags.
*/
@Nonnull
Set<SKUFlag> getFlags();

/**
* Checks whether the SKU is available to be purchased for a guild.
* @return true if it's for guilds
*/
default boolean isGuildSKU()
{
return getFlags().contains(SKUFlag.GUILD_SUBSCRIPTION);
}

/**
* Checks whether the SKU is available to be purchased for a user.
* @return true if it's for users
*/
default boolean isUserSKU()
{
return getFlags().contains(SKUFlag.USER_SUBSCRIPTION);
}

/**
* Checks whether this SKU is available.
* @return true if available
*/
default boolean isAvailable()
{
return getFlags().contains(SKUFlag.AVAILABLE);
}
}
63 changes: 63 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/SKUFlag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.entities;

import javax.annotation.Nonnull;
import java.util.EnumSet;

public enum SKUFlag
{
/**
* SKU is available for purchase
*/
AVAILABLE(2),
/**
* Recurring SKU that can be purchased by a user and applied to a single server. Grants access to every user in that server.
*/
GUILD_SUBSCRIPTION(7),
/**
* Recurring SKU purchased by a user for themselves. Grants access to the purchasing user in every server.
*/
USER_SUBSCRIPTION(8);

private final int offset;
private final int raw;

SKUFlag(int offset)
{
this.offset = offset;
this.raw = 1 << offset;
}

public int getOffset()
{
return offset;
}

@Nonnull
public static EnumSet<SKUFlag> getFlags(int flags)
{
if (flags == 0) return EnumSet.noneOf(SKUFlag.class);

EnumSet<SKUFlag> flagSet = EnumSet.noneOf(SKUFlag.class);
for (SKUFlag flag : SKUFlag.values())
{
if ((flags & flag.raw) == flag.raw) flagSet.add(flag);
}
return flagSet;
}
}
85 changes: 85 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/SKUType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.entities;

import javax.annotation.Nonnull;

/**
* SKU Types represent the type of the offered SKU.
*
* <p>For subscriptions, SKUs will have a type of either {@link #SUBSCRIPTION} or {@link #SUBSCRIPTION_GROUP}.
* For any current implementations, you will want to use the SKU type {@link #SUBSCRIPTION}.
* A {@link #SUBSCRIPTION_GROUP} is automatically created for each SUBSCRIPTION SKU and are not used at this time.
*/
public enum SKUType
{
/**
* Durable one-time purchase
*/
DURABLE(2),
/**
* Consumable one-time purchase
*/
CONSUMABLE(3),
/**
* Represents a recurring subscription
*/
SUBSCRIPTION(5),
/**
* System-generated group for each SUBSCRIPTION SKU created
*/
SUBSCRIPTION_GROUP(6),
/**
* Unknown type.
*/
UNKNOWN(-1);

private final int id;

SKUType(int id)
{
this.id = id;
}

/**
* Parse a SKU type from the id
*
* @param type
* The type id
*
* @return SKU type or {@link #UNKNOWN} if the SKU is not known.
*/
@Nonnull
public static SKUType fromId(int type)
{
for (SKUType value : values())
{
if (value.id == type) return value;
}
return UNKNOWN;
}

/**
* The Discord id of the SKU type
*
* @return The discord id
*/
public int getId()
{
return id;
}
}
1 change: 1 addition & 0 deletions src/main/java/net/dv8tion/jda/api/requests/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static class Applications
public static final Route GET_BOT_APPLICATION = new Route(GET, "oauth2/applications/@me");
public static final Route GET_ROLE_CONNECTION_METADATA = new Route(GET, "applications/{application_id}/role-connections/metadata");
public static final Route UPDATE_ROLE_CONNECTION_METADATA = new Route(PUT, "applications/{application_id}/role-connections/metadata");
public static final Route GET_SKUS = new Route(GET, "applications/{application_id}/skus");
public static final Route GET_ENTITLEMENTS = new Route(GET, "applications/{application_id}/entitlements");
public static final Route GET_ENTITLEMENT = new Route(GET, "applications/{application_id}/entitlements/{entitlement_id}");
public static final Route CONSUME_ENTITLEMENT = new Route(POST, "applications/{application_id}/entitlements/{entitlement_id}/consume");
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/JDAImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import net.dv8tion.jda.internal.utils.config.ThreadingConfig;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import org.jetbrains.annotations.Unmodifiable;
import org.slf4j.Logger;
import org.slf4j.MDC;

Expand Down Expand Up @@ -1237,6 +1238,19 @@ public RestAction<ApplicationInfo> retrieveApplicationInfo()
});
}

@Nonnull
@Override
public RestAction<@Unmodifiable List<SKU>> retrieveSKUList()
{
Route.CompiledRoute route = Route.Applications.GET_SKUS.compile(getSelfUser().getApplicationId());
return new RestActionImpl<>(this, route,
(response, request) ->
response.getArray()
.stream(DataArray::getObject)
.map(EntityBuilder::createSKU)
.collect(Helpers.toUnmodifiableList()));
}

@Nonnull
@Override
public EntitlementPaginationAction retrieveEntitlements()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ public static EmojiUnion createEmoji(DataObject emoji, String nameKey, String id
return new CustomEmojiImpl(emoji.getString(nameKey, ""), id, emoji.getBoolean("animated"));
}

public static SKU createSKU(DataObject object)
{
return new SKUImpl(object.getLong("id"),
SKUType.fromId(object.getInt("type")),
object.getString("name"),
object.getString("slug"),
SKUFlag.getFlags(object.getInt("flags")));
}

private void createGuildEmojiPass(GuildImpl guildObj, DataArray array)
{
if (!getJDA().isCacheFlagSet(CacheFlag.EMOJI))
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/entities/SKUImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.internal.entities;

import net.dv8tion.jda.api.entities.SKU;
import net.dv8tion.jda.api.entities.SKUFlag;
import net.dv8tion.jda.api.entities.SKUType;

import javax.annotation.Nonnull;
import java.util.Set;

public class SKUImpl extends SkuSnowflakeImpl implements SKU
{
private final SKUType type;
private final String name;
private final String slug;
private final Set<SKUFlag> flags;

public SKUImpl(long id, SKUType type, String name, String slug, Set<SKUFlag> flags)
{
super(id);
this.type = type;
this.name = name;
this.slug = slug;
this.flags = flags;
}

@Override
@Nonnull
public SKUType getType()
{
return type;
}

@Override
@Nonnull
public String getName()
{
return name;
}

@Override
@Nonnull
public String getSlug()
{
return slug;
}

@Override
@Nonnull
public Set<SKUFlag> getFlags()
{
return flags;
}
}