Search in sources :

Example 6 with IRole

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;
}
Also used : IRole(io.discloader.discloader.entity.guild.IRole) RoleJSON(io.discloader.discloader.network.json.RoleJSON)

Example 7 with IRole

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();
    }
}
Also used : VoiceState(io.discloader.discloader.entity.voice.VoiceState) IGuildChannel(io.discloader.discloader.entity.channel.IGuildChannel) IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) IOException(java.io.IOException) GuildSyncException(io.discloader.discloader.common.exceptions.GuildSyncException) UnauthorizedException(io.discloader.discloader.common.exceptions.UnauthorizedException) MissmatchException(io.discloader.discloader.common.exceptions.MissmatchException) AccountTypeException(io.discloader.discloader.common.exceptions.AccountTypeException) RoleJSON(io.discloader.discloader.network.json.RoleJSON) ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) VoiceRegion(io.discloader.discloader.entity.guild.VoiceRegion) IRole(io.discloader.discloader.entity.guild.IRole) VoiceStateJSON(io.discloader.discloader.network.json.VoiceStateJSON) IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) IGuildTextChannel(io.discloader.discloader.entity.channel.IGuildTextChannel) PresenceJSON(io.discloader.discloader.network.json.PresenceJSON) IGuildVoiceChannel(io.discloader.discloader.entity.channel.IGuildVoiceChannel) MemberJSON(io.discloader.discloader.network.json.MemberJSON) EmojiJSON(io.discloader.discloader.network.json.EmojiJSON)

Example 8 with IRole

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;
}
Also used : IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) CompletableFuture(java.util.concurrent.CompletableFuture) IRole(io.discloader.discloader.entity.guild.IRole) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) GuildMemberUpdateEvent(io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter)

Example 9 with IRole

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;
}
Also used : IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) IEventListener(io.discloader.discloader.common.event.IEventListener) CompletableFuture(java.util.concurrent.CompletableFuture) IRole(io.discloader.discloader.entity.guild.IRole) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) GuildMemberUpdateEvent(io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter)

Example 10 with IRole

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);
}
Also used : IRole(io.discloader.discloader.entity.guild.IRole) IGuild(io.discloader.discloader.entity.guild.IGuild) GuildRoleJSON(io.discloader.discloader.network.json.GuildRoleJSON) GuildRoleCreateEvent(io.discloader.discloader.common.event.guild.role.GuildRoleCreateEvent)

Aggregations

IRole (io.discloader.discloader.entity.guild.IRole)10 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)3 IGuild (io.discloader.discloader.entity.guild.IGuild)3 IGuildMember (io.discloader.discloader.entity.guild.IGuildMember)3 EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)2 GuildMemberUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent)2 GuildRoleCreateEvent (io.discloader.discloader.common.event.guild.role.GuildRoleCreateEvent)2 GuildRoleJSON (io.discloader.discloader.network.json.GuildRoleJSON)2 RoleJSON (io.discloader.discloader.network.json.RoleJSON)2 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 JSONObject (org.json.JSONObject)2 IEventListener (io.discloader.discloader.common.event.IEventListener)1 GuildRoleDeleteEvent (io.discloader.discloader.common.event.guild.role.GuildRoleDeleteEvent)1 GuildRoleUpdateEvent (io.discloader.discloader.common.event.guild.role.GuildRoleUpdateEvent)1 AccountTypeException (io.discloader.discloader.common.exceptions.AccountTypeException)1 GuildSyncException (io.discloader.discloader.common.exceptions.GuildSyncException)1 MissmatchException (io.discloader.discloader.common.exceptions.MissmatchException)1 UnauthorizedException (io.discloader.discloader.common.exceptions.UnauthorizedException)1 Permission (io.discloader.discloader.core.entity.Permission)1