Skip to content

Commit

Permalink
Added force fetch members
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackzzila committed Oct 16, 2016
1 parent b0f0bff commit db9c70f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 51 deletions.
31 changes: 6 additions & 25 deletions lib/src/events/GuildCreateEvent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,16 @@ class GuildCreateEvent {
/// The guild created.
Guild guild;

GuildCreateEvent._new(Client client, Map<String, dynamic> json, _WS ws) {
GuildCreateEvent._new(Client client, Map<String, dynamic> json, Shard shard) {
this.guild =
new Guild._new(client, json['d'] as Map<String, dynamic>, true, true);

if (!client.ready) {
bool match = true;
client.guilds.forEach((String id, Guild o) {
if (o == null) match = false;
});

bool match2 = true;
ws.client.shards.forEach((int id, Shard s) {
if (!s.ready) match = false;
});
if (shard._ws.client._options.forceFetchMembers)
shard._send("REQUEST_GUILD_MEMBERS",
{"guild_id": guild.id, "query": "", "limit": 0});

if (match && match2) {
client.ready = true;
if (client.user.bot) {
client._http
.get('/oauth2/applications/@me', true)
.then((_HttpResponse r) {
client.app = new ClientOAuth2Application._new(
client, r.json as Map<String, dynamic>);
new ReadyEvent._new(client);
});
} else {
new ReadyEvent._new(client);
}
}
if (!client.ready) {
shard._ws.testReady();
} else {
client._events.onGuildCreate.add(this);
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/events/GuildMemberAddEvent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class GuildMemberAddEvent {
GuildMemberAddEvent._new(Client client, Map<String, dynamic> json) {
if (client.ready) {
final Guild guild = client.guilds[json['d']['guild_id']];
guild.memberCount++;
this.member =
new Member._new(client, json['d'] as Map<String, dynamic>, guild);
client._events.onGuildMemberAdd.add(this);
Expand Down
1 change: 1 addition & 0 deletions lib/src/events/GuildMemberRemoveEvent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class GuildMemberRemoveEvent {
GuildMemberRemoveEvent._new(Client client, Map<String, dynamic> json) {
if (client.ready && json['d']['user']['id'] != client.user.id) {
this.guild = client.guilds[json['d']['guild_id']];
guild.memberCount--;
this.user =
new User._new(client, json['d']['user'] as Map<String, dynamic>);
guild.members.remove(user.id);
Expand Down
56 changes: 32 additions & 24 deletions lib/src/internal/_WS.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,7 @@ class _WS {

shard.onReady.stream.listen((Shard s) {
if (!client.ready) {
bool match = true;
client.guilds.forEach((String id, Guild o) {
if (o == null) match = false;
});

bool match2 = true;
client.shards.forEach((int id, Shard s) {
if (!s.ready) match = false;
});

if (match && match2) {
client.ready = true;
if (client.user.bot) {
client._http
.get('/oauth2/applications/@me', true)
.then((_HttpResponse r) {
client.app = new ClientOAuth2Application._new(
client, r.json as Map<String, dynamic>);
new ReadyEvent._new(client);
});
} else {
new ReadyEvent._new(client);
}
}
testReady();
}
});
}
Expand All @@ -87,4 +64,35 @@ class _WS {
if (index + 1 != this.client._options.shardIds.length)
new Timer(new Duration(seconds: 6), () => connectShard(index + 1));
}

void testReady() {
bool match = true;
client.guilds.forEach((String id, Guild o) {
if (client._options.forceFetchMembers) {
if (o == null || o.members.length != o.memberCount) match = false;
} else {
if (o == null) match = false;
}
});

bool match2 = true;
this.client.shards.forEach((int id, Shard s) {
if (!s.ready) match = false;
});

if (match && match2) {
client.ready = true;
if (client.user.bot) {
client._http
.get('/oauth2/applications/@me', true)
.then((_HttpResponse r) {
client.app = new ClientOAuth2Application._new(
client, r.json as Map<String, dynamic>);
new ReadyEvent._new(client);
});
} else {
new ReadyEvent._new(client);
}
}
}
}
8 changes: 7 additions & 1 deletion lib/src/objects/ClientOptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class ClientOptions {
/// Ex: `onMessageUpdate`.
bool ignoreUncachedEvents;

/// Whether or not to force fetch all of the members the client can see.
/// Can slow down ready times but is recommended if you rely on `Message.member`
/// or the member cache.
bool forceFetchMembers;

/// A list of discord formatted events to be disabled. Note: some of these events
/// can be dangerous to disable. Ex: `TYPING_START`
List<String> disabledEvents;
Expand All @@ -34,5 +39,6 @@ class ClientOptions {
this.shardCount: 1,
this.disabledEvents: const [],
this.messageCacheSize: 200,
this.ignoreUncachedEvents: true});
this.ignoreUncachedEvents: true,
this.forceFetchMembers: true});
}
12 changes: 11 additions & 1 deletion lib/src/objects/Shard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ class Shard extends _BaseObj {
this.onReady.add(this);
break;

case 'GUILD_MEMBERS_CHUNK':
json['d']['members'].forEach((Map<String, dynamic> o) {
new Member._new(this._ws.client, o,
this._ws.client.guilds[json['d']['guild_id']]);
});
if (!_ws.client.ready) {
_ws.testReady();
}
break;

case 'MESSAGE_CREATE':
MessageEvent msgEvent =
new MessageEvent._new(this._ws.client, json);
Expand All @@ -156,7 +166,7 @@ class Shard extends _BaseObj {
break;

case 'GUILD_CREATE':
new GuildCreateEvent._new(this._ws.client, json, this._ws);
new GuildCreateEvent._new(this._ws.client, json, this);
break;

case 'GUILD_UPDATE':
Expand Down

0 comments on commit db9c70f

Please sign in to comment.