Search in sources :

Example 1 with GuildChannelUpdateEvent

use of io.discloader.discloader.common.event.channel.GuildChannelUpdateEvent 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 2 with GuildChannelUpdateEvent

use of io.discloader.discloader.common.event.channel.GuildChannelUpdateEvent 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

GuildChannelUpdateEvent (io.discloader.discloader.common.event.channel.GuildChannelUpdateEvent)2 IGuildChannel (io.discloader.discloader.entity.channel.IGuildChannel)2 EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)1 ChannelUpdateEvent (io.discloader.discloader.common.event.channel.ChannelUpdateEvent)1 IChannel (io.discloader.discloader.entity.channel.IChannel)1 ITextChannel (io.discloader.discloader.entity.channel.ITextChannel)1 IGuild (io.discloader.discloader.entity.guild.IGuild)1 IMessage (io.discloader.discloader.entity.message.IMessage)1 ChannelJSON (io.discloader.discloader.network.json.ChannelJSON)1 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)1 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 JSONObject (org.json.JSONObject)1