Skip to content

Commit

Permalink
Add custom status support for bots (#2521)
Browse files Browse the repository at this point in the history
  • Loading branch information
RedDaedalus authored Aug 10, 2023
1 parent 77c626e commit a2fa810
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
31 changes: 21 additions & 10 deletions src/main/java/net/dv8tion/jda/api/entities/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package net.dv8tion.jda.api.entities;

import net.dv8tion.jda.annotations.Incubating;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.entities.emoji.EmojiUnion;
import net.dv8tion.jda.internal.entities.EntityBuilder;
Expand Down Expand Up @@ -194,11 +193,8 @@ static Activity listening(@Nonnull String name)
* if the specified name is null, empty, blank or longer than 128 characters
*
* @return A valid Activity instance with the provided name with {@link net.dv8tion.jda.api.entities.Activity.ActivityType#WATCHING}
*
* @incubating This feature is not yet confirmed for the official bot API
*/
@Nonnull
@Incubating
static Activity watching(@Nonnull String name)
{
Checks.notBlank(name, "Name");
Expand Down Expand Up @@ -230,6 +226,27 @@ static Activity competing(@Nonnull String name)
return EntityBuilder.createActivity(name, null, ActivityType.COMPETING);
}

/**
* Creates a new Activity instance with the specified name.
* <br>This will display without a prefix in the official client
*
* @param 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
*
* @return A valid Activity instance with the provided name with {@link net.dv8tion.jda.api.entities.Activity.ActivityType#CUSTOM_STATUS}
*/
@Nonnull
static Activity customStatus(@Nonnull String name)
{
Checks.notBlank(name, "Name");
name = name.trim();
Checks.notLonger(name, 128, "Name");
return EntityBuilder.createActivity(name, null, ActivityType.CUSTOM_STATUS);
}

/**
* Creates a new Activity instance with the specified name.
*
Expand Down Expand Up @@ -331,18 +348,12 @@ enum ActivityType
/**
* Used to indicate that the {@link Activity Activity} should display
* as {@code Watching...} in the official client.
*
* @incubating This feature is not yet confirmed for the official bot API
*/
@Incubating
WATCHING(3),
/**
* Used to indicate that the {@link Activity Activity} should display as a custom status
* in the official client.
*
* @incubating This Activity type is <b>read-only</b> for bots
*/
@Incubating
CUSTOM_STATUS(4),

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ public static Activity createActivity(DataObject gameJson)

if (type == Activity.ActivityType.CUSTOM_STATUS)
{
if (gameJson.hasKey("state") && name.equalsIgnoreCase("Custom Status"))
if (gameJson.hasKey("state"))
{
name = gameJson.getString("state", "");
gameJson = gameJson.remove("state");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,17 @@ private DataObject getGameJson(Activity activity)
if (activity == null || activity.getName() == null || activity.getType() == null)
return null;
DataObject gameObj = DataObject.empty();
gameObj.put("name", activity.getName());

if (activity.getType() == Activity.ActivityType.CUSTOM_STATUS)
{
gameObj.put("name", "Custom Status");
gameObj.put("state", activity.getName());
}
else
{
gameObj.put("name", activity.getName());
}

gameObj.put("type", activity.getType().getKey());
if (activity.getUrl() != null)
gameObj.put("url", activity.getUrl());
Expand Down

0 comments on commit a2fa810

Please sign in to comment.