Search in sources :

Example 11 with ChannelJSON

use of io.discloader.discloader.network.json.ChannelJSON in project DiscLoader by R3alCl0ud.

the class CreateTextChannel method complete.

public void complete(String packet, Throwable ex) {
    if (ex != null) {
        future.completeExceptionally(ex);
        return;
    }
    ChannelJSON data = gson.fromJson(packet, ChannelJSON.class);
    future.complete((IGuildTextChannel) EntityRegistry.addChannel(data, loader));
}
Also used : ChannelJSON(io.discloader.discloader.network.json.ChannelJSON)

Example 12 with ChannelJSON

use of io.discloader.discloader.network.json.ChannelJSON in project DiscLoader by R3alCl0ud.

the class TextChannel method edit.

@Override
public CompletableFuture<IGuildTextChannel> edit(String name, int position) {
    if (!this.permissionsOf(guild.getCurrentMember()).hasAny(Permissions.MANAGE_CHANNELS, Permissions.ADMINISTRATOR) && !guild.isOwner()) {
        throw new PermissionsException("Insufficient Permissions");
    }
    if (name.length() < 2 || name.length() > 100) {
        throw new RuntimeException("Name.length() out of bounds [2-100]");
    }
    CompletableFuture<IGuildTextChannel> future = new CompletableFuture<>();
    JSONObject payload = new JSONObject().put("name", sanitizeChannelName(name)).put("position", position);
    CompletableFuture<ChannelJSON> cf = loader.rest.request(Methods.PATCH, Endpoints.channel(getID()), new RESTOptions(payload), ChannelJSON.class);
    cf.thenAcceptAsync(data -> {
        IGuildTextChannel channel = (IGuildTextChannel) EntityBuilder.getChannelFactory().buildChannel(data, getLoader(), getGuild(), false);
        future.complete(channel);
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IGuildTextChannel(io.discloader.discloader.entity.channel.IGuildTextChannel) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 13 with ChannelJSON

use of io.discloader.discloader.network.json.ChannelJSON in project DiscLoader by R3alCl0ud.

the class VoiceChannel method edit.

/**
 * Changes the channels settings
 *
 * @param name
 *            The new name for the channel
 * @param position
 *            The new position for the channel
 * @param bitrate
 *            The new {@link #bitrate}
 * @param userLimit
 *            The new {@link #userLimit}
 * @return A Future that completes with a voice channel if successful
 */
public CompletableFuture<IGuildVoiceChannel> edit(String name, int position, int bitrate, int userLimit) {
    CompletableFuture<IGuildVoiceChannel> future = new CompletableFuture<>();
    EditChannel d = new EditChannel(name, null, position, bitrate, userLimit);
    CompletableFuture<ChannelJSON> cf = loader.rest.request(Methods.PATCH, Endpoints.channel(getID()), new RESTOptions(d), ChannelJSON.class);
    cf.thenAcceptAsync(channelJSON -> {
        if (channelJSON != null) {
            IGuildVoiceChannel channel = (IGuildVoiceChannel) EntityBuilder.getChannelFactory().buildChannel(channelJSON, getLoader(), getGuild(), false);
            if (channel != null)
                future.complete(channel);
        }
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) EditChannel(io.discloader.discloader.entity.sendable.EditChannel) IGuildVoiceChannel(io.discloader.discloader.entity.channel.IGuildVoiceChannel)

Example 14 with ChannelJSON

use of io.discloader.discloader.network.json.ChannelJSON in project DiscLoader by R3alCl0ud.

the class Guild method createVoiceChannel.

@Override
public CompletableFuture<IGuildVoiceChannel> createVoiceChannel(String name, int bitRate, int userLimit, IChannelCategory category, IOverwrite... overwrites) {
    CompletableFuture<IGuildVoiceChannel> future = new CompletableFuture<>();
    if (!hasPermission(Permissions.MANAGE_CHANNELS)) {
        PermissionsException ex = new PermissionsException("Insufficient Permissions");
        future.completeExceptionally(ex);
        // return early
        return future;
    }
    // normalize
    bitRate = Math.max(8, Math.min(96, bitRate)) * 1000;
    // normalize
    userLimit = Math.max(0, Math.min(99, userLimit));
    ChannelPayload data = new ChannelPayload(name, bitRate, userLimit, overwrites);
    if (category != null && getChannelCategoryByID(category.getID()) != null) {
        data.setParent(category);
    }
    CompletableFuture<ChannelJSON> cf = loader.rest.request(Methods.POST, Endpoints.guildChannels(getID()), new RESTOptions(data), ChannelJSON.class);
    cf.thenAcceptAsync(channelJSON -> {
        if (channelJSON != null) {
            IGuildVoiceChannel channel = (IGuildVoiceChannel) EntityBuilder.getChannelFactory().buildChannel(channelJSON, getLoader(), this, false);
            if (channel != null)
                future.complete(channel);
        }
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) ChannelPayload(io.discloader.discloader.network.rest.payloads.ChannelPayload) IGuildVoiceChannel(io.discloader.discloader.entity.channel.IGuildVoiceChannel) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 15 with ChannelJSON

use of io.discloader.discloader.network.json.ChannelJSON 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)

Aggregations

ChannelJSON (io.discloader.discloader.network.json.ChannelJSON)21 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)13 CompletableFuture (java.util.concurrent.CompletableFuture)13 IGuildTextChannel (io.discloader.discloader.entity.channel.IGuildTextChannel)8 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)7 JSONObject (org.json.JSONObject)7 IGuildVoiceChannel (io.discloader.discloader.entity.channel.IGuildVoiceChannel)5 ChannelPayload (io.discloader.discloader.network.rest.payloads.ChannelPayload)5 IGuildChannel (io.discloader.discloader.entity.channel.IGuildChannel)4 IChannel (io.discloader.discloader.entity.channel.IChannel)3 IGuild (io.discloader.discloader.entity.guild.IGuild)3 ChannelCreateEvent (io.discloader.discloader.common.event.channel.ChannelCreateEvent)1 ChannelDeleteEvent (io.discloader.discloader.common.event.channel.ChannelDeleteEvent)1 ChannelUpdateEvent (io.discloader.discloader.common.event.channel.ChannelUpdateEvent)1 GuildChannelUpdateEvent (io.discloader.discloader.common.event.channel.GuildChannelUpdateEvent)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 DLUser (io.discloader.discloader.core.entity.user.DLUser)1