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

Implement invite targets #1628

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
170 changes: 161 additions & 9 deletions src/main/java/net/dv8tion/jda/api/entities/Invite.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,19 @@ static RestAction<Invite> resolve(@Nonnull final JDA api, @Nonnull final String
/**
* The type of this invite.
*
* @return The invites's type
* @return The invite's type
*/
@Nonnull
Invite.InviteType getType();

/**
* The target type of this invite.
*
* @return The invite's target type or {@link TargetType#UNKNOWN}
*/
@Nonnull
Invite.TargetType getTargetType();
anweisen marked this conversation as resolved.
Show resolved Hide resolved

/**
* An {@link net.dv8tion.jda.api.entities.Invite.Channel Invite.Channel} object
* containing information about this invite's origin channel.
Expand All @@ -153,14 +161,6 @@ static RestAction<Invite> resolve(@Nonnull final JDA api, @Nonnull final String
@Nullable
Channel getChannel();

/**
* The invite code
*
* @return the invite code
*/
@Nonnull
String getCode();

/**
* An {@link net.dv8tion.jda.api.entities.Invite.Group Invite.Group} object
* containing information about this invite's origin group.
Expand All @@ -172,6 +172,33 @@ static RestAction<Invite> resolve(@Nonnull final JDA api, @Nonnull final String
@Nullable
Group getGroup();

/**
* An {@link net.dv8tion.jda.api.entities.Invite.EmbeddedApplication Invite.EmbeddedApplication} object
* containing information about this invite's application.
*
* @return Information about this invite's application or {@code null} if this invite's {@link #getTargetType() Invite.TargetType} is not {@link Invite.TargetType#EMBEDDED_APPLICATION}
*
* @see net.dv8tion.jda.api.entities.Invite.EmbeddedApplication
*/
@Nullable
EmbeddedApplication getTargetApplication();

/**
* The user to whose stream this invite goes.
*
* @return The user to whose stream this invite goes or {@code null} if this invite's {@link #getTargetType() Invite.TargetType} is not {@link Invite.TargetType#STREAM Invite.TargetType.STREAM}
*/
@Nullable
User getTargetUser();

/**
* The invite code
*
* @return the invite code
*/
@Nonnull
String getCode();

/**
* The invite URL for this invite in the format of:
* {@code "https://discord.gg/" + getCode()}
Expand Down Expand Up @@ -492,6 +519,65 @@ interface Group extends ISnowflake
List<String> getUsers();
}

/**
* POJO for the target application information provided by an invite.
*
* @see #getTargetApplication()
*/
interface EmbeddedApplication extends ISnowflake
{
/**
* The name of this application.
*
* @return The name of this application.
*/
@Nonnull
String getName();

/**
* The description of this application.
*
* @return The description of this application.
*/
@Nonnull
String getDescription();

/**
* The summary of this application or {@code null} if this application has no summary.
*
* @return The summary of this application.
*/
@Nullable
String getSummary();

/**
* The icon id of this application or {@code null} if the application has no icon.
*
* @return The application's icon id
*
* @see #getIconUrl()
*/
@Nullable
String getIconId();

/**
* The icon url of this application or {@code null} if the application has no icon.
*
* @return The application's icon url
*
* @see #getIconId()
*/
@Nullable
String getIconUrl();

/**
* The max participant count of this application or {@code -1} if no max participant count is set
*
* @return {@code -1} if this application does not have a max participant count
*/
int getMaxParticipants();
}

/**
* Enum representing the type of an invite.
*
Expand All @@ -503,4 +589,70 @@ enum InviteType
GROUP,
UNKNOWN
}

/**
* Enum representing the target type of an invite.
anweisen marked this conversation as resolved.
Show resolved Hide resolved
*
* @see #getTargetType()
*/
anweisen marked this conversation as resolved.
Show resolved Hide resolved
enum TargetType
{
/**
* The invite points to an users stream in a voice channel.
anweisen marked this conversation as resolved.
Show resolved Hide resolved
* The user to whose stream the invite goes can be get with {@link Invite#getTargetUser() Invite.getTargetUser} and is not {@code null}.
*
* @see Invite#getTargetUser()
*/
STREAM(1),

/**
* The invite points to an application in a voice channel.
* The application to which the invite goes can be get with {@link Invite#getTargetApplication() Invite.getTargetApplication} and is not {@code null}.
*
* @see Invite#getTargetApplication()
*/
EMBEDDED_APPLICATION(2),

/**
* Unknown invite target type.
* Either this invite does not have a target type or Discord implemented a new invite target type and JDA has not yet implemented support for it.
*/
UNKNOWN(-1);
anweisen marked this conversation as resolved.
Show resolved Hide resolved

private final int id;

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

/**
* The Discord id key used to represent the target type.
*
* @return The id key used by discord for this channel type.
*/
public int getId()
anweisen marked this conversation as resolved.
Show resolved Hide resolved
{
return id;
}

/**
* Static accessor for retrieving a target type based on its Discord id key.
*
* @param id
* The id key of the requested target type.
*
* @return The TargetType that is referred to by the provided key. If the id key is unknown, {@link #UNKNOWN} is returned.
*/
@Nonnull
public static TargetType fromId(int id) {
anweisen marked this conversation as resolved.
Show resolved Hide resolved
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
for (TargetType type : values())
{
if (type.id == id)
return type;
}
return UNKNOWN;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import net.dv8tion.jda.api.entities.GuildChannel;
import net.dv8tion.jda.api.entities.Invite;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.utils.MiscUtil;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -118,4 +120,91 @@ public interface InviteAction extends AuditableRestAction<Invite>
@Nonnull
@CheckReturnValue
InviteAction setUnique(@Nullable final Boolean unique);

/**
* Sets the id of the targeted application.
* The invite has to point to a voice channel.
anweisen marked this conversation as resolved.
Show resolved Hide resolved
* The invite will have the {@link Invite.TargetType#EMBEDDED_APPLICATION} target.
*
* @param applicationId
* The id of the embedded application to target or {@code 0} to remove
*
* @return The current InviteAction for chaining.
*/
@Nonnull
@CheckReturnValue
InviteAction setTargetApplication(final long applicationId);

/**
* Sets the id of the targeted application.
* The invite has to point to a voice channel.
anweisen marked this conversation as resolved.
Show resolved Hide resolved
* The invite will have the {@link Invite.TargetType#EMBEDDED_APPLICATION} target.
*
* @param applicationId
* The id of the embedded application to target
*
* @throws java.lang.IllegalArgumentException
* If the provided ID is null
* @throws java.lang.NumberFormatException
* If the provided ID is not a snowflake
*
* @return The current InviteAction for chaining.
anweisen marked this conversation as resolved.
Show resolved Hide resolved
*/
@Nonnull
@CheckReturnValue
default InviteAction setTargetApplication(@Nonnull final String applicationId) {
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
return setTargetApplication(MiscUtil.parseSnowflake(applicationId));
anweisen marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Sets the user whose stream to target for this invite.
* The user must be streaming in the same channel.
anweisen marked this conversation as resolved.
Show resolved Hide resolved
* The invite will have the {@link Invite.TargetType#STREAM} target.
*
* @param userId
* The id of the user whose stream to target or {@code 0} to remove.
*
* @return The current InviteAction for chaining.
*/
@Nonnull
@CheckReturnValue
InviteAction setTargetUser(final long userId);
anweisen marked this conversation as resolved.
Show resolved Hide resolved

/**
* Sets the user whose stream to display for this invite.
* The user must be streaming in the same channel.
anweisen marked this conversation as resolved.
Show resolved Hide resolved
* The invite will have the {@link Invite.TargetType#STREAM} target.
*
* @param userId
* The id of the user whose stream to target.
*
* @throws java.lang.IllegalArgumentException
* If the provided ID is null
* @throws java.lang.NumberFormatException
* If the provided ID is not a snowflake
*
* @return The current InviteAction for chaining.
*/
@Nonnull
@CheckReturnValue
default InviteAction setTargetUser(@Nonnull final String userId) {
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
return setTargetUser(MiscUtil.parseSnowflake(userId));
}

/**
* Sets the user whose stream to display for this invite.
* The user must be streaming in the same channel.
anweisen marked this conversation as resolved.
Show resolved Hide resolved
* The invite will have the {@link Invite.TargetType#STREAM} target.
*
* @param user
* The user whose stream to target.
*
* @return The current InviteAction for chaining.
*/
@Nonnull
@CheckReturnValue
default InviteAction setTargetUser(@Nonnull final User user) {
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
return setTargetUser(user.getIdLong());
anweisen marked this conversation as resolved.
Show resolved Hide resolved
}

}
32 changes: 30 additions & 2 deletions src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,7 @@ public WebhookImpl createWebhook(DataObject object, boolean allowMissingChannel)

Optional<DataObject> ownerJson = object.optObject("user");
User owner = null;

if (ownerJson.isPresent())
{
DataObject json = ownerJson.get();
Expand Down Expand Up @@ -1556,11 +1556,14 @@ public Invite createInvite(DataObject object)

final DataObject channelObject = object.getObject("channel");
final ChannelType channelType = ChannelType.fromId(channelObject.getInt("type"));
final Invite.TargetType targetType = Invite.TargetType.fromId(object.getInt("target_type", -1));

final Invite.InviteType type;
final Invite.Guild guild;
final Invite.Channel channel;
final Invite.Group group;
final Invite.EmbeddedApplication application;
final User targetUser;

if (channelType == ChannelType.GROUP)
{
Expand Down Expand Up @@ -1618,6 +1621,31 @@ else if (channelType.isGuild())
group = null;
}

switch (targetType)
{
case EMBEDDED_APPLICATION:
final DataObject applicationObject = object.getObject("target_application");

final String applicationIconId = applicationObject.getString("icon", null);
final String applicationName = applicationObject.getString("name");
final String applicationDescription = applicationObject.getString("description");
final String applicationSummary = applicationObject.getString("summary");
final long applicationId = applicationObject.getLong("id");
final int maxApplicationParticipants = applicationObject.getInt("max_participants", -1);

application = new InviteImpl.EmbeddedApplicationImpl(applicationIconId, applicationName, applicationDescription, applicationSummary, applicationId, maxApplicationParticipants);
targetUser = null;
break;
case STREAM:
final DataObject targetUserObject = object.getObject("target_user");
targetUser = createUser(targetUserObject);
application = null;
break;
default:
application = null;
targetUser = null;
}

final int maxAge;
final int maxUses;
final boolean temporary;
Expand Down Expand Up @@ -1646,7 +1674,7 @@ else if (channelType.isGuild())

return new InviteImpl(getJDA(), code, expanded, inviter,
maxAge, maxUses, temporary,
timeCreated, uses, channel, guild, group, type);
timeCreated, uses, channel, guild, group, application, targetUser, type, targetType);
}

public ApplicationInfo createApplicationInfo(DataObject object)
Expand Down
Loading