Skip to content

Commit

Permalink
Support setting Custom Statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
wasdennnoch committed Sep 16, 2023
1 parent 734d6f5 commit 65e554b
Showing 1 changed file with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.javacord.api.Javacord;
import org.javacord.api.entity.Nameable;
import org.javacord.api.entity.activity.Activity;
import org.javacord.api.entity.activity.ActivityType;
import org.javacord.api.entity.channel.ServerVoiceChannel;
import org.javacord.api.entity.intent.Intent;
import org.javacord.api.entity.server.Server;
Expand Down Expand Up @@ -935,7 +936,7 @@ public CompletableFuture<Boolean> isReady() {
* Sends the update status packet.
*/
public void updateStatus() {
Optional<Activity> activity = api.getActivity();
Optional<Activity> optionalActivity = api.getActivity();
ObjectNode updateStatus = JsonNodeFactory.instance.objectNode()
.put("op", GatewayOpcode.STATUS_UPDATE.getCode());
ObjectNode data = updateStatus.putObject("d")
Expand All @@ -944,18 +945,21 @@ public void updateStatus() {
.putNull("since");

ObjectNode activityJson = data.putObject("game");
activityJson.put("name", activity.map(Nameable::getName).orElse(null));
activityJson.put("type", activity.flatMap(g -> {
int type = g.getType().getId();
if (type == 4) {
logger.warn("Can't set the activity to ActivityType.CUSTOM"
+ ", using ActivityType.PLAYING instead");
return Optional.empty();
if (optionalActivity.isPresent()) {
Activity activity = optionalActivity.get();
ActivityType type = activity.getType();
activityJson.put("type", type.getId());
if (type == ActivityType.CUSTOM) {
activityJson.put("name", "Custom Status");
activityJson.put("state", activity.getName());
} else {
return Optional.of(type);
activityJson.put("name", activity.getName());
}
}).orElse(0));
activity.flatMap(Activity::getStreamingUrl).ifPresent(url -> activityJson.put("url", url));
activity.getStreamingUrl().ifPresent(url -> activityJson.put("url", url));
} else {
activityJson.put("name", (String) null);
activityJson.put("type", 0);
}
logger.debug("Updating status (content: {})", updateStatus);
sendTextFrame(updateStatus.toString());
}
Expand Down

0 comments on commit 65e554b

Please sign in to comment.