Search in sources :

Example 6 with IGuildMember

use of io.discloader.discloader.entity.guild.IGuildMember in project DiscLoader by R3alCl0ud.

the class Guild method addMember.

/**
 * Method used internally by DiscLoader to make a new {@link GuildMember} object
 * when a member's data is recieved
 *
 * @param data
 *            The member's data
 * @param shouldEmit
 *            if a {@code GuildMemberAddEvent} should be fired by the client
 * @return The {@link GuildMember} that was instantiated.
 */
public IGuildMember addMember(MemberJSON data, boolean shouldEmit) {
    boolean exists = members.containsKey(SnowflakeUtil.parse(data.user.id));
    IGuildMember member = new GuildMember(this, data);
    members.put(member.getID(), member);
    if (!exists && shouldEmit) {
        memberCount++;
        GuildMemberAddEvent event = new GuildMemberAddEvent(member);
        // loader.emit(DLUtil.Events.GUILD_MEMBER_ADD, event);
        loader.emit(event);
    }
    return member;
}
Also used : GuildMemberAddEvent(io.discloader.discloader.common.event.guild.member.GuildMemberAddEvent) IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) IGuildMember(io.discloader.discloader.entity.guild.IGuildMember)

Example 7 with IGuildMember

use of io.discloader.discloader.entity.guild.IGuildMember in project DiscLoader by R3alCl0ud.

the class Guild method ban.

@Override
public CompletableFuture<IGuildMember> ban(IGuildMember member, String reason) throws PermissionsException {
    if (!hasPermission(Permissions.BAN_MEMBERS))
        throw new PermissionsException("");
    CompletableFuture<IGuildMember> future = new CompletableFuture<>();
    loader.rest.request(Methods.PUT, Endpoints.guildBanMember(getID(), member.getID()), new RESTOptions(reason), Void.class).thenAcceptAsync(action -> {
        future.complete(member);
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 8 with IGuildMember

use of io.discloader.discloader.entity.guild.IGuildMember in project DiscLoader by R3alCl0ud.

the class Guild method ban.

@Override
public CompletableFuture<IGuildMember> ban(IGuildMember member) {
    if (!hasPermission(Permissions.BAN_MEMBERS))
        throw new PermissionsException("");
    CompletableFuture<IGuildMember> future = new CompletableFuture<>();
    loader.rest.request(Methods.PUT, Endpoints.guildBanMember(getID(), member.getID()), new RESTOptions(), Void.class).thenAcceptAsync(action -> {
        future.complete(member);
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 9 with IGuildMember

use of io.discloader.discloader.entity.guild.IGuildMember 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 10 with IGuildMember

use of io.discloader.discloader.entity.guild.IGuildMember 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)

Aggregations

IGuildMember (io.discloader.discloader.entity.guild.IGuildMember)13 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)7 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 GuildMemberUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent)4 IGuild (io.discloader.discloader.entity.guild.IGuild)4 MemberJSON (io.discloader.discloader.network.json.MemberJSON)4 EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)3 IRole (io.discloader.discloader.entity.guild.IRole)3 JSONObject (org.json.JSONObject)3 IEventListener (io.discloader.discloader.common.event.IEventListener)2 PresenceJSON (io.discloader.discloader.network.json.PresenceJSON)2 UserUpdateEvent (io.discloader.discloader.common.event.UserUpdateEvent)1 GuildMemberAddEvent (io.discloader.discloader.common.event.guild.member.GuildMemberAddEvent)1 NicknameUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberEvent.NicknameUpdateEvent)1 GuildMemberNicknameUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberNicknameUpdateEvent)1 GuildMemberRemoveEvent (io.discloader.discloader.common.event.guild.member.GuildMemberRemoveEvent)1 GuildMemberRoleAddEvent (io.discloader.discloader.common.event.guild.member.GuildMemberRoleAddEvent)1 GuildMemberRoleRemoveEvent (io.discloader.discloader.common.event.guild.member.GuildMemberRoleRemoveEvent)1 GuildMembersChunkEvent (io.discloader.discloader.common.event.guild.member.GuildMembersChunkEvent)1