Search in sources :

Example 6 with ChannelJSON

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

the class Guild method createTextChannel.

@Override
public CompletableFuture<IGuildTextChannel> createTextChannel(String name, IChannelCategory category, IOverwrite... overwrites) {
    CompletableFuture<IGuildTextChannel> future = new CompletableFuture<>();
    if (!hasPermission(Permissions.MANAGE_CHANNELS)) {
        PermissionsException ex = new PermissionsException("Insufficient Permissions");
        future.completeExceptionally(ex);
        // return early
        return future;
    }
    ChannelPayload data = new ChannelPayload(name, ChannelTypes.TEXT, overwrites);
    data.setParent(category);
    CompletableFuture<ChannelJSON> cf = getLoader().rest.request(Methods.POST, Endpoints.guildChannels(getID()), new RESTOptions(data), ChannelJSON.class);
    cf.thenAcceptAsync(channelJSON -> {
        IGuildTextChannel channel = (IGuildTextChannel) EntityBuilder.getChannelFactory().buildChannel(channelJSON, getLoader(), this, 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) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) ChannelPayload(io.discloader.discloader.network.rest.payloads.ChannelPayload) IGuildTextChannel(io.discloader.discloader.entity.channel.IGuildTextChannel) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 7 with ChannelJSON

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

the class ChannelCreate method handle.

@Override
public void handle(SocketPacket packet) {
    String d = gson.toJson(packet.d);
    ChannelJSON data = gson.fromJson(d, ChannelJSON.class);
    IGuild guild = EntityRegistry.getGuildByID(data.guild_id);
    IChannel channel = EntityRegistry.addChannel(data, loader, guild);
    ChannelCreateEvent event = new ChannelCreateEvent(channel);
    loader.emit(Events.CHANNEL_CREATE, event);
    loader.emit(event);
}
Also used : ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) IChannel(io.discloader.discloader.entity.channel.IChannel) ChannelCreateEvent(io.discloader.discloader.common.event.channel.ChannelCreateEvent) IGuild(io.discloader.discloader.entity.guild.IGuild)

Example 8 with ChannelJSON

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

the class ChannelDelete 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 channel = null;
    if (data.guild_id != null) {
        guild = EntityRegistry.getGuildByID(data.guild_id);
        channel = EntityRegistry.addChannel(data, loader, guild);
    } else {
        channel = EntityRegistry.addChannel(data, loader);
    }
    switch(channel.getType()) {
        case TEXT:
            guild.getTextChannels().remove(channel.getID());
            break;
        case VOICE:
            guild.getVoiceChannels().remove(channel.getID());
            break;
        default:
            EntityRegistry.removeChannel(channel);
            break;
    }
    ChannelDeleteEvent event = new ChannelDeleteEvent(channel);
    loader.emit(Events.CHANNEL_DELETE, event);
    loader.emit(event);
}
Also used : ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) IChannel(io.discloader.discloader.entity.channel.IChannel) ChannelDeleteEvent(io.discloader.discloader.common.event.channel.ChannelDeleteEvent) IGuild(io.discloader.discloader.entity.guild.IGuild)

Example 9 with ChannelJSON

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

Example 10 with ChannelJSON

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

the class Ready method handle.

@Override
public void handle(SocketPacket packet) {
    String d = gson.toJson(packet.d);
    ReadyJSON readyJSON = gson.fromJson(d, ReadyJSON.class);
    // set session id first just incase some screws up
    socket.sessionID = readyJSON.session_id;
    // setup the Loaders user object
    try {
        loader.user = new DLUser(EntityRegistry.addUser(readyJSON.user));
        /* 
			 * need to make sure that we don't already have the 'Bot ' prefix on the token
			 * otherwise bots fail to work if they've had to start a completely new session after disconnecting
			 */
        if (loader.user.isBot() && !loader.token.startsWith("Bot ")) {
            loader.token = "Bot " + loader.token;
        }
        // load the guilds
        for (GuildJSON guild : readyJSON.guilds) {
            EntityRegistry.addGuild(guild);
        }
        // load the private channels
        for (ChannelJSON data : readyJSON.private_channels) {
            EntityRegistry.addChannel(data, null);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // check if the loader is ready to rock & roll
    loader.socket.setRetries(0);
    loader.checkReady();
}
Also used : ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) DLUser(io.discloader.discloader.core.entity.user.DLUser) ReadyJSON(io.discloader.discloader.network.json.ReadyJSON) GuildJSON(io.discloader.discloader.network.json.GuildJSON)

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