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 1 commit
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
43 changes: 43 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/Invite.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ static RestAction<Invite> resolve(@Nonnull final JDA api, @Nonnull final String
@Nonnull
Invite.InviteType getType();

/**
* The target type of this invite.
*
* @return The invite's type
*/
@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 Down Expand Up @@ -503,4 +511,39 @@ enum InviteType
GROUP,
UNKNOWN
}

enum TargetType
{
STREAM(1),

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;
}

public int getId()
anweisen marked this conversation as resolved.
Show resolved Hide resolved
{
return id;
}

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,99 @@ public interface InviteAction extends AuditableRestAction<Invite>
@Nonnull
@CheckReturnValue
InviteAction setUnique(@Nullable final Boolean unique);

/**
* Sets the type of target for this voice channel invite.
*
* @param type
* The target type for this voice channel invite.
*
* @throws java.lang.IllegalArgumentException
* If this channel is not a voice channel.
*
* @return The current InviteAction for chaining.
*/
@Nonnull
@CheckReturnValue
InviteAction setTargetType(@Nullable final Invite.TargetType type);
anweisen marked this conversation as resolved.
Show resolved Hide resolved

/**
* Sets the id of the targeted application.
* This will automatically {@link #setTargetType(Invite.TargetType) set the target type} to {@link Invite.TargetType#EMBEDDED_APPLICATION}.
*
* @param applicationId
* The id of the embedded application or {@code null} to use none, requires type {@link Invite.TargetType#EMBEDDED_APPLICATION}
*
* @return The current InviteAction for chaining.
*/
@Nonnull
@CheckReturnValue
InviteAction setTargetApplication(@Nullable final Long applicationId);
anweisen marked this conversation as resolved.
Show resolved Hide resolved

/**
* Sets the id of the targeted application.
* This will automatically {@link #setTargetType(Invite.TargetType) set the target type} to {@link Invite.TargetType#EMBEDDED_APPLICATION}.
*
* @param applicationId
* The id of the embedded application or {@code null} to use none, requires type {@link Invite.TargetType#EMBEDDED_APPLICATION}
*
* @return The current InviteAction for chaining.
anweisen marked this conversation as resolved.
Show resolved Hide resolved
*/
@Nonnull
@CheckReturnValue
default InviteAction setTargetApplication(@Nullable final String applicationId) {
return setTargetApplication(MiscUtil.parseSnowflake(applicationId));
anweisen marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Sets the user whose stream to display for this invite.
* This will automatically {@link #setTargetType(Invite.TargetType) set the target type} to {@link Invite.TargetType#STREAM}.
* The user must be streaming in the same channel.
anweisen marked this conversation as resolved.
Show resolved Hide resolved
*
* @param userId
* The id of the user whose stream to display.
*
* @return The current InviteAction for chaining.
*/
@Nonnull
@CheckReturnValue
InviteAction setTargetUser(@Nullable final Long userId);
anweisen marked this conversation as resolved.
Show resolved Hide resolved

/**
* Sets the user whose stream to display for this invite.
* This will automatically {@link #setTargetType(Invite.TargetType) set the target type} to {@link Invite.TargetType#STREAM}.
* The user must be streaming in the same channel.
anweisen marked this conversation as resolved.
Show resolved Hide resolved
*
* @param userId
* The id of the user whose stream to display.
*
* @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 String userId) {
return setTargetUser(MiscUtil.parseSnowflake(userId));
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,7 @@ 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;
Expand Down Expand Up @@ -1646,7 +1647,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, type, targetType);
}

public ApplicationInfo createApplicationInfo(DataObject object)
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/net/dv8tion/jda/internal/entities/InviteImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ public class InviteImpl implements Invite
private final OffsetDateTime timeCreated;
private final int uses;
private final Invite.InviteType type;
private final Invite.TargetType targetType;

public InviteImpl(final JDAImpl api, final String code, final boolean expanded, final User inviter,
final int maxAge, final int maxUses, final boolean temporary, final OffsetDateTime timeCreated,
final int uses, final Channel channel, final Guild guild, final Group group, final Invite.InviteType type)
final int uses, final Channel channel, final Guild guild, final Group group, final Invite.InviteType type, Invite.TargetType targetType)
{
this.api = api;
this.code = code;
Expand All @@ -72,6 +73,7 @@ public InviteImpl(final JDAImpl api, final String code, final boolean expanded,
this.guild = guild;
this.group = group;
this.type = type;
this.targetType = targetType;
}

public static RestAction<Invite> resolve(final JDA api, final String code, final boolean withCounts)
Expand Down Expand Up @@ -157,6 +159,13 @@ public Invite.InviteType getType()
return this.type;
}

@Nonnull
@Override
public TargetType getTargetType()
{
return this.targetType;
}

@Override
public Channel getChannel()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ protected Long handleInternally(DataObject content)
InviteImpl.ChannelImpl channel = new InviteImpl.ChannelImpl(realChannel);
InviteImpl.GuildImpl guild = new InviteImpl.GuildImpl(realGuild);

Invite invite = new InviteImpl(getJDA(), code, expanded, inviter, maxAge, maxUses, temporary, creationTime, 0, channel, guild, null, Invite.InviteType.GUILD);
Invite.TargetType targetType = Invite.TargetType.fromId(content.getInt("target_type", -1));

Invite invite = new InviteImpl(getJDA(), code, expanded, inviter, maxAge, maxUses, temporary, creationTime, 0, channel, guild, null, Invite.InviteType.GUILD, targetType);
getJDA().handleEvent(
new GuildInviteCreateEvent(
getJDA(), responseNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;

Expand All @@ -37,6 +38,9 @@ public class InviteActionImpl extends AuditableRestActionImpl<Invite> implements
private Integer maxUses = null;
private Boolean temporary = null;
private Boolean unique = null;
private Long targetApplication = null;
private Long targetUser = null;
private Invite.TargetType targetType = null;

public InviteActionImpl(final JDA api, final String channelId)
{
Expand Down Expand Up @@ -120,6 +124,32 @@ public InviteActionImpl setUnique(final Boolean unique)
return this;
}

@Nonnull
@Override
public InviteAction setTargetType(@Nullable final Invite.TargetType type)
{
this.targetType = type;
return this;
}

@Nonnull
@Override
public InviteAction setTargetApplication(@Nullable Long applicationId)
{
this.targetType = Invite.TargetType.EMBEDDED_APPLICATION;
this.targetApplication = applicationId;
return this;
}

@Nonnull
@Override
public InviteAction setTargetUser(@Nullable Long userId)
{
this.targetType = Invite.TargetType.STREAM;
this.targetUser = userId;
return this;
}

@Override
protected RequestBody finalizeData()
{
Expand All @@ -133,6 +163,12 @@ protected RequestBody finalizeData()
object.put("temporary", this.temporary);
if (this.unique != null)
object.put("unique", this.unique);
if (this.targetType != null)
object.put("target_type", targetType.getId());
if (this.targetUser != null)
object.put("target_user_id", targetUser);
if (this.targetApplication != null)
object.put("target_application_id", targetApplication);

return getRequestBody(object);
}
Expand Down