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 missing features relating to premium app subscriptions #2667

Merged
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
46fe370
Add missing features relating to premium app subscriptions
Tobias123567 Apr 27, 2024
d456ca7
Add missing documentations
Tobias123567 Apr 27, 2024
a8291b4
Add range check for percent off
Tobias123567 Apr 27, 2024
5310011
Remove Store endpoints as they are discontinued
Tobias123567 Apr 27, 2024
dbbb12e
Remove sku retrieval
Tobias123567 May 4, 2024
fab28ca
Add string id overrides
Tobias123567 May 4, 2024
034a514
Add missing nullability annotation
Tobias123567 May 4, 2024
cef74d9
Remove payment data from entitlements
Tobias123567 May 4, 2024
49034ad
Return TestEntitlementCreateAction instead of void
Tobias123567 May 4, 2024
42c45c8
Do not use import of implementation class
Tobias123567 May 4, 2024
621742e
Remove UNKNOWN OwnerType
Tobias123567 May 4, 2024
e8206d0
Add documentation for OwnerType
Tobias123567 May 4, 2024
ac314cb
Rename wasConsumed method to isConsumed
Tobias123567 May 4, 2024
d123470
Do not throw exception instead return completed rest action
Tobias123567 May 4, 2024
c8d50d6
Do not change the entitlement state
Tobias123567 May 4, 2024
aaddf0f
Add copyright headers
Tobias123567 May 4, 2024
245cc83
Fix coding style
Tobias123567 May 5, 2024
244458e
Add nonnull checks
Tobias123567 May 5, 2024
5b92b07
Use Long.unsignedLong instead of String.valueOf
Tobias123567 May 5, 2024
a5b52c6
Fix route names
Tobias123567 May 5, 2024
d4f6c58
Fix docs
Tobias123567 May 5, 2024
c08f4bb
Add missing string id overload
Tobias123567 May 5, 2024
0aeba3d
Use constructor values
Tobias123567 May 5, 2024
6522797
Fix docs
Tobias123567 May 6, 2024
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
108 changes: 108 additions & 0 deletions src/main/java/net/dv8tion/jda/api/JDA.java
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,114 @@ default List<RichCustomEmoji> getEmojisByName(@Nonnull String name, boolean igno
@CheckReturnValue
EntitlementPaginationAction retrieveEntitlements();

/**
* Retrieves an {@link Entitlement} by its id.
*
* @param entitlementId
* The id of the entitlement to retrieve
*
* @throws IllegalArgumentException
* If the provided id is not a valid snowflake
*
* @return {@link RestAction} - Type: {@link Entitlement}
* <br>The entitlement with the provided id
*/
@Nonnull
@CheckReturnValue
default RestAction<Entitlement> retrieveEntitlementById(@Nonnull String entitlementId)
{
return retrieveEntitlementById(MiscUtil.parseSnowflake(entitlementId));
}

/**
* Retrieves an {@link Entitlement} by its id.
*
* @param entitlementId
* The id of the entitlement to retrieve
*
* @return {@link RestAction} - Type: {@link Entitlement}
* <br>The entitlement with the provided id
*/
@Nonnull
@CheckReturnValue
RestAction<Entitlement> retrieveEntitlementById(long entitlementId);

/**
* Constructs a new {@link Entitlement Entitlement} with the skuId and the type.
* <br>Use the returned {@link TestEntitlementCreateAction TestEntitlementCreateAction} to provide more details.
*
* @param skuId
* The id of the SKU the entitlement is for
*
* @param ownerId
* The id of the owner of the entitlement
*
* @param ownerType
* The type of the owner of the entitlement
*
* @throws IllegalArgumentException
* If the provided skuId or ownerId is not a valid snowflake
*
* @return {@link TestEntitlementCreateAction TestEntitlementCreateAction}
* <br>Allows for setting various details for the resulting Entitlement
*/
@Nonnull
@CheckReturnValue
default TestEntitlementCreateAction createTestEntitlement(@Nonnull String skuId, @Nonnull String ownerId, @Nonnull TestEntitlementCreateAction.OwnerType ownerType)
{
return createTestEntitlement(MiscUtil.parseSnowflake(skuId), MiscUtil.parseSnowflake(ownerId), ownerType);
}

/**
* Constructs a new {@link Entitlement Entitlement} with the skuId and the type.
* <br>Use the returned {@link TestEntitlementCreateAction TestEntitlementCreateAction} to provide more details.
*
* @param skuId
* The id of the SKU the entitlement is for
*
* @param ownerId
* The id of the owner of the entitlement
*
* @param ownerType
* The type of the owner of the entitlement
*
* @return {@link TestEntitlementCreateAction TestEntitlementCreateAction}
* <br>Allows for setting various details for the resulting Entitlement
*/
@Nonnull
@CheckReturnValue
TestEntitlementCreateAction createTestEntitlement(long skuId, long ownerId, @Nonnull TestEntitlementCreateAction.OwnerType ownerType);
Tobias123567 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Deletes a test entitlement by its id.
*
* @param entitlementId
* The id of the entitlement to delete
*
* @throws IllegalArgumentException
* If the provided id is not a valid snowflake
*
* @return {@link RestAction} - Type: Void
*/
@Nonnull
@CheckReturnValue
default RestAction<Void> deleteTestEntitlement(@Nonnull String entitlementId)
{
return deleteTestEntitlement(MiscUtil.parseSnowflake(entitlementId));
}

/**
* Deletes a test entitlement by its id.
*
* @param entitlementId
* The id of the entitlement to delete
*
* @return {@link RestAction} - Type: Void
*/
@Nonnull
@CheckReturnValue
RestAction<Void> deleteTestEntitlement(long entitlementId);

/**
* Configures the required scopes applied to the {@link #getInviteUrl(Permission...)} and similar methods.
* <br>To use slash commands you must add {@code "applications.commands"} to these scopes. The scope {@code "bot"} is always applied.
Expand Down
54 changes: 52 additions & 2 deletions src/main/java/net/dv8tion/jda/api/entities/Entitlement.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package net.dv8tion.jda.api.entities;

import net.dv8tion.jda.api.requests.RestAction;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.OffsetDateTime;
Expand Down Expand Up @@ -104,8 +107,6 @@ default String getGuildId()

/**
* The type of the Entitlement
* <br>The only possible type of Entitlement currently is {@link EntitlementType#APPLICATION_SUBSCRIPTION}
* <br>Discord doesn't currently support other types for entitlements.
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
*
* @return the {@link Entitlement Entitlement} type
*/
Expand Down Expand Up @@ -137,11 +138,60 @@ default String getGuildId()
@Nullable
OffsetDateTime getTimeEnding();

/**
* Whether the {@link Entitlement Entitlement} was consumed or not.
*
* @return True if the {@link Entitlement Entitlement} was consumed, False otherwise
*/
boolean isConsumed();

/**
* Consumes the {@link Entitlement Entitlement} if it has not already been consumed.
Tobias123567 marked this conversation as resolved.
Show resolved Hide resolved
* <br>Only One-Time Purchase consumable {@link Entitlement Entitlement}s can be consumed.
Tobias123567 marked this conversation as resolved.
Show resolved Hide resolved
* <br>After the {@link Entitlement Entitlement} has been consumed, it will be marked as consumed.
*
* @return A {@link RestAction} that will consume the {@link Entitlement Entitlement}
*/
@Nonnull
@CheckReturnValue
RestAction<Void> consume();

/**
* Represents the type of this Entitlement
*/
enum EntitlementType
{
/**
* Entitlement was purchased by user
*/
PURCHASE(1),
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
/**
* Entitlement for Discord Nitro subscription
*/
PREMIUM_SUBSCRIPTION(2),
/**
* Entitlement was gifted by developer
*/
DEVELOPER_GIFT(3),
/**
* Entitlement was purchased by a dev in application test mode
*/
TEST_MODE_PURCHASE(4),
/**
* Entitlement was granted when the SKU was free
*/
FREE_PURCHASE(5),
/**
* Entitlement was gifted by another user
*/
USER_GIFT(6),
/**
* Entitlement was claimed by user for free as a Nitro Subscriber
*/
PREMIUM_PURCHASE(7),
/**
* Entitlement was purchased as an app subscription
*/
APPLICATION_SUBSCRIPTION(8),
/**
* Placeholder for unsupported types.
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/dv8tion/jda/api/requests/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public static class Applications
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_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");
public static final Route CREATE_TEST_ENTITLEMENT = new Route(POST, "applications/{application_id}/entitlements");
public static final Route DELETE_TEST_ENTITLEMENT = new Route(DELETE, "applications/{application_id}/entitlements/{entitlement_id}");
}

public static class Interactions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* 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.requests.restaction;
Tobias123567 marked this conversation as resolved.
Show resolved Hide resolved

import net.dv8tion.jda.api.entities.Entitlement;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.MiscUtil;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;

/**
* Extension of {@link net.dv8tion.jda.api.requests.RestAction RestAction} specifically
* designed to create a {@link Entitlement Entitlement}.
* This extension allows setting properties before executing the action.
*
* @see net.dv8tion.jda.api.JDA
* @see net.dv8tion.jda.api.JDA#createTestEntitlement(long, long, OwnerType)
*/
public interface TestEntitlementCreateAction extends RestAction<Entitlement>
{

/**
* Set the SKU's id to create the entitlement in
*
* @param skuId
* The id of the SKU
*
* @throws IllegalArgumentException
* If the provided skuId is not a valid snowflake
Tobias123567 marked this conversation as resolved.
Show resolved Hide resolved
*
* @return The current {@link TestEntitlementCreateAction} for chaining convenience
*/
@CheckReturnValue
@Nonnull
default TestEntitlementCreateAction setSkuId(@Nonnull String skuId)
{
return setSkuId(MiscUtil.parseSnowflake(skuId));
}

/**
* Set the SKU's id to create the entitlement in
*
* @param skuId
* The id of the SKU
*
* @return The current {@link TestEntitlementCreateAction} for chaining convenience
*/
@CheckReturnValue
@Nonnull
TestEntitlementCreateAction setSkuId(long skuId);

/**
* Set the owner's id to create the entitlement for
*
* @param ownerId
* The id of the owner - either guild id or user id
*
* @throws IllegalArgumentException
* If the provided ownerId is not a valid snowflake
*
* @return The current {@link TestEntitlementCreateAction} for chaining convenience
*/
@CheckReturnValue
@Nonnull
default TestEntitlementCreateAction setOwnerId(@Nonnull String ownerId)
{
return setOwnerId(MiscUtil.parseSnowflake(ownerId));
}

/**
* Set the owner's id to create the entitlement for
*
* @param ownerId
* The id of the owner - either guild id or user id
*
* @return The current {@link TestEntitlementCreateAction} for chaining convenience
*/
@CheckReturnValue
@Nonnull
TestEntitlementCreateAction setOwnerId(long ownerId);

/**
* Set the owner type to create the entitlement for
*
* @param type
* The type of the owner
*
* @return The current {@link TestEntitlementCreateAction} for chaining convenience
*/
@CheckReturnValue
@Nonnull
TestEntitlementCreateAction setOwnerType(@Nonnull OwnerType type);

/**
* The type of the owner for the entitlement
*/
enum OwnerType
Tobias123567 marked this conversation as resolved.
Show resolved Hide resolved
{
GUILD_SUBSCRIPTION(1),
USER_SUBSCRIPTION(2);

private final int key;

OwnerType(int key)
{
this.key = key;
}

/**
* The Discord defined id key for this OwnerType.
*
* @return the id key.
*/
public int getKey()
{
return key;
}
}
}
34 changes: 26 additions & 8 deletions src/main/java/net/dv8tion/jda/internal/JDAImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@
import net.dv8tion.jda.api.managers.AudioManager;
import net.dv8tion.jda.api.managers.Presence;
import net.dv8tion.jda.api.requests.*;
import net.dv8tion.jda.api.requests.restaction.CacheRestAction;
import net.dv8tion.jda.api.requests.restaction.CommandCreateAction;
import net.dv8tion.jda.api.requests.restaction.CommandEditAction;
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction;
import net.dv8tion.jda.api.requests.restaction.*;
import net.dv8tion.jda.api.requests.restaction.pagination.EntitlementPaginationAction;
import net.dv8tion.jda.api.sharding.ShardManager;
import net.dv8tion.jda.api.utils.*;
Expand All @@ -72,10 +69,7 @@
import net.dv8tion.jda.internal.managers.DirectAudioControllerImpl;
import net.dv8tion.jda.internal.managers.PresenceImpl;
import net.dv8tion.jda.internal.requests.*;
import net.dv8tion.jda.internal.requests.restaction.CommandCreateActionImpl;
import net.dv8tion.jda.internal.requests.restaction.CommandEditActionImpl;
import net.dv8tion.jda.internal.requests.restaction.CommandListUpdateActionImpl;
import net.dv8tion.jda.internal.requests.restaction.GuildActionImpl;
import net.dv8tion.jda.internal.requests.restaction.*;
import net.dv8tion.jda.internal.requests.restaction.pagination.EntitlementPaginationActionImpl;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.*;
Expand Down Expand Up @@ -1173,6 +1167,30 @@ public EntitlementPaginationAction retrieveEntitlements()
return new EntitlementPaginationActionImpl(this);
}

@Nonnull
@Override
public RestAction<Entitlement> retrieveEntitlementById(long entitlementId)
{
return new RestActionImpl<>(this, Route.Applications.GET_ENTITLEMENT.compile(getSelfUser().getApplicationId(), Long.toUnsignedString(entitlementId)));
}

@Nonnull
@Override
public TestEntitlementCreateAction createTestEntitlement(long skuId, long ownerId, @Nonnull TestEntitlementCreateActionImpl.OwnerType ownerType)
Tobias123567 marked this conversation as resolved.
Show resolved Hide resolved
{
Checks.notNull(ownerType, "ownerType");
Tobias123567 marked this conversation as resolved.
Show resolved Hide resolved

return new TestEntitlementCreateActionImpl(this, skuId, ownerId, ownerType);
}

@Nonnull
@Override
public RestAction<Void> deleteTestEntitlement(long entitlementId)
{
Route.CompiledRoute route = Route.Applications.DELETE_TEST_ENTITLEMENT.compile(getSelfUser().getApplicationId(), Long.toUnsignedString(entitlementId));
return new RestActionImpl<>(this, route);
}

@Nonnull
@Override
public JDA setRequiredScopes(@Nonnull Collection<String> scopes)
Expand Down
Loading