Skip to content

Commit

Permalink
#29 complete
Browse files Browse the repository at this point in the history
`ChannelCategory`s have been implemented into DiscLoader
  • Loading branch information
R3alCl0ud committed Nov 7, 2017
1 parent 6295171 commit 89b069d
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 143 deletions.
74 changes: 44 additions & 30 deletions src/main/java/io/discloader/discloader/common/DiscLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,8 +131,7 @@ public static DiscLoader getDiscLoader() {
// public HashMap<Long, IPrivateChannel> 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
Expand Down Expand Up @@ -209,7 +209,8 @@ public DiscLoader() {
* }
* </pre>
*
* @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);
Expand Down Expand Up @@ -251,16 +252,20 @@ public DiscLoader(DLOptions options) {
* }
* </pre>
*
* @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);
Expand All @@ -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() {
Expand Down Expand Up @@ -373,28 +379,27 @@ public boolean isGuildSyncing(IGuild guild) {
}

public boolean isGuildSyncing(String guildID) {
return syncingGuilds.containsKey(guildID);
return syncingGuilds.containsKey(SnowflakeUtil.parse(guildID));
}

/**
* Makes the client log into the gateway. using a predefined token.<br>
* 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<DiscLoader> 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<DiscLoader> login(String token) {
LOG.info("Attempting to login");
Expand Down Expand Up @@ -448,16 +453,16 @@ public <T> DiscLoader onEvent(Class<T> cls, Consumer<Object> 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.<br>
* <br>
* <strike>This method <u><b>must</b></u> be called to start DiscLoader.
* This method gets called in {@link #login(String)} before attempting to login
* now.<br>
* <br>
* <strike>This method <u><b>must</b></u> be called to start DiscLoader. <br>
* As it begins the setup process for DiscLoader to be able to function
* correctly.<br>
* It <u><b>will</b></u> crash otherwise</strike>
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
}

Expand All @@ -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++) {
Expand All @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/io/discloader/discloader/common/ShardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
* <code>
* Class used to handling the shards of a sharded connection to Discord's
* Gateway Usage: <code>
* DLOptions options = new DLOptions();
*
* ShardManager manager = new ShardManager(
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,21 +40,28 @@ public class EntityRegistry {
private static final Map<Long, IPrivateChannel> privateChannels = new HashMap<>();
private static final Map<Long, IVoiceChannel> voiceChannels = new HashMap<>();
private static final Map<Long, IGuildChannel> guildChannels = new HashMap<>();
private static final Map<Integer, Shard> 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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -137,10 +150,12 @@ public static Collection<IGuild> getGuilds() {
}

public static List<IGuild> getGuildsOnShard(Shard shard) {
if (shard == null) return new ArrayList<>();
if (shard == null)
return new ArrayList<>();
List<IGuild> 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;
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -209,7 +226,8 @@ public static Collection<IVoiceChannel> getVoiceChannels() {
}

public static VoiceConnection getVoiceConnectionByGuild(IGuild guild) {
if (guild == null) return null;
if (guild == null)
return null;
return getVoiceConnectionByID(guild.getID());
}

Expand Down Expand Up @@ -237,7 +255,8 @@ public static Map<Long, IWebhook> getWebhooks() {
}

public static boolean guildExists(IGuild guild) {
if (guild == null) return false;
if (guild == null)
return false;
return guilds.containsValue(guild);
}

Expand All @@ -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());
}

Expand All @@ -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());
Expand All @@ -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());
}

Expand All @@ -300,4 +322,12 @@ public static boolean webhookExists(long webhookID) {
public static boolean webhookExists(String webhookID) {
return webhookExists(SnowflakeUtil.parse(webhookID));
}

public static Collection<Shard> getShards() {
return shards.values();
}

public static Shard getShardByID(int shardID) {
return shards.get(shardID);
}
}
Loading

0 comments on commit 89b069d

Please sign in to comment.