use of io.discloader.discloader.entity.guild.IRole in project DiscLoader by R3alCl0ud.
the class CreateRole method complete.
public void complete(String s, Throwable ex) {
if (ex != null) {
future.completeExceptionally(ex.getCause());
return;
}
IRole role = gfac.buildRole(guild, gson.fromJson(s, RoleJSON.class));
future.complete(role);
return;
}
use of io.discloader.discloader.entity.guild.IRole in project DiscLoader by R3alCl0ud.
the class Guild method setup.
/**
* Sets up a guild with data from the gateway
*
* @param data
* The guild's data
*/
@Override
public void setup(GuildJSON data) {
try {
name = data.name;
icon = data.icon != null ? data.icon : null;
iconURL = icon != null ? Endpoints.guildIcon(getID(), icon) : null;
ownerID = SnowflakeUtil.parse(data.owner_id);
memberCount = data.member_count;
voiceRegion = new VoiceRegion(data.region);
splashHash = data.splash;
if (data.roles.length > 0) {
roles.clear();
for (RoleJSON role : data.roles) {
IRole r = gfac.buildRole(this, role);
roles.put(r.getID(), r);
}
}
if (data.members != null && data.members.length > 0) {
members.clear();
for (MemberJSON member : data.members) {
IGuildMember m = gfac.buildMember(this, member);
members.put(m.getID(), m);
}
}
if (data.channels != null && data.channels.length > 0) {
for (ChannelJSON channelData : data.channels) {
IGuildChannel chan = (IGuildChannel) EntityRegistry.addChannel(channelData, getLoader(), this);
if (chan instanceof IGuildTextChannel)
textChannels.put(chan.getID(), (IGuildTextChannel) chan);
else if (chan instanceof IGuildVoiceChannel)
voiceChannels.put(chan.getID(), (IGuildVoiceChannel) chan);
}
}
if (data.presences != null && data.presences.length > 0) {
presences.clear();
for (PresenceJSON presence : data.presences) {
this.setPresence(presence);
}
}
if (data.emojis != null && data.emojis.length > 0) {
this.guildEmojis.clear();
for (EmojiJSON e : data.emojis) {
this.guildEmojis.put(SnowflakeUtil.parse(e.id), new GuildEmoji(e, this));
}
}
if (data.voice_states != null && data.voice_states.length > 0) {
this.rawStates.clear();
for (VoiceStateJSON v : data.voice_states) {
this.rawStates.put(SnowflakeUtil.parse(v.user_id), new VoiceState(v, this));
}
}
this.available = data.unavailable == true ? false : true;
} catch (Exception e) {
e.printStackTrace();
}
}
use of io.discloader.discloader.entity.guild.IRole in project DiscLoader by R3alCl0ud.
the class GuildMember method giveRole.
/**
* Gives a member a new role
*
* @param roles
* The roles to give to the member
* @return A CompletableFuture that completes with {@code this} if successful
* @throws PermissionsException
* thrown if a role with a higher position than the current user's
* highest role is attempted to be given to the member. Also thrown
* if the current user doesn't have the MANAGE_ROLE permission.
*/
@Override
public CompletableFuture<IGuildMember> giveRole(IRole... roles) {
CompletableFuture<IGuildMember> future = new CompletableFuture<>();
if (!guild.hasPermission(Permissions.MANAGE_ROLES)) {
future.completeExceptionally(new PermissionsException("Insufficient Permissions"));
return future;
}
for (IRole role : roles) {
if (role == null)
continue;
if (!guild.isOwner() && role.getPosition() >= guild.getCurrentMember().getHighestRole().getPosition()) {
future.completeExceptionally(new PermissionsException("Can not assign higher role"));
return future;
// throw ;
}
}
List<IRole> rls = mergeRoles(roles);
String[] ids = new String[rls.size()];
for (int i = 0; i < ids.length; i++) {
ids[i] = SnowflakeUtil.asString(rls.get(i));
}
JSONObject payload = new JSONObject().put("roles", ids);
System.out.println(payload);
CompletableFuture<Void> vcf = getLoader().rest.request(Methods.PATCH, Endpoints.guildMember(getGuild().getID(), getID()), new RESTOptions(payload), Void.class);
vcf.thenAcceptAsync(v -> {
getLoader().addEventListener(new EventListenerAdapter() {
public void GuildMemberUpdate(GuildMemberUpdateEvent e) {
future.complete(e.getMember());
getLoader().removeEventListener(this);
}
});
});
vcf.exceptionally(ex -> {
future.completeExceptionally(ex);
return null;
});
// }
return future;
}
use of io.discloader.discloader.entity.guild.IRole in project DiscLoader by R3alCl0ud.
the class GuildMember method takeRole.
@Override
public CompletableFuture<IGuildMember> takeRole(IRole... roles) {
if (!guild.hasPermission(Permissions.MANAGE_ROLES)) {
throw new PermissionsException("Insuccficient Permissions");
}
List<IRole> rls = getRoles();
for (IRole role : roles) {
if (!guild.isOwner() && role.getPosition() >= guild.getCurrentMember().getHighestRole().getPosition()) {
throw new PermissionsException("Cannot take away roles higher than your's");
}
if (hasRole(role)) {
rls.remove(role);
}
}
CompletableFuture<IGuildMember> future = new CompletableFuture<>();
String[] ids = new String[rls.size()];
for (int i = 0; i < ids.length; i++) {
ids[i] = SnowflakeUtil.asString(rls.get(i));
}
JSONObject payload = new JSONObject().put("roles", ids);
CompletableFuture<Void> tcf = getLoader().rest.request(Methods.PATCH, Endpoints.guildMember(getGuild().getID(), getID()), new RESTOptions(payload), Void.class);
IEventListener iel = new EventListenerAdapter() {
public void GuildMemberUpdate(GuildMemberUpdateEvent e) {
if (e.getMember().getID() == getID()) {
future.complete(e.getMember());
getLoader().removeEventListener(this);
}
}
};
getLoader().addEventListener(iel);
tcf.exceptionally(ex -> {
getLoader().removeEventListener(iel);
future.completeExceptionally(ex);
return null;
});
return future;
}
use of io.discloader.discloader.entity.guild.IRole in project DiscLoader by R3alCl0ud.
the class RoleCreate method handle.
@Override
public void handle(SocketPacket packet) {
String d = gson.toJson(packet.d);
GuildRoleJSON data = gson.fromJson(d, GuildRoleJSON.class);
IGuild guild = EntityRegistry.getGuildByID(data.guild_id);
IRole role = guild.addRole(gfac.buildRole(guild, data.role));
GuildRoleCreateEvent event = new GuildRoleCreateEvent(role);
loader.emit(DLUtil.Events.GUILD_ROLE_CREATE, event);
loader.emit(event);
}
Aggregations