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

feat(gateway): bot custom status support #206

Merged
merged 3 commits into from
Aug 10, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Activity implements Compilerable {

private final String name;
private final ActivityType type;
private String state;

private String streamUrl;
private String applicationId;
Expand All @@ -32,6 +33,24 @@ public Activity(String name, ActivityType type) {
this.type = type;
}

/**
* Allows a bot to define a custom status.
* Please note: Emojis are not supported in custom statuses for bots.
*/
public Activity(String customStatus) {
this(customStatus, ActivityType.CUSTOM);
setState(customStatus);
}

/**
* If you've selected an Activity Type that's not 4 (custom), you can set the state as extra info to your activity.
* User's current party status.
*/
public Activity setState(String state) {
this.state = state;
return this;
}

public Activity setStreamUrl(String url) {
this.streamUrl = url;
return this;
Expand All @@ -42,11 +61,6 @@ public Activity setApplicationId(String applicationId) {
return this;
}

public Activity setEmoji(Emoji emoji) {
this.emoji = emoji;
return this;
}

public Activity setInstance(boolean instance) {
this.instance = instance;
return this;
Expand All @@ -57,13 +71,19 @@ public Activity setFlags(EnumSet<ActivityFlags> flags) {
return this;
}

public Activity setEmoji(Emoji emoji) {
this.emoji = emoji;
return this;
}

public String name() {
return name;
}

public ActivityType type() {
return type;
}
public String state() { return state; }

public String streamUrl() {
return streamUrl;
Expand Down Expand Up @@ -94,7 +114,7 @@ public JSONObject compile() {
.put("application_id", applicationId)
.put("emoji", emoji == null ? JSONObject.NULL : emoji.compile())
.put("instance", instance)
.put("flags", flags == null ? JSONObject.NULL : new JSONObject().put("flags", flags));
.put("state", state);
}

@NotNull
Expand All @@ -103,6 +123,7 @@ public static Activity decompile(JSONObject obj, DiscordJar discordJar) {
ActivityType type;
String url;
String applicationId;
String state;
Emoji emoji;
boolean instance;
EnumSet<ActivityFlags> flags;
Expand All @@ -128,6 +149,10 @@ public static Activity decompile(JSONObject obj, DiscordJar discordJar) {
if (obj.has("flags")) flags = ActivityFlags.fromInt(obj.getInt("flags"));
else flags = null;

if (obj.has("state")) state = obj.getString("state");
else state = null;


return new Activity(name, type)
.setStreamUrl(url)
.setApplicationId(applicationId)
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/com/seailz/discordjar/utils/StatusRotor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.seailz.discordjar.utils;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.model.status.Status;

import java.util.List;

/**
* Simple util for a status rotor - switches between statuses every x seconds.
* @author Seailz
*/
public class StatusRotor {

private List<Status> statuses;
private long interval;
private DiscordJar dJar;
private boolean running;

/**
* Creates a new status rotor.
* Start the rotor by calling {@link #start()}
* @param status The statuses to rotate between
* @param interval The interval (in milliseconds) to switch between statuses
* @param dJar The DiscordJar instance
*/
public StatusRotor(List<Status> status, long interval, DiscordJar dJar) {
this.statuses = status;
this.interval = interval;
this.dJar = dJar;

new Thread(() -> {
int index = 0;
while (true) {
if (!running) continue;
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

if (!running) continue;
if (statuses == null) continue;
if (statuses.size() == 0) continue;

dJar.setStatus(statuses.get(index));
index++;
if (index >= statuses.size()) index = 0;
}
}, "Status Rotor").start();
}

/**
* Starts the rotor
*/
public void start() {
this.running = true;
}

/**
* Stops the rotor
*/
public void stop() {
this.running = false;
}

public DiscordJar getdJar() {
return dJar;
}

public List<Status> getStatuses() {
return statuses;
}

public long getInterval() {
return interval;
}

public StatusRotor addStatus(Status status) {
statuses.add(status);
return this;
}
}