Search in sources :

Example 1 with GuildJSON

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

the class Guild method edit.

public CompletableFuture<IGuild> edit(String name, String icon, IGuildVoiceChannel afkChannel) throws IOException {
    if (!isOwner() && !getCurrentMember().getPermissions().hasPermission(Permissions.MANAGE_GUILD)) {
        throw new PermissionsException();
    }
    CompletableFuture<IGuild> future = new CompletableFuture<>();
    String base64 = new String("data:image/jpg;base64," + Base64.encodeBase64String(Files.readAllBytes(Paths.get(icon))));
    JSONObject payload = new JSONObject().put("name", name).put("icon", base64);
    CompletableFuture<GuildJSON> cf = getLoader().rest.request(Methods.PATCH, Endpoints.guild(getID()), new RESTOptions(payload), GuildJSON.class);
    cf.thenAcceptAsync(guildJSON -> {
        IGuild guild = clone();
        guild.setup(guildJSON);
        future.complete(guild);
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) GuildJSON(io.discloader.discloader.network.json.GuildJSON) IGuild(io.discloader.discloader.entity.guild.IGuild) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 2 with GuildJSON

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

the class GuildUpdate method handle.

@Override
public void handle(SocketPacket packet) {
    String d = gson.toJson(packet.d);
    GuildJSON data = gson.fromJson(d, GuildJSON.class);
    IGuild guild = EntityRegistry.getGuildByID(data.id);
    IGuild oldGuild = guild.clone();
    guild.setup(data);
    GuildUpdateEvent event = new GuildUpdateEvent(guild, oldGuild);
    loader.emit(DLUtil.Events.GUILD_UPDATE, event);
    loader.emit(event);
}
Also used : GuildUpdateEvent(io.discloader.discloader.common.event.guild.GuildUpdateEvent) GuildJSON(io.discloader.discloader.network.json.GuildJSON) IGuild(io.discloader.discloader.entity.guild.IGuild)

Example 3 with GuildJSON

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

Example 4 with GuildJSON

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

the class GuildCreate method handle.

@Override
public void handle(SocketPacket packet) {
    Gson gson = new Gson();
    String d = gson.toJson(packet.d);
    GuildJSON data = gson.fromJson(d, GuildJSON.class);
    IGuild guild = null;
    if (EntityRegistry.guildExists(data.id))
        guild = EntityRegistry.getGuildByID(data.id);
    if (guild != null) {
        try {
            if (!guild.isAvailable() && !data.unavailable) {
                guild.setup(data);
                if (shouldEmit()) {
                    GuildCreateEvent event = new GuildCreateEvent(guild);
                    loader.emit(Events.GUILD_CREATE, event);
                    loader.emit(event);
                }
                loader.checkReady();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        // a brand new guild
        guild = EntityRegistry.addGuild(data);
        if (this.shouldEmit()) {
            GuildCreateEvent event = new GuildCreateEvent(guild);
            loader.emit(Events.GUILD_CREATE, event);
            loader.emit(event);
        }
    }
}
Also used : GuildCreateEvent(io.discloader.discloader.common.event.guild.GuildCreateEvent) Gson(com.google.gson.Gson) GuildJSON(io.discloader.discloader.network.json.GuildJSON) IGuild(io.discloader.discloader.entity.guild.IGuild)

Example 5 with GuildJSON

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

the class GuildDelete method handle.

@Override
public void handle(SocketPacket packet) {
    String d = this.gson.toJson(packet.d);
    GuildJSON data = this.gson.fromJson(d, GuildJSON.class);
    IGuild guild = null;
    if (EntityRegistry.guildExists(data.id)) {
        guild = EntityRegistry.getGuildByID(data.id);
        guild.setup(data);
    } else {
        guild = EntityRegistry.addGuild(data);
    }
    if (!guild.isAvailable()) {
        EntityRegistry.removeGuild(guild);
        if (socket.status == Status.READY && loader.ready) {
            GuildDeleteEvent event = new GuildDeleteEvent(guild);
            loader.emit(Events.GUILD_DELETE, event);
            for (IEventListener e : loader.handlers) {
                e.GuildDelete(event);
            }
        }
    }
}
Also used : GuildDeleteEvent(io.discloader.discloader.common.event.guild.GuildDeleteEvent) IEventListener(io.discloader.discloader.common.event.IEventListener) GuildJSON(io.discloader.discloader.network.json.GuildJSON) IGuild(io.discloader.discloader.entity.guild.IGuild)

Aggregations

GuildJSON (io.discloader.discloader.network.json.GuildJSON)6 IGuild (io.discloader.discloader.entity.guild.IGuild)4 Gson (com.google.gson.Gson)1 IEventListener (io.discloader.discloader.common.event.IEventListener)1 GuildCreateEvent (io.discloader.discloader.common.event.guild.GuildCreateEvent)1 GuildDeleteEvent (io.discloader.discloader.common.event.guild.GuildDeleteEvent)1 GuildUpdateEvent (io.discloader.discloader.common.event.guild.GuildUpdateEvent)1 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)1 DLUser (io.discloader.discloader.core.entity.user.DLUser)1 ChannelJSON (io.discloader.discloader.network.json.ChannelJSON)1 ReadyJSON (io.discloader.discloader.network.json.ReadyJSON)1 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 JSONObject (org.json.JSONObject)1