use of de.btobastian.javacord.entities.UserStatus in project Javacord by BtoBastian.
the class PresenceUpdateHandler method handle.
@Override
public void handle(JSONObject packet) {
final User user = api.getOrCreateUser(packet.getJSONObject("user"));
if (user == null) {
return;
}
Server server = null;
if (packet.has("guild_id")) {
server = api.getServerById(packet.getString("guild_id"));
}
if (server != null) {
// add user to server
((ImplServer) server).addMember(user);
}
if (server != null && packet.has("roles")) {
JSONArray roleIds = packet.getJSONArray("roles");
for (int i = 0; i < roleIds.length(); i++) {
// add user to the role
((ImplRole) server.getRoleById(roleIds.getString(i))).addUserNoUpdate(user);
}
}
// check status
if (packet.has("status")) {
UserStatus status = UserStatus.fromString(packet.getString("status"));
final UserStatus oldStatus = user.getStatus();
((ImplUser) user).setStatus(status);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<UserChangeStatusListener> listeners = api.getListeners(UserChangeStatusListener.class);
synchronized (listeners) {
for (UserChangeStatusListener listener : listeners) {
try {
listener.onUserChangeStatus(api, user, oldStatus);
} catch (Throwable t) {
logger.warn("Uncaught exception in UserChangeStatusListener!", t);
}
}
}
}
});
}
// check username
if (packet.getJSONObject("user").has("username")) {
String name = packet.getJSONObject("user").getString("username");
if (!user.getName().equals(name)) {
final String oldName = user.getName();
((ImplUser) user).setName(name);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<UserChangeNameListener> listeners = api.getListeners(UserChangeNameListener.class);
synchronized (listeners) {
for (UserChangeNameListener listener : listeners) {
try {
listener.onUserChangeName(api, user, oldName);
} catch (Throwable t) {
logger.warn("Uncaught exception in UserChangeNameListener!", t);
}
}
}
}
});
}
}
// check game
if (packet.has("game")) {
String game;
if (!packet.isNull("game") && packet.getJSONObject("game").has("name") && !packet.getJSONObject("game").isNull("name")) {
game = packet.getJSONObject("game").get("name").toString();
} else {
game = null;
}
String oldGame = user.getGame();
if ((game == null && oldGame != null) || (game != null && oldGame == null) || (game != null && !game.equals(oldGame))) {
((ImplUser) user).setGame(game);
List<UserChangeGameListener> listeners = api.getListeners(UserChangeGameListener.class);
synchronized (listeners) {
for (UserChangeGameListener listener : listeners) {
try {
listener.onUserChangeGame(api, user, oldGame);
} catch (Throwable t) {
logger.warn("Uncaught exception in UserChangeGameListener!", t);
}
}
}
}
}
}
Aggregations