Skip to content

Commit

Permalink
Add Activity#withState (#2523)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment authored Aug 14, 2023
1 parent 2c36ba7 commit 72aaf10
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 40 deletions.
74 changes: 60 additions & 14 deletions src/main/java/net/dv8tion/jda/api/entities/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.EntityString;
import net.dv8tion.jda.internal.utils.Helpers;
import org.jetbrains.annotations.Contract;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -49,6 +50,12 @@ public interface Activity
/** The Pattern used for {@link #isValidStreamingUrl(String)} */
Pattern STREAMING_URL = Pattern.compile("https?://(www\\.)?(twitch\\.tv/|youtube\\.com/watch\\?v=).+", Pattern.CASE_INSENSITIVE);

/** Maximum length for an activity name */
int MAX_ACTIVITY_NAME_LENGTH = 128;

/** Maximum length for an activity state */
int MAX_ACTIVITY_STATE_LENGTH = 128;

/**
* Whether this is a <a href="https://discord.com/developers/docs/rich-presence/best-practices" target="_blank">Rich Presence</a>
* <br>If {@code false} the result of {@link #asRichPresence()} is {@code null}
Expand All @@ -75,6 +82,29 @@ public interface Activity
@Nonnull
String getName();

/**
* The user's activity state
* <br>Example: "Looking to Play", "Playing Solo", "In a Group"
*
* <p>This shows below the normal activity information in the profile.
*
* <p><b>Example</b><br>
* Code:
* <pre>{@code
* Activity.playing("Trivia")
* .withState("Question 20")
* }</pre>
* Display:
* <pre>
* Playing Trivia
* Question 20
* </pre>
*
* @return The user's current party status
*/
@Nullable
String getState();

/**
* The URL of the {@link Activity Activity} if the game is actually a Stream.
* <br>This will return null for regular games.
Expand Down Expand Up @@ -108,6 +138,22 @@ public interface Activity
@Nullable
EmojiUnion getEmoji();

/**
* Adds the provided state to the activity.
* <br>The state is shown below the activity, unless it is a {@link #customStatus(String) custom status}.
*
* @param state
* The activity state, or null to unset
*
* @throws IllegalArgumentException
* If the state is longer than {@value #MAX_ACTIVITY_STATE_LENGTH} characters
*
* @return New activity instance with the provided state
*/
@Nonnull
@Contract("_->new")
Activity withState(@Nullable String state);

/**
* Creates a new Activity instance with the specified name.
* <br>In order to appear as "streaming" in the official client you must
Expand All @@ -117,7 +163,7 @@ public interface Activity
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* if the specified name is null, empty, blank or longer than 128 characters
* if the specified name is null, empty, blank or longer than {@value #MAX_ACTIVITY_NAME_LENGTH} characters
*
* @return A valid Activity instance with the provided name with {@link net.dv8tion.jda.api.entities.Activity.ActivityType#PLAYING}
*/
Expand All @@ -126,7 +172,7 @@ static Activity playing(@Nonnull String name)
{
Checks.notBlank(name, "Name");
name = name.trim();
Checks.notLonger(name, 128, "Name");
Checks.notLonger(name, MAX_ACTIVITY_NAME_LENGTH, "Name");
return EntityBuilder.createActivity(name, null, ActivityType.PLAYING);
}

