Search in sources :

Example 1 with IGuildChannel

use of io.discloader.discloader.entity.channel.IGuildChannel in project DiscLoader by R3alCl0ud.

the class ChannelCategory method createChannel.

@Override
public CompletableFuture<IGuildChannel> createChannel(String name, ChannelTypes type) {
    CompletableFuture<IGuildChannel> future = new CompletableFuture<>();
    JSONObject data = new JSONObject().put("parent_id", SnowflakeUtil.asString(this)).put("name", name).put("type", type.toInt());
    CompletableFuture<ChannelJSON> cf = loader.rest.request(Methods.POST, Endpoints.guildChannels(getGuild().getID()), new RESTOptions(data), ChannelJSON.class);
    cf.thenAcceptAsync(channelJSON -> {
        if (channelJSON != null) {
            IGuildChannel channel = (IGuildChannel) 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) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IGuildChannel(io.discloader.discloader.entity.channel.IGuildChannel)

Example 2 with IGuildChannel

use of io.discloader.discloader.entity.channel.IGuildChannel in project DiscLoader by R3alCl0ud.

the class ChannelCategory method addChannel.

@Override
public <T extends IGuildChannel> CompletableFuture<T> addChannel(T channel) {
    CompletableFuture<T> future = new CompletableFuture<>();
    JSONObject data = new JSONObject().put("parent_id", SnowflakeUtil.asString(this));
    loader.rest.request(Methods.PATCH, Endpoints.channel(channel.getID()), new RESTOptions(data), ChannelJSON.class).thenAcceptAsync(d -> {
        @SuppressWarnings("unchecked") T newChannel = (T) EntityBuilder.getChannelFactory().buildChannel(d, getLoader(), guild, false);
        future.complete(newChannel);
    }).exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : Endpoints(io.discloader.discloader.network.util.Endpoints) Methods(io.discloader.discloader.network.util.Methods) IOverwrite(io.discloader.discloader.entity.IOverwrite) ChannelTypes(io.discloader.discloader.entity.channel.ChannelTypes) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ChannelPayload(io.discloader.discloader.network.rest.payloads.ChannelPayload) EntityBuilder(io.discloader.discloader.common.registry.EntityBuilder) IGuildTextChannel(io.discloader.discloader.entity.channel.IGuildTextChannel) SnowflakeUtil(io.discloader.discloader.entity.util.SnowflakeUtil) IGuildChannel(io.discloader.discloader.entity.channel.IGuildChannel) JSONObject(org.json.JSONObject) Map(java.util.Map) ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) IGuild(io.discloader.discloader.entity.guild.IGuild) IChannelCategory(io.discloader.discloader.entity.channel.IChannelCategory) IGuildVoiceChannel(io.discloader.discloader.entity.channel.IGuildVoiceChannel) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions)

Example 3 with IGuildChannel

use of io.discloader.discloader.entity.channel.IGuildChannel in project DiscLoader by R3alCl0ud.

the class GuildChannel method setPosition.

@Override
public CompletableFuture<IGuildChannel> setPosition(int position) {
    CompletableFuture<IGuildChannel> future = new CompletableFuture<>();
    List<JSONObject> positions = new ArrayList<>();
    boolean normalize = position < 0;
    List<IGuildChannel> channels;
    switch(getType()) {
        case CATEGORY:
            channels = new ArrayList<>(getGuild().getChannelCategories().values());
            break;
        case TEXT:
            channels = new ArrayList<>(getGuild().getTextChannels().values());
            break;
        case VOICE:
            channels = new ArrayList<>(getGuild().getVoiceChannels().values());
            break;
        default:
            channels = new ArrayList<>();
            break;
    }
    for (IGuildChannel channel : channels) {
        if (channel.getID() == getID()) {
            positions.add(new JSONObject().put("id", SnowflakeUtil.asString(channel)).put("position", position));
        } else if (channel.getPosition() >= position) {
            positions.add(new JSONObject().put("id", SnowflakeUtil.asString(channel)).put("position", channel.getPosition() + 1));
        } else {
            positions.add(new JSONObject().put("id", SnowflakeUtil.asString(channel)).put("position", channel.getPosition()));
        }
        if (channel.getPosition() < 0)
            normalize = true;
    }
    positions.sort((a, b) -> {
        if (a.getInt("position") < b.getInt("position"))
            return -1;
        if (a.getInt("position") > b.getInt("position"))
            return 1;
        return 0;
    });
    if (normalize) {
        for (int i = 0; i < positions.size(); i++) {
            positions.get(i).put("position", i);
        }
    }
    getLoader().addEventListener(new EventListenerAdapter() {

        @Override
        public void GuildChannelUpdate(GuildChannelUpdateEvent e) {
            if (getID() == e.getChannel().getID()) {
                future.complete(e.getChannel());
                getLoader().removeEventListener(this);
            }
        }
    });
    getLoader().rest.request(Methods.PATCH, Endpoints.guildChannels(getGuild().getID()), new RESTOptions(true, null, positions.toString()), Void.class).exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : IGuildChannel(io.discloader.discloader.entity.channel.IGuildChannel) ArrayList(java.util.ArrayList) GuildChannelUpdateEvent(io.discloader.discloader.common.event.channel.GuildChannelUpdateEvent) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter)

Example 4 with IGuildChannel

use of io.discloader.discloader.entity.channel.IGuildChannel in project DiscLoader by R3alCl0ud.

the class Message method deleteAllReactions.

@Override
public CompletableFuture<IMessage> deleteAllReactions() {
    CompletableFuture<IMessage> future = new CompletableFuture<>();
    if (channel instanceof IGuildChannel) {
        if (!((IGuildChannel) channel).permissionsOf(guild.getCurrentMember()).hasPermission(Permissions.MANAGE_MESSAGES)) {
            future.completeExceptionally(new PermissionsException());
            return future;
        }
    }
    CompletableFuture<Void> cf = loader.rest.request(Methods.DELETE, Endpoints.messageReactions(getChannel().getID(), getID()), new RESTOptions(), Void.class);
    cf.thenAcceptAsync(Null -> {
        future.complete(this);
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IGuildChannel(io.discloader.discloader.entity.channel.IGuildChannel) IMessage(io.discloader.discloader.entity.message.IMessage) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 5 with IGuildChannel

use of io.discloader.discloader.entity.channel.IGuildChannel in project DiscLoader by R3alCl0ud.

the class ChannelUpdate method handle.

@Override
public void handle(SocketPacket packet) {
    String d = gson.toJson(packet.d);
    ChannelJSON data = gson.fromJson(d, ChannelJSON.class);
    IGuild guild = null;
    IChannel oldChannel = EntityRegistry.getChannelByID(data.id);
    IChannel channel = null;
    if (data.guild_id != null) {
        guild = EntityRegistry.getGuildByID(data.guild_id);
    }
    channel = EntityRegistry.addChannel(data, loader, guild);
    if (oldChannel instanceof ITextChannel) {
        ITextChannel oitc = (ITextChannel) oldChannel, itc = (ITextChannel) channel;
        for (IMessage message : oitc.getMessages().values()) {
            itc.getMessages().put(message.getID(), message);
        }
    }
    ChannelUpdateEvent event = new ChannelUpdateEvent(channel, oldChannel);
    if (channel instanceof IGuildChannel && oldChannel instanceof IGuildChannel) {
        loader.emit(new GuildChannelUpdateEvent((IGuildChannel) channel, (IGuildChannel) oldChannel));
    }
    loader.emit(Events.CHANNEL_UPDATE, event);
    loader.emit(event);
}
Also used : ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) ITextChannel(io.discloader.discloader.entity.channel.ITextChannel) IChannel(io.discloader.discloader.entity.channel.IChannel) GuildChannelUpdateEvent(io.discloader.discloader.common.event.channel.GuildChannelUpdateEvent) ChannelUpdateEvent(io.discloader.discloader.common.event.channel.ChannelUpdateEvent) IGuildChannel(io.discloader.discloader.entity.channel.IGuildChannel) IMessage(io.discloader.discloader.entity.message.IMessage) IGuild(io.discloader.discloader.entity.guild.IGuild) GuildChannelUpdateEvent(io.discloader.discloader.common.event.channel.GuildChannelUpdateEvent)

Aggregations

IGuildChannel (io.discloader.discloader.entity.channel.IGuildChannel)11 ChannelJSON (io.discloader.discloader.network.json.ChannelJSON)9 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 JSONObject (org.json.JSONObject)7 IGuild (io.discloader.discloader.entity.guild.IGuild)6 GuildChannelUpdateEvent (io.discloader.discloader.common.event.channel.GuildChannelUpdateEvent)5 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)5 EntityBuilder (io.discloader.discloader.common.registry.EntityBuilder)5 IOverwrite (io.discloader.discloader.entity.IOverwrite)5 IChannelCategory (io.discloader.discloader.entity.channel.IChannelCategory)5 SnowflakeUtil (io.discloader.discloader.entity.util.SnowflakeUtil)5 Endpoints (io.discloader.discloader.network.util.Endpoints)5 Methods (io.discloader.discloader.network.util.Methods)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)4 IChannel (io.discloader.discloader.entity.channel.IChannel)4 IGuildMember (io.discloader.discloader.entity.guild.IGuildMember)4 IRole (io.discloader.discloader.entity.guild.IRole)4