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

Add custom status support for bots #2521

Merged
Merged
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
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"))
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
{
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