Expand All @@ -141,7 +187,7 @@ static Activity playing(@Nonnull String name)
* The streaming url to use, required to display as "streaming"
*
* @throws IllegalArgumentException
* If the specified name is null, empty or longer than 128 characters
* If the specified name is null, empty or longer than {@value #MAX_ACTIVITY_NAME_LENGTH} characters
*
* @return A valid Activity instance with the provided name and url
*
Expand All @@ -152,7 +198,7 @@ static Activity streaming(@Nonnull String name, @Nullable String url)
{
Checks.notEmpty(name, "Provided game name");
name = Helpers.isBlank(name) ? name : name.trim();
Checks.notLonger(name, 128, "Name");
Checks.notLonger(name, MAX_ACTIVITY_NAME_LENGTH, "Name");
ActivityType type;
if (isValidStreamingUrl(url))
type = ActivityType.STREAMING;
Expand All @@ -169,7 +215,7 @@ static Activity streaming(@Nonnull String name, @Nullable String url)
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* if the specified name is null, empty, blank or longer than 128 characters
* if the specified name is null, empty, blank or longer than {@value #MAX_ACTIVITY_NAME_LENGTH} characters
*
* @return A valid Activity instance with the provided name with {@link net.dv8tion.jda.api.entities.Activity.ActivityType#LISTENING}
*/
Expand All @@ -178,7 +224,7 @@ static Activity listening(@Nonnull String name)
{
Checks.notBlank(name, "Name");
name = name.trim();
Checks.notLonger(name, 128, "Name");
Checks.notLonger(name, MAX_ACTIVITY_NAME_LENGTH, "Name");
return EntityBuilder.createActivity(name, null, ActivityType.LISTENING);
}

Expand All @@ -190,7 +236,7 @@ static Activity listening(@Nonnull String name)
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* if the specified name is null, empty, blank or longer than 128 characters
* if the specified name is null, empty, blank or longer than {@value #MAX_ACTIVITY_NAME_LENGTH} characters
*
* @return A valid Activity instance with the provided name with {@link net.dv8tion.jda.api.entities.Activity.ActivityType#WATCHING}
*/
Expand All @@ -199,7 +245,7 @@ static Activity watching(@Nonnull String name)
{
Checks.notBlank(name, "Name");
name = name.trim();
Checks.notLonger(name, 128, "Name");
Checks.notLonger(name, MAX_ACTIVITY_NAME_LENGTH, "Name");
return EntityBuilder.createActivity(name, null, ActivityType.WATCHING);
}

Expand All @@ -211,7 +257,7 @@ static Activity watching(@Nonnull String name)
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* If the specified name is null, empty, blank or longer than 128 characters
* If the specified name is null, empty, blank or longer than {@value #MAX_ACTIVITY_NAME_LENGTH} characters
*
* @return A valid Activity instance with the provided name with {@link net.dv8tion.jda.api.entities.Activity.ActivityType#COMPETING}
*
Expand All @@ -222,7 +268,7 @@ static Activity competing(@Nonnull String name)
{
Checks.notBlank(name, "Name");
name = name.trim();
Checks.notLonger(name, 128, "Name");
Checks.notLonger(name, MAX_ACTIVITY_NAME_LENGTH, "Name");
return EntityBuilder.createActivity(name, null, ActivityType.COMPETING);
}

Expand All @@ -234,7 +280,7 @@ static Activity competing(@Nonnull String name)
* The not-null name of the newly created status
*
* @throws IllegalArgumentException
* If the specified name is null, empty, blank or longer than 128 characters
* If the specified name is null, empty, blank or longer than {@value #MAX_ACTIVITY_NAME_LENGTH} characters
*
* @return A valid Activity instance with the provided name with {@link net.dv8tion.jda.api.entities.Activity.ActivityType#CUSTOM_STATUS}
*/
Expand All @@ -243,7 +289,7 @@ static Activity customStatus(@Nonnull String name)
{
Checks.notBlank(name, "Name");
name = name.trim();
Checks.notLonger(name, 128, "Name");
Checks.notLonger(name, MAX_ACTIVITY_NAME_LENGTH, "Name");
return EntityBuilder.createActivity(name, null, ActivityType.CUSTOM_STATUS);
}

Expand All @@ -258,7 +304,7 @@ static Activity customStatus(@Nonnull String name)
* @throws IllegalArgumentException
* <ul>
* <li>If the specified ActivityType is null or unsupported</li>
* <li>If the specified name is null, empty or longer than 128 characters</li>
* <li>If the specified name is null, empty or longer than {@value #MAX_ACTIVITY_NAME_LENGTH} characters</li>
* </ul>
*
* @return A valid Activity instance with the provided name
Expand All @@ -284,7 +330,7 @@ static Activity of(@Nonnull ActivityType type, @Nonnull String name)
* @throws IllegalArgumentException
* <ul>
* <li>If the specified ActivityType is null or unsupported</li>
* <li>If the specified name is null, empty or longer than 128 characters</li>
* <li>If the specified name is null, empty or longer than {@value #MAX_ACTIVITY_NAME_LENGTH} characters</li>
* </ul>
*
* @return A valid Activity instance with the provided name and url
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/net/dv8tion/jda/api/entities/RichPresence.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,6 @@ public interface RichPresence extends Activity
*/
EnumSet<ActivityFlag> getFlagSet();

/**
* The user's current party status
* <br>Example: "Looking to Play", "Playing Solo", "In a Group"
*
* @return The user's current party status
*/
@Nullable
String getState();

/**
* What the player is currently doing
* <br>Example: "Competitive - Captain's Mode", "In Queue", "Unranked PvP"
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/net/dv8tion/jda/api/utils/data/DataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -911,4 +911,20 @@ else if (value instanceof String && stringParse != null)
throw new ParsingException(Helpers.format("Cannot parse value for %s into type %s: %s instance of %s",
key, type.getSimpleName(), value, value.getClass().getSimpleName()));
}

@Override
public boolean equals(Object obj)
{
if (obj == this)
return true;
if (!(obj instanceof DataObject))
return false;
return ((DataObject) obj).toMap().equals(this.toMap());
}

@Override
public int hashCode()
{
return toMap().hashCode();
}
}
38 changes: 35 additions & 3 deletions src/main/java/net/dv8tion/jda/internal/entities/ActivityImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.RichPresence;
import net.dv8tion.jda.api.entities.emoji.EmojiUnion;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.EntityString;

import javax.annotation.Nonnull;
Expand All @@ -28,6 +29,7 @@ public class ActivityImpl implements Activity
{
protected final String name;
protected final String url;
protected final String state;
protected final ActivityType type;
protected final Timestamps timestamps;
protected final EmojiUnion emoji;
Expand All @@ -44,12 +46,18 @@ protected ActivityImpl(String name, String url)

protected ActivityImpl(String name, String url, ActivityType type)
{
this(name, url, type, null, null);
this(name, null, url, type, null, null);
}

protected ActivityImpl(String name, String url, ActivityType type, Activity.Timestamps timestamps, EmojiUnion emoji)
protected ActivityImpl(String name, String state, String url, ActivityType type)
{
this(name, state, url, type, null, null);
}

protected ActivityImpl(String name, String state, String url, ActivityType type, Activity.Timestamps timestamps, EmojiUnion emoji)
{
this.name = name;
this.state = state;
this.url = url;
this.type = type;
this.timestamps = timestamps;
Expand All @@ -75,6 +83,13 @@ public String getName()
return name;
}

@Nullable
@Override
public String getState()
{
return state;
}

@Override
public String getUrl()
{
Expand All @@ -101,6 +116,22 @@ public EmojiUnion getEmoji()
return emoji;
}

@Nonnull
@Override
public Activity withState(@Nullable String state)
{
if (state != null)
{
state = state.trim();
if (state.isEmpty())
state = null;
else
Checks.notLonger(state, MAX_ACTIVITY_STATE_LENGTH, "State");
}

return new ActivityImpl(name, state, url, type);
}

@Override
public boolean equals(Object o)
{
Expand All @@ -112,14 +143,15 @@ public boolean equals(Object o)
ActivityImpl oGame = (ActivityImpl) o;
return oGame.getType() == type
&& Objects.equals(name, oGame.getName())
&& Objects.equals(state, oGame.state)
&& Objects.equals(url, oGame.getUrl())
&& Objects.equals(timestamps, oGame.timestamps);
}

@Override
public int hashCode()
{
return Objects.hash(name, type, url, timestamps);
return Objects.hash(name, state, type, url, timestamps);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public class EntityBuilder
tmp.add("flags");
tmp.add("party");
tmp.add("session_id");
tmp.add("state");
tmp.add("sync_id");
richGameFields = Collections.unmodifiableSet(tmp);
}
Expand Down Expand Up @@ -938,16 +937,17 @@ public static Activity createActivity(DataObject gameJson)
}
}

String state = gameJson.isNull("state") ? null : String.valueOf(gameJson.get("state"));

if (!CollectionUtils.containsAny(gameJson.keys(), richGameFields))
return new ActivityImpl(name, url, type, timestamps, emoji);
return new ActivityImpl(name, state, url, type, timestamps, emoji);

// data for spotify
long id = gameJson.getLong("application_id", 0L);
String sessionId = gameJson.getString("session_id", null);
String syncId = gameJson.getString("sync_id", null);
int flags = gameJson.getInt("flags", 0);
String details = gameJson.isNull("details") ? null : String.valueOf(gameJson.get("details"));
String state = gameJson.isNull("state") ? null : String.valueOf(gameJson.get("state"));

RichPresence.Party party = null;
if (!gameJson.isNull("party"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class RichPresenceImpl extends ActivityImpl implements RichPresence

protected final Party party;
protected final String details;
protected final String state;
protected final Image largeImage;
protected final Image smallImage;
protected final String sessionId;
Expand All @@ -44,11 +43,10 @@ protected RichPresenceImpl(
EmojiUnion emoji, Party party, String details, String state, Timestamps timestamps, String syncId, String sessionId,
int flags, String largeImageKey, String largeImageText, String smallImageKey, String smallImageText)
{
super(name, url, type, timestamps, emoji);
super(name, state, url, type, timestamps, emoji);
this.applicationId = applicationId;
this.party = party;
this.details = details;
this.state = state;
this.sessionId = sessionId;
this.syncId = syncId;
this.flags = flags;
Expand Down Expand Up @@ -107,13 +105,6 @@ public EnumSet<ActivityFlag> getFlagSet()
return ActivityFlag.getFlags(getFlags());
}

@Nullable
@Override
public String getState()
{
return state;
}

@Nullable
@Override
public String getDetails()
Expand Down
Loading

0 comments on commit 72aaf10

Please sign in to comment.