use of de.btobastian.javacord.entities.permissions.Role in project Javacord by BtoBastian.
the class ChannelUpdateHandler method handleServerVoiceChannel.
/**
* Handles the server voice channels.
*
* @param packet The packet (the "d"-object).
* @param server The server of the channel.
*/
private void handleServerVoiceChannel(JSONObject packet, Server server) {
final VoiceChannel channel = server.getVoiceChannelById(packet.getString("id"));
if (channel == null) {
// no channel with the given id was found
return;
}
String name = packet.getString("name");
if (!channel.getName().equals(name)) {
final String oldName = channel.getName();
((ImplVoiceChannel) channel).setName(name);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<VoiceChannelChangeNameListener> listeners = api.getListeners(VoiceChannelChangeNameListener.class);
synchronized (listeners) {
for (VoiceChannelChangeNameListener listener : listeners) {
try {
listener.onVoiceChannelChangeName(api, channel, oldName);
} catch (Throwable t) {
logger.warn("Uncaught exception in VoiceChannelChangeNameListener!", t);
}
}
}
}
});
}
int position = packet.getInt("position");
if (channel.getPosition() != position) {
final int oldPosition = channel.getPosition();
((ImplVoiceChannel) channel).setPosition(position);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<VoiceChannelChangePositionListener> listeners = api.getListeners(VoiceChannelChangePositionListener.class);
synchronized (listeners) {
for (VoiceChannelChangePositionListener listener : listeners) {
try {
listener.onVoiceChannelChangePosition(api, channel, oldPosition);
} catch (Throwable t) {
logger.warn("Uncaught exception in VoiceChannelChangePositionListener!", t);
}
}
}
}
});
}
JSONArray permissionOverwrites = packet.getJSONArray("permission_overwrites");
for (int i = 0; i < permissionOverwrites.length(); i++) {
JSONObject permissionOverwrite = permissionOverwrites.getJSONObject(i);
int allow = permissionOverwrite.getInt("allow");
int deny = permissionOverwrite.getInt("deny");
String id = permissionOverwrite.getString("id");
String type = permissionOverwrite.getString("type");
// permissions overwritten by users
if (type.equals("member")) {
final User user;
try {
user = api.getUserById(id).get();
} catch (InterruptedException | ExecutionException e) {
continue;
}
ImplPermissions permissions = new ImplPermissions(allow, deny);
final Permissions oldPermissions = channel.getOverwrittenPermissions(user);
if (!oldPermissions.equals(permissions)) {
((ImplVoiceChannel) channel).setOverwrittenPermissions(user, permissions);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<UserChangeOverwrittenPermissionsListener> listeners = api.getListeners(UserChangeOverwrittenPermissionsListener.class);
synchronized (listeners) {
for (UserChangeOverwrittenPermissionsListener listener : listeners) {
try {
listener.onUserChangeOverwrittenPermissions(api, user, channel, oldPermissions);
} catch (Throwable t) {
logger.warn("Uncaught exception in UserChangeOverwrittenPermissionsListener!", t);
}
}
}
}
});
}
}
// permissions overwritten by roles
if (type.equals("role")) {
final Role role = channel.getServer().getRoleById(id);
ImplPermissions permissions = new ImplPermissions(allow, deny);
final Permissions oldPermissions = role.getOverwrittenPermissions(channel);
if (!permissions.equals(oldPermissions)) {
((ImplRole) role).setOverwrittenPermissions(channel, permissions);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<RoleChangeOverwrittenPermissionsListener> listeners = api.getListeners(RoleChangeOverwrittenPermissionsListener.class);
synchronized (listeners) {
for (RoleChangeOverwrittenPermissionsListener listener : listeners) {
try {
listener.onRoleChangeOverwrittenPermissions(api, role, channel, oldPermissions);
} catch (Throwable t) {
logger.warn("Uncaught exception in RoleChangeOverwrittenPermissionsListener!", t);
}
}
}
}
});
}
}
}
String parentId = packet.isNull("parent_id") ? null : packet.getString("parent_id");
if (!Objects.deepEquals(channel.getParentId(), parentId)) {
((ImplVoiceChannel) channel).setParentId(parentId);
}
}
use of de.btobastian.javacord.entities.permissions.Role in project Javacord by BtoBastian.
the class ImplServer method addMembers.
/**
* Adds members to the server.
*
* @param members An array of guild member objects.
*/
public void addMembers(JSONArray members) {
for (int i = 0; i < members.length(); i++) {
User member = api.getOrCreateUser(members.getJSONObject(i).getJSONObject("user"));
if (members.getJSONObject(i).has("nick") && !members.getJSONObject(i).isNull("nick")) {
nicknames.put(member.getId(), members.getJSONObject(i).getString("nick"));
}
this.members.put(member.getId(), member);
JSONArray memberRoles = members.getJSONObject(i).getJSONArray("roles");
for (int j = 0; j < memberRoles.length(); j++) {
Role role = getRoleById(memberRoles.getString(j));
if (role != null) {
((ImplRole) role).addUserNoUpdate(member);
}
}
}
}
use of de.btobastian.javacord.entities.permissions.Role in project Javacord by BtoBastian.
the class ImplUser method getRoles.
@Override
public Collection<Role> getRoles(Server server) {
Collection<Role> userRoles = new ArrayList<>();
Iterator<Role> rolesIterator = server.getRoles().iterator();
while (rolesIterator.hasNext()) {
Role role = rolesIterator.next();
if (role.getUsers().contains(this)) {
userRoles.add(role);
}
}
return userRoles;
}
use of de.btobastian.javacord.entities.permissions.Role in project Javacord by BtoBastian.
the class GuildRoleDeleteHandler method handle.
@Override
public void handle(JSONObject packet) {
String guildId = packet.getString("guild_id");
String roleId = packet.getString("role_id");
Server server = api.getServerById(guildId);
final Role role = server.getRoleById(roleId);
if (role == null) {
return;
}
((ImplServer) server).removeRole(role);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<RoleDeleteListener> listeners = api.getListeners(RoleDeleteListener.class);
synchronized (listeners) {
for (RoleDeleteListener listener : listeners) {
try {
listener.onRoleDelete(api, role);
} catch (Throwable t) {
logger.warn("Uncaught exception in RoleDeleteListener!", t);
}
}
}
}
});
}
use of de.btobastian.javacord.entities.permissions.Role in project Javacord by BtoBastian.
the class GuildRoleUpdateHandler method handle.
@Override
public void handle(JSONObject packet) {
String guildId = packet.getString("guild_id");
JSONObject roleJson = packet.getJSONObject("role");
Server server = api.getServerById(guildId);
final ImplRole role = (ImplRole) server.getRoleById(roleJson.getString("id"));
String name = roleJson.getString("name");
if (!role.getName().equals(name)) {
final String oldName = role.getName();
role.setName(name);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<RoleChangeNameListener> listeners = api.getListeners(RoleChangeNameListener.class);
synchronized (listeners) {
for (RoleChangeNameListener listener : listeners) {
try {
listener.onRoleChangeName(api, role, oldName);
} catch (Throwable t) {
logger.warn("Uncaught exception in RoleChangeNameListener!", t);
}
}
}
}
});
}
Permissions permissions = new ImplPermissions(roleJson.getInt("permissions"));
if (!role.getPermissions().equals(permissions)) {
final Permissions oldPermissions = role.getPermissions();
role.setPermissions((ImplPermissions) permissions);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<RoleChangePermissionsListener> listeners = api.getListeners(RoleChangePermissionsListener.class);
synchronized (listeners) {
for (RoleChangePermissionsListener listener : listeners) {
try {
listener.onRoleChangePermissions(api, role, oldPermissions);
} catch (Throwable t) {
logger.warn("Uncaught exception in RoleChangePermissionsListener!", t);
}
}
}
}
});
}
Color color = new Color(roleJson.getInt("color"));
if (role.getColor().getRGB() != color.getRGB()) {
final Color oldColor = role.getColor();
role.setColor(color);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<RoleChangeColorListener> listeners = api.getListeners(RoleChangeColorListener.class);
synchronized (listeners) {
for (RoleChangeColorListener listener : listeners) {
try {
listener.onRoleChangeColor(api, role, oldColor);
} catch (Throwable t) {
logger.warn("Uncaught exception in RoleChangeColorListener!", t);
}
}
}
}
});
}
if (role.getHoist() != roleJson.getBoolean("hoist")) {
role.setHoist(!role.getHoist());
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<RoleChangeHoistListener> listeners = api.getListeners(RoleChangeHoistListener.class);
synchronized (listeners) {
for (RoleChangeHoistListener listener : listeners) {
try {
listener.onRoleChangeHoist(api, role, !role.getHoist());
} catch (Throwable t) {
logger.warn("Uncaught exception in RoleChangeHoistListener!", t);
}
}
}
}
});
}
synchronized (Role.class) {
// we don't want strange positions
int position = roleJson.getInt("position");
if (role.getPosition() != position) {
final int oldPosition = role.getPosition();
role.setPosition(position);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<RoleChangePositionListener> listeners = api.getListeners(RoleChangePositionListener.class);
synchronized (listeners) {
for (RoleChangePositionListener listener : listeners) {
try {
listener.onRoleChangePosition(api, role, oldPosition);
} catch (Throwable t) {
logger.warn("Uncaught exception in RoleChangePositionListener!", t);
}
}
}
}
});
}
}
}
Aggregations