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;
}
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);
}
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();
}
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);
}
}
}
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);
}
}
}
}
Aggregations