From 89b069d0022790617f0f32d1a9273d4b07fd35e1 Mon Sep 17 00:00:00 2001 From: Perry Berman Date: Tue, 7 Nov 2017 09:44:25 -0700 Subject: [PATCH] #29 complete `ChannelCategory`s have been implemented into DiscLoader --- .../discloader/common/DiscLoader.java | 74 +++++++++------ .../discloader/common/ShardManager.java | 13 ++- .../common/registry/EntityRegistry.java | 70 ++++++++++---- .../registry/factory/ChannelFactory.java | 9 +- .../core/entity/channel/ChannelCategory.java | 31 +++++-- .../core/entity/channel/GuildChannel.java | 5 +- .../discloader/core/entity/guild/Guild.java | 92 ++++++++----------- .../entity/channel/IGuildChannel.java | 13 ++- .../gateway/packets/ChannelCreate.java | 2 +- .../gateway/packets/ChannelDelete.java | 4 +- .../gateway/packets/ChannelUpdate.java | 4 +- .../discloader/network/rest/RESTManager.java | 23 +++-- .../rest/actions/channel/CreateDMChannel.java | 2 +- .../rest/actions/guild/CreateTextChannel.java | 2 +- .../actions/guild/CreateVoiceChannel.java | 2 +- 15 files changed, 203 insertions(+), 143 deletions(-) diff --git a/src/main/java/io/discloader/discloader/common/DiscLoader.java b/src/main/java/io/discloader/discloader/common/DiscLoader.java index a94a844e2..6a2335d58 100644 --- a/src/main/java/io/discloader/discloader/common/DiscLoader.java +++ b/src/main/java/io/discloader/discloader/common/DiscLoader.java @@ -34,6 +34,7 @@ import io.discloader.discloader.entity.guild.IGuild; import io.discloader.discloader.entity.invite.IInvite; import io.discloader.discloader.entity.sendable.Packet; +import io.discloader.discloader.entity.util.SnowflakeUtil; import io.discloader.discloader.network.gateway.Gateway; import io.discloader.discloader.network.json.GatewayJSON; import io.discloader.discloader.network.rest.RESTManager; @@ -130,8 +131,7 @@ public static DiscLoader getDiscLoader() { // public HashMap privateChannels; /** - * A HashMap of the client's cached TextChannels. Indexed by - * {@link Channel#id}. + * A HashMap of the client's cached TextChannels. Indexed by {@link Channel#id}. * * @see Channel * @see TextChannel @@ -209,7 +209,8 @@ public DiscLoader() { * } * * - * @param options Options to be passed to the client + * @param options + * Options to be passed to the client */ public DiscLoader(DLOptions options) { this(options.shard, options.shards); @@ -251,16 +252,20 @@ public DiscLoader(DLOptions options) { * } * * - * @param shards The total number of shards - * @param shard The number id of this shard + * @param shards + * The total number of shards + * @param shard + * The number id of this shard * @author Perry Berman * @since 0.0.3 */ public DiscLoader(int shard, int shards) { this.shards = shards; this.shardid = shard; - if (!(System.out instanceof DLPrintStream)) System.setOut(new DLPrintStream(System.out, LOG)); - if (!(System.err instanceof DLErrorStream)) System.setErr(new DLErrorStream(System.err, LOG)); + if (!(System.out instanceof DLPrintStream)) + System.setOut(new DLPrintStream(System.out, LOG)); + if (!(System.err instanceof DLErrorStream)) + System.setErr(new DLErrorStream(System.err, LOG)); System.setProperty("http.agent", "DiscLoader"); socket = new Gateway(this); rest = new RESTManager(this); @@ -278,8 +283,9 @@ public DiscLoader(Shard shard) { this.shard = shard; } - public void addEventHandler(IEventListener e) { + public DiscLoader addEventHandler(IEventListener e) { eventManager.addEventHandler(e); + return this; } public void checkReady() { @@ -373,7 +379,7 @@ public boolean isGuildSyncing(IGuild guild) { } public boolean isGuildSyncing(String guildID) { - return syncingGuilds.containsKey(guildID); + return syncingGuilds.containsKey(SnowflakeUtil.parse(guildID)); } /** @@ -381,20 +387,19 @@ public boolean isGuildSyncing(String guildID) { * You can use {@link DLOptions} to set the token when you create a new * DiscLoader object. * - * @return A CompletableFuture that completes with {@code this} if - * successful. + * @return A CompletableFuture that completes with {@code this} if successful. */ public CompletableFuture login() { return login(token); } /** - * Connects the current instance of the {@link DiscLoader loader} into - * Discord's gateway servers + * Connects the current instance of the {@link DiscLoader loader} into Discord's + * gateway servers * - * @param token your API token - * @return A CompletableFuture that completes with {@code this} if - * successful. + * @param token + * your API token + * @return A CompletableFuture that completes with {@code this} if successful. */ public CompletableFuture login(String token) { LOG.info("Attempting to login"); @@ -448,16 +453,16 @@ public DiscLoader onEvent(Class cls, Consumer consumer) { return this; } - public void removeEventHandler(IEventListener e) { + public DiscLoader removeEventHandler(IEventListener e) { eventManager.removeEventHandler(e); + return this; } /** - * This method gets called in {@link #login(String)} before attempting to - * login now.
- *
- * This method must be called to start DiscLoader. + * This method gets called in {@link #login(String)} before attempting to login + * now.
*
+ * This method must be called to start DiscLoader.
* As it begins the setup process for DiscLoader to be able to function * correctly.
* It will crash otherwise
@@ -495,7 +500,8 @@ public void removeEventHandler(IEventListener e) { /** * Sets the clients options; * - * @param options The new options to use + * @param options + * The new options to use * @return this. */ public DiscLoader setOptions(DLOptions options) { @@ -512,16 +518,19 @@ public DiscLoader setOptions(DLOptions options) { /** * Syncs guilds to client if the logged in user is not a bot * - * @param guilds the guilds to sync + * @param guilds + * the guilds to sync * @throws GuildSyncException * @throws AccountTypeException */ public void syncGuilds(IGuild... guilds) throws GuildSyncException, AccountTypeException { - if (user.bot) throw new AccountTypeException("Only user accounts are allowed to sync guilds"); + if (user.bot) + throw new AccountTypeException("Only user accounts are allowed to sync guilds"); String[] ids = new String[guilds.length]; for (int i = 0; i < guilds.length; i++) { - if (isGuildSyncing(guilds[i])) throw new GuildSyncException("Cannot syncing a guild that is currently syncing"); + if (isGuildSyncing(guilds[i])) + throw new GuildSyncException("Cannot syncing a guild that is currently syncing"); ids[i] = Long.toUnsignedString(guilds[i].getID()); } @@ -532,12 +541,14 @@ public void syncGuilds(IGuild... guilds) throws GuildSyncException, AccountTypeE /** * Syncs guilds to client if the logged in user is not a bot * - * @param guildIDs the ids of the guilds to sync + * @param guildIDs + * the ids of the guilds to sync * @throws AccountTypeException * @throws GuildSyncException */ public void syncGuilds(long... guildIDs) throws AccountTypeException, GuildSyncException { - if (user.isBot()) throw new AccountTypeException("Only user accounts are allowed to sync guilds"); + if (user.isBot()) + throw new AccountTypeException("Only user accounts are allowed to sync guilds"); String[] ids = new String[guildIDs.length]; for (int i = 0; i < guildIDs.length; i++) { @@ -551,15 +562,18 @@ public void syncGuilds(long... guildIDs) throws AccountTypeException, GuildSyncE /** * Syncs guilds to client if the logged in user is not a bot * - * @param guildIDs the ids of the guilds to sync + * @param guildIDs + * the ids of the guilds to sync * @throws AccountTypeException * @throws GuildSyncException */ public void syncGuilds(String... guildIDs) throws AccountTypeException, GuildSyncException { - if (user.isBot()) throw new AccountTypeException("Only user accounts are allowed to sync guilds"); + if (user.isBot()) + throw new AccountTypeException("Only user accounts are allowed to sync guilds"); for (String id : guildIDs) { - if (isGuildSyncing(id)) throw new GuildSyncException("Cannot syncing a guild that is currently syncing"); + if (isGuildSyncing(id)) + throw new GuildSyncException("Cannot syncing a guild that is currently syncing"); } Packet packet = new Packet(12, guildIDs); diff --git a/src/main/java/io/discloader/discloader/common/ShardManager.java b/src/main/java/io/discloader/discloader/common/ShardManager.java index 4bb292734..3d9234629 100644 --- a/src/main/java/io/discloader/discloader/common/ShardManager.java +++ b/src/main/java/io/discloader/discloader/common/ShardManager.java @@ -9,11 +9,11 @@ import io.discloader.discloader.client.logger.DLLogger; import io.discloader.discloader.common.event.sharding.IShardingListener; +import io.discloader.discloader.common.registry.EntityRegistry; /** - * Class used to handling the shards of a sharded connection to Discord's Gateway - * Usage: - * + * Class used to handling the shards of a sharded connection to Discord's + * Gateway Usage: * DLOptions options = new DLOptions(); * * ShardManager manager = new ShardManager( @@ -87,6 +87,7 @@ public void run() { options.defaultCommands = ShardManager.this.options.defaultCommands; Shard shard = new Shard(options, ShardManager.this); shards.add(shard); + EntityRegistry.addShard(shard); shard.launch(); try { sleep(5500L); @@ -105,13 +106,15 @@ public void launchShards() { } /** - * @param token the token to set + * @param token + * the token to set */ public void setToken(String token) { this.token = token; } public void setTotalShards(int shards) { - if (shards >= 1) shardCount = shards; + if (shards >= 1) + shardCount = shards; } } diff --git a/src/main/java/io/discloader/discloader/common/registry/EntityRegistry.java b/src/main/java/io/discloader/discloader/common/registry/EntityRegistry.java index f9b3c870e..6469e07b7 100644 --- a/src/main/java/io/discloader/discloader/common/registry/EntityRegistry.java +++ b/src/main/java/io/discloader/discloader/common/registry/EntityRegistry.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; +import io.discloader.discloader.common.DiscLoader; import io.discloader.discloader.common.Shard; import io.discloader.discloader.core.entity.Webhook; import io.discloader.discloader.entity.IWebhook; @@ -39,21 +40,28 @@ public class EntityRegistry { private static final Map privateChannels = new HashMap<>(); private static final Map voiceChannels = new HashMap<>(); private static final Map guildChannels = new HashMap<>(); + private static final Map shards = new HashMap<>(); - public static IChannel addChannel(ChannelJSON data) { - return addChannel(data, null); + public static IChannel addChannel(ChannelJSON data, DiscLoader loader) { + return addChannel(data, loader, null); } - public static IChannel addChannel(ChannelJSON data, IGuild guild) { - IChannel channel = EntityBuilder.getChannelFactory().buildChannel(data, guild); + public static IChannel addChannel(ChannelJSON data, DiscLoader loader, IGuild guild) { + IChannel channel = EntityBuilder.getChannelFactory().buildChannel(data, loader, guild); if (channel != null) { channels.put(channel.getID(), channel); - if (channel instanceof ITextChannel) textChannels.put(channel.getID(), (ITextChannel) channel); - if (channel instanceof IPrivateChannel) privateChannels.put(channel.getID(), (IPrivateChannel) channel); - if (channel instanceof IGroupChannel) groupChannels.put(channel.getID(), (IGroupChannel) channel); - if (channel instanceof IVoiceChannel) voiceChannels.put(channel.getID(), (IVoiceChannel) channel); - if (channel instanceof IGuildChannel) guildChannels.put(channel.getID(), (IGuildChannel) channel); - if (channel instanceof IChannelCategory) categories.put(channel.getID(), (IChannelCategory) channel); + if (channel instanceof ITextChannel) + textChannels.put(channel.getID(), (ITextChannel) channel); + if (channel instanceof IPrivateChannel) + privateChannels.put(channel.getID(), (IPrivateChannel) channel); + if (channel instanceof IGroupChannel) + groupChannels.put(channel.getID(), (IGroupChannel) channel); + if (channel instanceof IVoiceChannel) + voiceChannels.put(channel.getID(), (IVoiceChannel) channel); + if (channel instanceof IGuildChannel) + guildChannels.put(channel.getID(), (IGuildChannel) channel); + if (channel instanceof IChannelCategory) + categories.put(channel.getID(), (IChannelCategory) channel); } return channel; } @@ -64,8 +72,13 @@ public static IGuild addGuild(GuildJSON data) { return guild; } + public static Shard addShard(Shard shard) { + return shards.put(shard.getShardID(), shard); + } + public static IUser addUser(UserJSON data) { - if (userExists(data.id == null ? "0" : data.id)) return getUserByID(data.id == null ? "0" : data.id); + if (userExists(data.id == null ? "0" : data.id)) + return getUserByID(data.id == null ? "0" : data.id); IUser user = EntityBuilder.getUserFactory().buildUser(data); users.put(user.getID(), user); return user; @@ -137,10 +150,12 @@ public static Collection getGuilds() { } public static List getGuildsOnShard(Shard shard) { - if (shard == null) return new ArrayList<>(); + if (shard == null) + return new ArrayList<>(); List sgs = new ArrayList<>(); for (IGuild guild : getGuilds()) { - if ((guild.getID() >> 22) % shard.getShardCount() == shard.getShardID()) sgs.add(guild); + if ((guild.getID() >> 22) % shard.getShardCount() == shard.getShardID()) + sgs.add(guild); } return sgs; } @@ -154,13 +169,15 @@ public static IPrivateChannel getPrivateChannelByID(String channelID) { } public static IPrivateChannel getPrivateChannelByUser(IUser user) { - if (user == null) return null; + if (user == null) + return null; return getPrivateChannelByUserID(user.getID()); } public static IPrivateChannel getPrivateChannelByUserID(long userID) { for (IPrivateChannel channel : getPrivateChannels()) - if (channel.getRecipient().getID() == userID) return channel; + if (channel.getRecipient().getID() == userID) + return channel; return null; } @@ -209,7 +226,8 @@ public static Collection getVoiceChannels() { } public static VoiceConnection getVoiceConnectionByGuild(IGuild guild) { - if (guild == null) return null; + if (guild == null) + return null; return getVoiceConnectionByID(guild.getID()); } @@ -237,7 +255,8 @@ public static Map getWebhooks() { } public static boolean guildExists(IGuild guild) { - if (guild == null) return false; + if (guild == null) + return false; return guilds.containsValue(guild); } @@ -250,7 +269,8 @@ public static boolean guildExists(String guildID) { } public static boolean hasVoiceConnection(IGuild guild) { - if (guild == null) return false; + if (guild == null) + return false; return voiceConnections.containsKey(guild.getID()); } @@ -263,7 +283,8 @@ public static void putVoiceConnection(VoiceConnection connection) { } public static void removeChannel(IChannel channel) { - if (channel == null) return; + if (channel == null) + return; channels.remove(channel.getID()); if (channel.getType() == ChannelTypes.TEXT) { textChannels.remove(channel.getID()); @@ -277,7 +298,8 @@ public static void removeChannel(IChannel channel) { } public static void removeGuild(IGuild guild) { - if (guild == null) return; + if (guild == null) + return; guilds.remove(guild.getID()); } @@ -300,4 +322,12 @@ public static boolean webhookExists(long webhookID) { public static boolean webhookExists(String webhookID) { return webhookExists(SnowflakeUtil.parse(webhookID)); } + + public static Collection getShards() { + return shards.values(); + } + + public static Shard getShardByID(int shardID) { + return shards.get(shardID); + } } diff --git a/src/main/java/io/discloader/discloader/common/registry/factory/ChannelFactory.java b/src/main/java/io/discloader/discloader/common/registry/factory/ChannelFactory.java index a4359d3ce..22649f685 100644 --- a/src/main/java/io/discloader/discloader/common/registry/factory/ChannelFactory.java +++ b/src/main/java/io/discloader/discloader/common/registry/factory/ChannelFactory.java @@ -3,6 +3,7 @@ import io.discloader.discloader.common.DiscLoader; import io.discloader.discloader.core.entity.channel.Channel; import io.discloader.discloader.core.entity.channel.ChannelCategory; +import io.discloader.discloader.core.entity.channel.GroupChannel; import io.discloader.discloader.core.entity.channel.PrivateChannel; import io.discloader.discloader.core.entity.channel.TextChannel; import io.discloader.discloader.core.entity.channel.VoiceChannel; @@ -19,16 +20,16 @@ public class ChannelFactory { - public IChannel buildChannel(ChannelJSON data, IGuild guild) { - return buildChannel(data, guild, true); + public IChannel buildChannel(ChannelJSON data, DiscLoader loader, IGuild guild) { + return buildChannel(data, loader, guild, true); } - public IChannel buildChannel(ChannelJSON data, IGuild guild, boolean insert) { + public IChannel buildChannel(ChannelJSON data, DiscLoader loader, IGuild guild, boolean insert) { IChannel channel = null; if (data.type == DLUtil.ChannelTypes.DM) { channel = new PrivateChannel(DiscLoader.getDiscLoader(), data); } else if (data.type == DLUtil.ChannelTypes.groupDM) { - channel = new Channel(DiscLoader.getDiscLoader(), data); + channel = new GroupChannel(DiscLoader.getDiscLoader(), data); } else { if (guild != null) { if (data.type == DLUtil.ChannelTypes.text) { diff --git a/src/main/java/io/discloader/discloader/core/entity/channel/ChannelCategory.java b/src/main/java/io/discloader/discloader/core/entity/channel/ChannelCategory.java index 42db43dc7..8d601b8e4 100644 --- a/src/main/java/io/discloader/discloader/core/entity/channel/ChannelCategory.java +++ b/src/main/java/io/discloader/discloader/core/entity/channel/ChannelCategory.java @@ -7,7 +7,6 @@ import org.json.JSONObject; import io.discloader.discloader.common.registry.EntityBuilder; -import io.discloader.discloader.common.registry.EntityRegistry; import io.discloader.discloader.entity.IOverwrite; import io.discloader.discloader.entity.channel.ChannelTypes; import io.discloader.discloader.entity.channel.IChannelCategory; @@ -34,7 +33,7 @@ public CompletableFuture addChannel(T channel) { 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, guild, false); + T newChannel = (T) EntityBuilder.getChannelFactory().buildChannel(d, getLoader(), guild, false); future.complete(newChannel); }).exceptionally(ex -> { future.completeExceptionally(ex); @@ -50,7 +49,7 @@ public CompletableFuture createChannel(String name, ChannelTypes CompletableFuture cf = loader.rest.request(Methods.POST, Endpoints.guildChannels(getGuild().getID()), new RESTOptions(data), ChannelJSON.class); cf.thenAcceptAsync(channelJSON -> { if (channelJSON != null) { - IGuildChannel channel = (IGuildChannel) EntityRegistry.addChannel(channelJSON, getGuild()); + IGuildChannel channel = (IGuildChannel) EntityBuilder.getChannelFactory().buildChannel(channelJSON, getLoader(), getGuild(), false); if (channel != null) future.complete(channel); } @@ -71,7 +70,7 @@ public CompletableFuture createChannel(String name, ChannelTypes CompletableFuture cf = loader.rest.request(Methods.POST, Endpoints.guildChannels(getGuild().getID()), new RESTOptions(data), ChannelJSON.class); cf.thenAcceptAsync(channelJSON -> { if (channelJSON != null) { - IGuildChannel channel = (IGuildChannel) EntityRegistry.addChannel(channelJSON, getGuild()); + IGuildChannel channel = (IGuildChannel) EntityBuilder.getChannelFactory().buildChannel(channelJSON, getLoader(), getGuild(), false); if (channel != null) future.complete(channel); } @@ -91,7 +90,7 @@ public CompletableFuture createTextChannel(String name) { CompletableFuture cf = loader.rest.request(Methods.POST, Endpoints.guildChannels(getGuild().getID()), new RESTOptions(data), ChannelJSON.class); cf.thenAcceptAsync(channelJSON -> { if (channelJSON != null) { - IGuildTextChannel channel = (IGuildTextChannel) EntityRegistry.addChannel(channelJSON, getGuild()); + IGuildTextChannel channel = (IGuildTextChannel) EntityBuilder.getChannelFactory().buildChannel(channelJSON, getLoader(), getGuild(), false); if (channel != null) future.complete(channel); } @@ -110,7 +109,7 @@ public CompletableFuture createTextChannel(String name, IOver CompletableFuture cf = loader.rest.request(Methods.POST, Endpoints.guildChannels(getGuild().getID()), new RESTOptions(data), ChannelJSON.class); cf.thenAcceptAsync(channelJSON -> { if (channelJSON != null) { - IGuildTextChannel channel = (IGuildTextChannel) EntityRegistry.addChannel(channelJSON, getGuild()); + IGuildTextChannel channel = (IGuildTextChannel) EntityBuilder.getChannelFactory().buildChannel(channelJSON, getLoader(), getGuild(), false); if (channel != null) future.complete(channel); } @@ -129,7 +128,7 @@ public CompletableFuture createVoiceChannel(String name) { CompletableFuture cf = loader.rest.request(Methods.POST, Endpoints.guildChannels(getGuild().getID()), new RESTOptions(data), ChannelJSON.class); cf.thenAcceptAsync(channelJSON -> { if (channelJSON != null) { - IGuildVoiceChannel channel = (IGuildVoiceChannel) EntityRegistry.addChannel(channelJSON, getGuild()); + IGuildVoiceChannel channel = (IGuildVoiceChannel) EntityBuilder.getChannelFactory().buildChannel(channelJSON, getLoader(), getGuild(), false); if (channel != null) future.complete(channel); } @@ -143,7 +142,21 @@ public CompletableFuture createVoiceChannel(String name) { @Override public CompletableFuture createVoiceChannel(String name, IOverwrite... overwrites) { - return null; + CompletableFuture future = new CompletableFuture<>(); + ChannelPayload data = new ChannelPayload(name, ChannelTypes.VOICE, overwrites); + CompletableFuture cf = loader.rest.request(Methods.POST, Endpoints.guildChannels(getGuild().getID()), new RESTOptions(data), ChannelJSON.class); + cf.thenAcceptAsync(channelJSON -> { + if (channelJSON != null) { + IGuildVoiceChannel channel = (IGuildVoiceChannel) EntityBuilder.getChannelFactory().buildChannel(channelJSON, getLoader(), getGuild(), false); + if (channel != null) + future.complete(channel); + } + }); + cf.exceptionally(ex -> { + future.completeExceptionally(ex); + return null; + }); + return future; } @Override @@ -182,7 +195,7 @@ public CompletableFuture removeChannel(T channel) { JSONObject settings = new JSONObject().put("parent_id", (String) null); loader.rest.request(Methods.PATCH, Endpoints.channel(channel.getID()), new RESTOptions(settings), ChannelJSON.class).thenAcceptAsync(data -> { @SuppressWarnings("unchecked") - T newChannel = (T) EntityBuilder.getChannelFactory().buildChannel(data, guild, false); + T newChannel = (T) EntityBuilder.getChannelFactory().buildChannel(data, getLoader(), guild, false); future.complete(newChannel); }).exceptionally(ex -> { future.completeExceptionally(ex); diff --git a/src/main/java/io/discloader/discloader/core/entity/channel/GuildChannel.java b/src/main/java/io/discloader/discloader/core/entity/channel/GuildChannel.java index 8cbfc0d65..09e95d9f7 100644 --- a/src/main/java/io/discloader/discloader/core/entity/channel/GuildChannel.java +++ b/src/main/java/io/discloader/discloader/core/entity/channel/GuildChannel.java @@ -174,7 +174,7 @@ public CompletableFuture edit(String name, int position CompletableFuture future = new CompletableFuture<>(); JSONObject settings = new JSONObject().put("name", name).put("position", position).put("nsfw", nsfw).put("permission_overwrites", overwrites); loader.rest.request(Methods.PATCH, Endpoints.channel(getID()), new RESTOptions(settings), ChannelJSON.class).thenAcceptAsync(data -> { - IChannel newChannel = EntityBuilder.getChannelFactory().buildChannel(data, guild, false); + IChannel newChannel = EntityBuilder.getChannelFactory().buildChannel(data, getLoader(), guild, false); if (newChannel instanceof IGuildChannel) future.complete((IGuildChannel) newChannel); }).exceptionally(ex -> { future.completeExceptionally(ex); @@ -281,7 +281,7 @@ public CompletableFuture setCategory(IChannelCategory category) { CompletableFuture future = new CompletableFuture<>(); JSONObject settings = new JSONObject().put("parent_id", SnowflakeUtil.asString(category)); loader.rest.request(Methods.PATCH, Endpoints.channel(getID()), new RESTOptions(settings), ChannelJSON.class).thenAcceptAsync(data -> { - IGuildChannel newChannel = (IGuildChannel) EntityBuilder.getChannelFactory().buildChannel(data, guild, false); + IGuildChannel newChannel = (IGuildChannel) EntityBuilder.getChannelFactory().buildChannel(data, getLoader(), guild, false); future.complete(newChannel); }).exceptionally(ex -> { future.completeExceptionally(ex); @@ -307,6 +307,7 @@ public CompletableFuture setOverwrite(IOverwrite overwrite) { @Override public CompletableFuture> setOverwrite(IOverwrite... overwrites) { + return null; } diff --git a/src/main/java/io/discloader/discloader/core/entity/guild/Guild.java b/src/main/java/io/discloader/discloader/core/entity/guild/Guild.java index 64046e955..a1e8a677e 100644 --- a/src/main/java/io/discloader/discloader/core/entity/guild/Guild.java +++ b/src/main/java/io/discloader/discloader/core/entity/guild/Guild.java @@ -186,8 +186,7 @@ public IGuildMember addMember(IGuildMember member, boolean emit) { * @return The {@link GuildMember} that was instantiated. */ @Override - public GuildMember addMember(IUser user, String[] roles, boolean deaf, boolean mute, String nick, - boolean emitEvent) { + public GuildMember addMember(IUser user, String[] roles, boolean deaf, boolean mute, String nick, boolean emitEvent) { boolean exists = members.containsKey(user.getID()); GuildMember member = new GuildMember(this, user, roles, deaf, mute, nick); members.put(member.getID(), member); @@ -261,11 +260,9 @@ public CompletableFuture ban(IGuildMember member) { if (!hasPermission(Permissions.BAN_MEMBERS)) throw new PermissionsException(""); CompletableFuture future = new CompletableFuture<>(); - loader.rest - .request(Methods.PUT, Endpoints.guildBanMember(getID(), member.getID()), new RESTOptions(), Void.class) - .thenAcceptAsync(action -> { - future.complete(member); - }); + loader.rest.request(Methods.PUT, Endpoints.guildBanMember(getID(), member.getID()), new RESTOptions(), Void.class).thenAcceptAsync(action -> { + future.complete(member); + }); return future; } @@ -274,10 +271,9 @@ public CompletableFuture ban(IGuildMember member, String reason) t if (!hasPermission(Permissions.BAN_MEMBERS)) throw new PermissionsException(""); CompletableFuture future = new CompletableFuture<>(); - loader.rest.request(Methods.PUT, Endpoints.guildBanMember(getID(), member.getID()), new RESTOptions(reason), - Void.class).thenAcceptAsync(action -> { - future.complete(member); - }); + loader.rest.request(Methods.PUT, Endpoints.guildBanMember(getID(), member.getID()), new RESTOptions(reason), Void.class).thenAcceptAsync(action -> { + future.complete(member); + }); return future; } @@ -293,10 +289,9 @@ public CompletableFuture beginPrune(int days) { if (!getCurrentMember().getPermissions().hasPermission(Permissions.KICK_MEMBERS)) throw new PermissionsException("Pruning members requires the 'KICK_MEMBERS' permission"); CompletableFuture future = new CompletableFuture<>(); - loader.rest.request(Methods.POST, Endpoints.guildPrune(getID()), new RESTOptions(), Integer.class) - .thenAcceptAsync(pruned -> { - future.complete(pruned); - }); + loader.rest.request(Methods.POST, Endpoints.guildPrune(getID()), new RESTOptions(), Integer.class).thenAcceptAsync(pruned -> { + future.complete(pruned); + }); return future; } @@ -327,12 +322,11 @@ public IGuild clone() { public CompletableFuture createCategory(String name) { CompletableFuture future = new CompletableFuture<>(); JSONObject chanSet = new JSONObject().put("name", name).put("type", 4); - loader.rest.request(Methods.POST, Endpoints.guildChannels(getID()), new RESTOptions(chanSet), ChannelJSON.class) - .thenAcceptAsync(d -> { - IChannel channel = EntityBuilder.getChannelFactory().buildChannel(d, this, false); - if (channel instanceof IChannelCategory) - future.complete((IChannelCategory) channel); - }); + loader.rest.request(Methods.POST, Endpoints.guildChannels(getID()), new RESTOptions(chanSet), ChannelJSON.class).thenAcceptAsync(d -> { + IChannel channel = EntityBuilder.getChannelFactory().buildChannel(d, loader, this, false); + if (channel instanceof IChannelCategory) + future.complete((IChannelCategory) channel); + }); return future; } @@ -345,12 +339,11 @@ public CompletableFuture createCategory(String name, IOverwrit ows.put(ow); } JSONObject chanSet = new JSONObject().put("name", name).put("type", 4).put("permission_overwrites", ows); - loader.rest.request(Methods.POST, Endpoints.guildChannels(getID()), new RESTOptions(chanSet), ChannelJSON.class) - .thenAcceptAsync(d -> { - IChannel channel = EntityBuilder.getChannelFactory().buildChannel(d, this, false); - if (channel instanceof IChannelCategory) - future.complete((IChannelCategory) channel); - }); + loader.rest.request(Methods.POST, Endpoints.guildChannels(getID()), new RESTOptions(chanSet), ChannelJSON.class).thenAcceptAsync(d -> { + IChannel channel = EntityBuilder.getChannelFactory().buildChannel(d, loader, this, false); + if (channel instanceof IChannelCategory) + future.complete((IChannelCategory) channel); + }); return future; } @@ -363,8 +356,7 @@ public OffsetDateTime createdAt() { public CompletableFuture createEmoji(String name, File image) { String base64 = null; try { - base64 = new String( - "data:image/jpg;base64," + Base64.encodeBase64String(Files.readAllBytes(image.toPath()))); + base64 = new String("data:image/jpg;base64," + Base64.encodeBase64String(Files.readAllBytes(image.toPath()))); } catch (IOException e) { e.printStackTrace(); } @@ -406,8 +398,7 @@ public CompletableFuture createRole(String name, long permissions, int co return this.createRole(name, permissions, color, false, false); } - public CompletableFuture createRole(String name, long permissions, int color, boolean hoist, - boolean mentionable) { + public CompletableFuture createRole(String name, long permissions, int color, boolean hoist, boolean mentionable) { return new CreateRole(this, new SendableRole(name, permissions, color, hoist, mentionable)).execute(); } @@ -461,8 +452,7 @@ public CompletableFuture createVoiceChannel(String name, int bitra * successful. */ public CompletableFuture createVoiceChannel(String name, int bitrate, int userLimit) { - return this.loader.rest.createVoiceChannel(this, - new JSONObject().put("name", name).put("bitrate", bitrate).put("user_limit", userLimit)); + return this.loader.rest.createVoiceChannel(this, new JSONObject().put("name", name).put("bitrate", bitrate).put("user_limit", userLimit)); } @Override @@ -507,8 +497,7 @@ public boolean equals(Object object) { for (IGuildMember member : members.values()) if (!guild.members.containsKey(member.getID())) return false; - return guild.name.equals(name) && guild.ownerID == ownerID && guild.icon.equals(icon) - && (isSyncing() == guild.isSyncing()); + return guild.name.equals(name) && guild.ownerID == ownerID && guild.icon.equals(icon) && (isSyncing() == guild.isSyncing()); } @Override @@ -592,18 +581,15 @@ public CompletableFuture getAuditLog(IUser user) { } @Override - public CompletableFuture getAuditLog(IUser user, ActionTypes action, IAuditLogEntry before, - short limit) { + public CompletableFuture getAuditLog(IUser user, ActionTypes action, IAuditLogEntry before, short limit) { CompletableFuture future = new CompletableFuture<>(); - JSONObject params = new JSONObject().put("user_id", SnowflakeUtil.asString(user)) - .put("action_type", action.toInt()).put("before", SnowflakeUtil.asString(before)).put("limit", limit); - loader.rest.request(Methods.GET, Endpoints.auditLogs(getID()), new RESTOptions(params), AuditLogJSON.class) - .thenAcceptAsync(al -> { - future.complete(new AuditLog(this, al)); - }).exceptionally(ex -> { - future.completeExceptionally(ex); - return null; - }); + JSONObject params = new JSONObject().put("user_id", SnowflakeUtil.asString(user)).put("action_type", action.toInt()).put("before", SnowflakeUtil.asString(before)).put("limit", limit); + loader.rest.request(Methods.GET, Endpoints.auditLogs(getID()), new RESTOptions(params), AuditLogJSON.class).thenAcceptAsync(al -> { + future.complete(new AuditLog(this, al)); + }).exceptionally(ex -> { + future.completeExceptionally(ex); + return null; + }); return future; } @@ -659,8 +645,7 @@ public IGuildMember getCurrentMember() { public IGuildTextChannel getDefaultChannel() { IGuildTextChannel defaultChannel = null; for (IGuildTextChannel channel : textChannels.values()) { - if ((defaultChannel == null || channel.getPosition() < defaultChannel.getPosition()) - && channel.permissionsOf(getCurrentMember()).hasPermission(Permissions.READ_MESSAGES, true)) { + if ((defaultChannel == null || channel.getPosition() < defaultChannel.getPosition()) && channel.permissionsOf(getCurrentMember()).hasPermission(Permissions.READ_MESSAGES, true)) { defaultChannel = channel; } } @@ -929,16 +914,16 @@ public CompletableFuture kick(IGuildMember member, String reason) CompletableFuture future = new CompletableFuture<>(); CompletableFuture kickFuture = loader.rest.request(Methods.DELETE, Endpoints.guildMember(getID(), member.getID()), new RESTOptions(reason), Void.class); - + kickFuture.thenAcceptAsync(n -> { future.complete(member); }); - + kickFuture.exceptionally(ex -> { future.completeExceptionally(ex); return null; }); - + return future; } @@ -991,8 +976,7 @@ public CompletableFuture setAFKChannel(IGuildVoiceChannel channel) { public CompletableFuture setIcon(String icon) throws IOException { if (!isOwner() && !getCurrentMember().getPermissions().hasPermission(Permissions.MANAGE_GUILD)) throw new PermissionsException("Insuficient Permissions"); - String base64 = new String( - "data:image/jpg;base64," + Base64.encodeBase64String(Files.readAllBytes(Paths.get(icon)))); + String base64 = new String("data:image/jpg;base64," + Base64.encodeBase64String(Files.readAllBytes(Paths.get(icon)))); return new ModifyGuild(this, new JSONObject().put("icon", base64)).execute(); } @@ -1058,7 +1042,7 @@ public void setup(GuildJSON data) { } if (data.channels != null && data.channels.length > 0) { for (ChannelJSON channelData : data.channels) { - IGuildChannel chan = (IGuildChannel) EntityRegistry.addChannel(channelData, this); + IGuildChannel chan = (IGuildChannel) EntityRegistry.addChannel(channelData, getLoader(), this); if (chan instanceof IGuildTextChannel) textChannels.put(chan.getID(), (IGuildTextChannel) chan); else if (chan instanceof IGuildVoiceChannel) diff --git a/src/main/java/io/discloader/discloader/entity/channel/IGuildChannel.java b/src/main/java/io/discloader/discloader/entity/channel/IGuildChannel.java index 4deb2db86..24758005c 100644 --- a/src/main/java/io/discloader/discloader/entity/channel/IGuildChannel.java +++ b/src/main/java/io/discloader/discloader/entity/channel/IGuildChannel.java @@ -4,6 +4,7 @@ import java.util.Map; import java.util.concurrent.CompletableFuture; +import io.discloader.discloader.common.exceptions.DiscordException; import io.discloader.discloader.common.exceptions.PermissionsException; import io.discloader.discloader.core.entity.guild.Guild; import io.discloader.discloader.core.entity.guild.Role; @@ -107,7 +108,12 @@ public interface IGuildChannel extends IChannel { */ IPermission permissionsOf(IGuildMember iGuildMember); - CompletableFuture setCategory(IChannelCategory category); + /** + * @param category + * @return + * @throws PermissionsException + */ + CompletableFuture setCategory(IChannelCategory category) throws PermissionsException; /** * Sets the name of the channel. @@ -115,8 +121,11 @@ public interface IGuildChannel extends IChannel { * @param name * New name for the channel who we are modifying. * @return A completed future object with the new name set, if successful. + * @throws PermissionsException + * @throws DiscordException */ - CompletableFuture setName(String name); + + CompletableFuture setName(String name) throws PermissionsException, DiscordException; CompletableFuture setNSFW(boolean nswf); diff --git a/src/main/java/io/discloader/discloader/network/gateway/packets/ChannelCreate.java b/src/main/java/io/discloader/discloader/network/gateway/packets/ChannelCreate.java index 4cb665c96..5926b89fb 100644 --- a/src/main/java/io/discloader/discloader/network/gateway/packets/ChannelCreate.java +++ b/src/main/java/io/discloader/discloader/network/gateway/packets/ChannelCreate.java @@ -26,7 +26,7 @@ public void handle(SocketPacket packet) { ChannelJSON data = gson.fromJson(d, ChannelJSON.class); IGuild guild = EntityRegistry.getGuildByID(data.guild_id); - IChannel channel = EntityRegistry.addChannel(data, guild); + IChannel channel = EntityRegistry.addChannel(data,loader, guild); ChannelCreateEvent event = new ChannelCreateEvent(channel); loader.emit(Events.CHANNEL_CREATE, event); loader.emit(event); diff --git a/src/main/java/io/discloader/discloader/network/gateway/packets/ChannelDelete.java b/src/main/java/io/discloader/discloader/network/gateway/packets/ChannelDelete.java index 7daea4fa3..1e8bd503d 100644 --- a/src/main/java/io/discloader/discloader/network/gateway/packets/ChannelDelete.java +++ b/src/main/java/io/discloader/discloader/network/gateway/packets/ChannelDelete.java @@ -25,9 +25,9 @@ public void handle(SocketPacket packet) { IChannel channel = null; if (data.guild_id != null) { guild = EntityRegistry.getGuildByID(data.guild_id); - channel = EntityRegistry.addChannel(data, guild); + channel = EntityRegistry.addChannel(data,loader, guild); } else { - channel = EntityRegistry.addChannel(data); + channel = EntityRegistry.addChannel(data,loader); } switch (channel.getType()) { case TEXT: diff --git a/src/main/java/io/discloader/discloader/network/gateway/packets/ChannelUpdate.java b/src/main/java/io/discloader/discloader/network/gateway/packets/ChannelUpdate.java index edbda200f..175dc956f 100644 --- a/src/main/java/io/discloader/discloader/network/gateway/packets/ChannelUpdate.java +++ b/src/main/java/io/discloader/discloader/network/gateway/packets/ChannelUpdate.java @@ -31,9 +31,9 @@ public void handle(SocketPacket packet) { IChannel channel = null; if (data.guild_id != null) { guild = EntityRegistry.getGuildByID(data.guild_id); - channel = EntityRegistry.addChannel(data, guild); + channel = EntityRegistry.addChannel(data, loader, guild); } else { - channel = EntityRegistry.addChannel(data); + channel = EntityRegistry.addChannel(data, loader); } if (oldChannel instanceof ITextChannel) { ITextChannel oitc = (ITextChannel) oldChannel, itc = (ITextChannel) channel; diff --git a/src/main/java/io/discloader/discloader/network/rest/RESTManager.java b/src/main/java/io/discloader/discloader/network/rest/RESTManager.java index 423fb55e9..0724887e3 100644 --- a/src/main/java/io/discloader/discloader/network/rest/RESTManager.java +++ b/src/main/java/io/discloader/discloader/network/rest/RESTManager.java @@ -68,19 +68,23 @@ public RESTManager(DiscLoader loader) { } /** - * @param The type to parse request response data as + * @param + * The type to parse request response data as * @param method * @param url * @param options - * @param cls The {@link Class} of Type param - * @return A {@link CompletableFuture} Object that completes with an - * Object of Type if applicable. + * @param cls + * The {@link Class} of Type param + * @return A {@link CompletableFuture} Object that completes with an Object + * of Type if applicable. */ @SuppressWarnings("unchecked") public CompletableFuture request(Methods method, String url, RESTOptions options, Class cls) { - if (options == null) options = new RESTOptions(); + if (options == null) + options = new RESTOptions(); Request apiRequest = new Request(options.getPayload()); - if (!routes.containsKey(url)) routes.put(url, new Route(this, url, method, options.isAuth(), cls)); + if (!routes.containsKey(url)) + routes.put(url, new Route(this, url, method, options.isAuth(), cls)); return ((Route) routes.get(url)).push(apiRequest); } @@ -113,7 +117,7 @@ public CompletableFuture createEmoji(Guild guild, String name, Stri public CompletableFuture createTextChannel(Guild guild, JSONObject data) { CompletableFuture future = new CompletableFuture(); this.makeRequest(DLUtil.Endpoints.guildChannels(guild.getID()), DLUtil.Methods.POST, true, data.put("type", "text")).thenAcceptAsync(action -> { - future.complete((TextChannel) EntityRegistry.addChannel(this.gson.fromJson(action, ChannelJSON.class), guild)); + future.complete((TextChannel) EntityRegistry.addChannel(this.gson.fromJson(action, ChannelJSON.class), loader, guild)); }); return future; } @@ -121,7 +125,7 @@ public CompletableFuture createTextChannel(Guild guild, JSONObject public CompletableFuture createVoiceChannel(Guild guild, JSONObject data) { CompletableFuture future = new CompletableFuture(); this.makeRequest(DLUtil.Endpoints.guildChannels(guild.getID()), DLUtil.Methods.POST, true, data.put("type", "voice")).thenAcceptAsync(action -> { - future.complete((VoiceChannel) EntityRegistry.addChannel(this.gson.fromJson(action, ChannelJSON.class), guild)); + future.complete((VoiceChannel) EntityRegistry.addChannel(this.gson.fromJson(action, ChannelJSON.class), loader, guild)); }); return future; } @@ -318,7 +322,8 @@ public boolean isGlobally() { } /** - * @param globally the globally to set + * @param globally + * the globally to set */ public void setGlobally(boolean globally) { this.globally = globally; diff --git a/src/main/java/io/discloader/discloader/network/rest/actions/channel/CreateDMChannel.java b/src/main/java/io/discloader/discloader/network/rest/actions/channel/CreateDMChannel.java index c7ece21db..4887cad62 100644 --- a/src/main/java/io/discloader/discloader/network/rest/actions/channel/CreateDMChannel.java +++ b/src/main/java/io/discloader/discloader/network/rest/actions/channel/CreateDMChannel.java @@ -38,7 +38,7 @@ public void complete(String packet, Throwable ex) { } ChannelJSON data = gson.fromJson(packet, ChannelJSON.class); - future.complete((IPrivateChannel) EntityRegistry.addChannel(data)); + future.complete((IPrivateChannel) EntityRegistry.addChannel(data, loader)); } } diff --git a/src/main/java/io/discloader/discloader/network/rest/actions/guild/CreateTextChannel.java b/src/main/java/io/discloader/discloader/network/rest/actions/guild/CreateTextChannel.java index 14a3b1cf9..39ce7c5f4 100644 --- a/src/main/java/io/discloader/discloader/network/rest/actions/guild/CreateTextChannel.java +++ b/src/main/java/io/discloader/discloader/network/rest/actions/guild/CreateTextChannel.java @@ -34,7 +34,7 @@ public void complete(String packet, Throwable ex) { } ChannelJSON data = gson.fromJson(packet, ChannelJSON.class); - future.complete((IGuildTextChannel) EntityRegistry.addChannel(data)); + future.complete((IGuildTextChannel) EntityRegistry.addChannel(data, loader)); } } diff --git a/src/main/java/io/discloader/discloader/network/rest/actions/guild/CreateVoiceChannel.java b/src/main/java/io/discloader/discloader/network/rest/actions/guild/CreateVoiceChannel.java index a429726df..aaeac0590 100644 --- a/src/main/java/io/discloader/discloader/network/rest/actions/guild/CreateVoiceChannel.java +++ b/src/main/java/io/discloader/discloader/network/rest/actions/guild/CreateVoiceChannel.java @@ -34,7 +34,7 @@ public void complete(String packet, Throwable ex) { } ChannelJSON data = gson.fromJson(packet, ChannelJSON.class); - future.complete((IGuildVoiceChannel) EntityRegistry.addChannel(data)); + future.complete((IGuildVoiceChannel) EntityRegistry.addChannel(data,loader)); } }