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

Update Invite to match Discord changes #2500

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
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
62 changes: 40 additions & 22 deletions src/main/java/net/dv8tion/jda/api/entities/Invite.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static RestAction<Invite> resolve(@Nonnull final JDA api, @Nonnull final String
* An {@link Invite.Channel Invite.Channel} object
* containing information about this invite's origin channel.
*
* @return Information about this invite's origin channel or null in case of a group invite
* @return Information about this invite's origin channel or null in case of a non-guild invite
*
* @see Invite.Channel
*/
Expand All @@ -167,7 +167,7 @@ static RestAction<Invite> resolve(@Nonnull final JDA api, @Nonnull final String
* An {@link Invite.Group Invite.Group} object
* containing information about this invite's origin group.
*
* @return Information about this invite's origin group or null in case of a guild invite
* @return Information about this invite's origin group or null in case of a non-group invite
*
* @see Invite.Group
*/
Expand Down Expand Up @@ -210,17 +210,17 @@ default String getUrl()
* An {@link Invite.Guild Invite.Guild} object
* containing information about this invite's origin guild.
*
* @return Information about this invite's origin guild or null in case of a group invite
* @return Information about this invite's origin guild or null in case of a non-guild invite
*
* @see Invite.Guild
*/
@Nullable
Guild getGuild();

/**
* The user who created this invite. For not expanded invites this may be null.
* The user who created this invite. For a non-expanded invite, this may be null.
*
* @return The user who created this invite
* @return The user who created this invite. If this is a {@link Invite.InviteType#FRIEND Friend invite} this is the target.
*/
@Nullable
User getInviter();
Expand Down Expand Up @@ -249,18 +249,15 @@ default String getUrl()
int getMaxAge();

/**
* The max uses of this invite. If there is no limit thus will return {@code 0}.
*
* <p>This works only for expanded invites and will throw a {@link IllegalStateException} otherwise!
*
* @throws IllegalStateException
* if this invite is not expanded
*
* @return The max uses of this invite or {@code 0} if there is no limit
*
* @see #expand()
* @see #isExpanded()
*/
* The max uses of this invite.
* If there is no limit this will return {@code 0}.
*
* <p>This works only for expanded invites, and will throw a {@link java.lang.IllegalStateException} otherwise!</p>
*
* @throws java.lang.IllegalStateException
* if this is not an expanded invite
* @return The max uses of this invite or {@code 0} if there is no limit
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs changes here seem unrelated to the rest of this PR, please revert them.

int getMaxUses();

/**
Expand All @@ -279,16 +276,23 @@ default String getUrl()
@Nonnull
OffsetDateTime getTimeCreated();

/**
* Returns the expiration date of this invite, if it expires
*
* @return The expiration date of this invite, or null if it doesn't expire
*/
@Nullable
OffsetDateTime getTimeExpires();

/**
* How often this invite has been used.
*
* <p>This works only for expanded invites and will throw a {@link IllegalStateException} otherwise!
*
* @throws IllegalStateException
* @throws java.lang.IllegalStateException
* if this invite is not expanded
*
* @return The uses of this invite
*
* @see #expand()
* @see #isExpanded()
*/
Expand All @@ -312,13 +316,12 @@ default String getUrl()
/**
* Whether this Invite grants only temporary access or not.
*
* <p>This works only for expanded invites and will throw a {@link IllegalStateException} otherwise!
* <p>This works only for expanded invites and will throw a {@link IllegalStateException} otherwise!</p>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* <p>This works only for expanded invites and will throw a {@link IllegalStateException} otherwise!</p>
* <p>This works only for expanded invites and will throw a {@link IllegalStateException} otherwise!

*
* @throws IllegalStateException
* if this invite is not expanded
*
* @return Whether this invite is temporary or not
*
* @see #expand()
* @see #isExpanded()
*/
Expand Down Expand Up @@ -689,7 +692,22 @@ enum InviteType
{
GUILD,
GROUP,
UNKNOWN
FRIEND,
UNKNOWN;

/**
* Static accessor for retrieving a invite type based on its Discord id key.
*
* @param id
* The id key of the requested invite type.
*
* @return The InviteType that is referred to by the provided key. If the id key is unknown, {@link #UNKNOWN} is returned.
*/
@Nonnull
public static InviteType fromId(int id)
{
return id < UNKNOWN.ordinal() ? values()[id] : UNKNOWN;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
*/
public interface InviteAction extends AuditableRestAction<Invite>
{
/**
* The maximum age, 30 days, in seconds, for a restricted invite.
*
* <p>A restricted invite is an invite that is created in a non-community server</p>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* <p>A restricted invite is an invite that is created in a non-community server</p>
* <p>A restricted invite is an invite that is created in a non-community server

*
* @see #hasCommunityRestrictions()
*/
int MAX_RESTRICTED_AGE = 2592000;

@Nonnull
@Override
InviteAction setCheck(@Nullable BooleanSupplier checks);
Expand All @@ -48,17 +57,28 @@ public interface InviteAction extends AuditableRestAction<Invite>
@Override
InviteAction deadline(long timestamp);

/**
* Invites created in non-community servers are not allowed to be permanent,
* and are restricted to a maximum age of 30 days.
*
* @see #MAX_RESTRICTED_AGE
*/
boolean hasCommunityRestrictions();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return docs


/**
* Sets the max age in seconds for the invite. Set this to {@code 0} if the invite should never expire. Default is {@code 86400} (24 hours).
* {@code null} will reset this to the default value.
*
* <p>Invites in non-community servers are not allowed to be permanent, with an allowed range of {@code 1} (1 second) to {@code 2592000} (30 days).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* <p>Invites in non-community servers are not allowed to be permanent, with an allowed range of {@code 1} (1 second) to {@code 2592000} (30 days).
* <p>Invites in {@link #hasCommunityRestrictions() non-community} servers are not allowed to be permanent, with an allowed range of {@code 1} (1 second) to {@code 2592000} (30 days).

*
* @param maxAge
* The max age for this invite or {@code null} to use the default value.
*
* @throws IllegalArgumentException
* If maxAge is negative.
* If maxAge is negative, or if the guild is not a community guild and maxAge is not in the allowed range.
*
* @return The current InviteAction for chaining.
* @see #hasCommunityRestrictions()
*/
@Nonnull
@CheckReturnValue
Expand All @@ -68,15 +88,20 @@ public interface InviteAction extends AuditableRestAction<Invite>
* Sets the max age for the invite. Set this to {@code 0} if the invite should never expire. Default is {@code 86400} (24 hours).
* {@code null} will reset this to the default value.
*
* <p>Invites in non-community servers are not allowed to be permanent, with an allowed range from 1 second to 30 days.
*
* @param maxAge
* The max age for this invite or {@code null} to use the default value.
* @param timeUnit
* The {@link java.util.concurrent.TimeUnit TimeUnit} type of {@code maxAge}.
*
* @throws IllegalArgumentException
* If maxAge is negative or maxAge is positive and timeUnit is null.
* If maxAge is negative,
* timeUnit is null,
* or if the guild is not a community guild and maxAge is not in the allowed range.
*
* @return The current InviteAction for chaining.
* @see #hasCommunityRestrictions()
*/
@Nonnull
@CheckReturnValue
Expand Down Expand Up @@ -235,5 +260,4 @@ default InviteAction setTargetStream(@Nonnull final Member member)
Checks.notNull(member, "Member");
return setTargetStream(member.getIdLong());
}

}
43 changes: 16 additions & 27 deletions src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2162,19 +2162,18 @@ public Invite createInvite(DataObject object)
final String code = object.getString("code");
final User inviter = object.hasKey("inviter") ? createUser(object.getObject("inviter")) : null;

final DataObject channelObject = object.getObject("channel");
final ChannelType channelType = ChannelType.fromId(channelObject.getInt("type"));
final DataObject channelObject = object.optObject("channel").orElse(null);
final ChannelType channelType = channelObject != null ? ChannelType.fromId(channelObject.getInt("type")) : null;
final Invite.TargetType targetType = Invite.TargetType.fromId(object.getInt("target_type", 0));

final Invite.InviteType type;
final Invite.InviteType type = Invite.InviteType.fromId(object.getInt("type"));
final Invite.Guild guild;
final Invite.Channel channel;
final Invite.Group group;
final Invite.InviteTarget target;

if (channelType == ChannelType.GROUP)
if (type == Invite.InviteType.GROUP)
{
type = Invite.InviteType.GROUP;
guild = null;
channel = null;

Expand All @@ -2190,10 +2189,8 @@ public Invite createInvite(DataObject object)

group = new InviteImpl.GroupImpl(groupIconId, groupName, groupId, usernames);
}
else if (channelType.isGuild())
else if (type == Invite.InviteType.GUILD)
{
type = Invite.InviteType.GUILD;

final DataObject guildObject = object.getObject("guild");

final String guildIconId = guildObject.getString("icon", null);
Expand Down Expand Up @@ -2225,8 +2222,6 @@ else if (channelType.isGuild())
else
{
// Unknown channel type for invites

type = Invite.InviteType.UNKNOWN;
guild = null;
channel = null;
group = null;
Expand Down Expand Up @@ -2254,34 +2249,28 @@ else if (channelType.isGuild())
target = new InviteImpl.InviteTargetImpl(targetType, null, null);
}

final int maxAge;
final int maxAge = object.getInt("max_age", -1);
final int maxUses;
final boolean temporary;
final OffsetDateTime timeCreated;
final boolean temporary = object.getBoolean("temporary", false);
final int uses;
final boolean expanded;
final boolean expanded = object.hasKey("max_uses");

if (object.hasKey("max_uses"))
if (expanded)
{
expanded = true;
maxAge = object.getInt("max_age");
maxUses = object.getInt("max_uses");
uses = object.getInt("uses");
temporary = object.getBoolean("temporary");
timeCreated = OffsetDateTime.parse(object.getString("created_at"));
maxUses = object.getInt("max_uses", -1);
uses = object.getInt("uses", 0);
}
else
{
expanded = false;
maxAge = -1;
maxUses = -1;
uses = -1;
temporary = false;
timeCreated = null;
}

return new InviteImpl(getJDA(), code, expanded, inviter,
maxAge, maxUses, temporary, timeCreated,
final OffsetDateTime timeCreated = object.getOffsetDateTime("created_at", null);
final OffsetDateTime timeExpires = object.getOffsetDateTime("expires_at", null);

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

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/net/dv8tion/jda/internal/entities/InviteImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ public class InviteImpl implements Invite
private final int maxUses;
private final boolean temporary;
private final OffsetDateTime timeCreated;
private final OffsetDateTime timeExpires;
private final int uses;
private final Invite.InviteType type;

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 int maxAge, final int maxUses, final boolean temporary, final OffsetDateTime timeCreated, final OffsetDateTime timeExpires, final int uses,
final Channel channel, final Guild guild, final Group group, final InviteTarget target, final Invite.InviteType type)
{
this.api = api;
Expand All @@ -70,6 +71,7 @@ public InviteImpl(final JDAImpl api, final String code, final boolean expanded,
this.maxUses = maxUses;
this.temporary = temporary;
this.timeCreated = timeCreated;
this.timeExpires = timeExpires;
this.uses = uses;
this.channel = channel;
this.guild = guild;
Expand Down Expand Up @@ -238,6 +240,13 @@ public OffsetDateTime getTimeCreated()
return this.timeCreated;
}

@Nullable
@Override
public OffsetDateTime getTimeExpires()
{
return this.timeExpires;
}

@Override
public int getUses()
{
Expand All @@ -251,7 +260,7 @@ public boolean isExpanded()
{
return this.expanded;
}

My-Name-Is-Jeff marked this conversation as resolved.
Show resolved Hide resolved
@Override
public boolean isTemporary()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ default InviteAction createInvite()
{
checkPermission(Permission.CREATE_INSTANT_INVITE);

return new InviteActionImpl(this.getJDA(), this.getId());
return new InviteActionImpl(this.getJDA(), this.getId(), !this.getGuild().getFeatures().contains("COMMUNITY"));
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ protected Long handleInternally(DataObject content)
.map(String::valueOf)
.map(OffsetDateTime::parse)
.orElse(null);
OffsetDateTime expirationTime = content.opt("expires_at")
.map(String::valueOf)
.map(OffsetDateTime::parse)
.orElse(null);

Optional<DataObject> inviterJson = content.optObject("inviter");
boolean expanded = maxUses != -1;
Expand Down Expand Up @@ -98,9 +102,9 @@ protected Long handleInternally(DataObject content)
target = new InviteImpl.InviteTargetImpl(targetType, null, null);
}

Invite invite = new InviteImpl(getJDA(), code, expanded, inviter,
maxAge, maxUses, temporary, creationTime,
0, channel, guild, null, target, Invite.InviteType.GUILD);
Invite invite = new InviteImpl(getJDA(), code, expanded, inviter, maxAge,
maxUses, temporary, creationTime, expirationTime,
0, channel, guild, null, target, Invite.InviteType.GUILD);
getJDA().handleEvent(
new GuildInviteCreateEvent(
getJDA(), responseNumber,
Expand Down
Loading
Loading