Skip to content

Commit

Permalink
Implement gateway intents; discord/discord-api-docs#1307
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
l7ssha committed Apr 9, 2020
1 parent e88fdfe commit 5380c4e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
56 changes: 55 additions & 1 deletion nyxx/lib/src/ClientOptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class ClientOptions {
/// List of ignored events
List<String> ignoredEvents;

GatewayIntents? gatewayIntents;

/// Makes a new `ClientOptions` object.
ClientOptions(
{this.disableEveryone = false,
Expand All @@ -40,5 +42,57 @@ class ClientOptions {
this.forceFetchMembers = false,
this.cacheMembers = true,
this.largeThreshold = 50,
this.ignoredEvents = const []});
this.ignoredEvents = const [],
this.gatewayIntents});
}

/// When identifying to the gateway, you can specify an intents parameter which
/// allows you to conditionally subscribe to pre-defined "intents", groups of events defined by Discord.
/// If you do not specify a certain intent, you will not receive any of the gateway events that are batched into that group.
/// [Reference](https://discordapp.com/developers/docs/topics/gateway#gateway-intents)
class GatewayIntents {
bool guilds = false;
bool guildMembers = false;
bool guildBans = false;
bool guildEmojis = false;
bool guildIntegrations = false;
bool guildWebhooks = false;
bool guildInvites = false;
bool guildVoiceState = false;
bool guildPresences = false;
bool guildMessageReactions = false;
bool guildMessageTyping = false;
bool directMessages = false;
bool directMessageReactions = false;
bool directMessageTyping = false;

bool _all = false;

GatewayIntents();
GatewayIntents.all() : _all = true;

int _calculate() {
if(_all) {
return 0x7FFF;
}

int value = 0;

if(guilds) value += 1 << 0;
if(guildMembers) value += 1 << 1;
if(guildBans) value += 1 << 2;
if(guildEmojis) value += 1 << 3;
if(guildIntegrations) value += 1 << 4;
if(guildWebhooks) value += 1 << 5;
if(guildInvites) value += 1 << 6;
if(guildVoiceState) value += 1 << 8;
if(guildPresences) value += 1 << 9;
if(guildMessageReactions) value += 1 << 10;
if(guildMessageTyping) value += 1 << 11;
if(directMessages) value += 1 << 12;
if(directMessageReactions) value += 1 << 13;
if(directMessageTyping) value += 1 << 14;

return value;
}
}
12 changes: 11 additions & 1 deletion nyxx/lib/src/Shard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,18 @@ class Shard implements Disposable {
"\$device": "nyxx",
},
"large_threshold": this._ws._client._options.largeThreshold,
"compress": !browser
"compress": !browser,
};

if(_ws._client._options.gatewayIntents != null) {
identifyMsg["intents"] = _ws._client._options.gatewayIntents!._calculate();
}

identifyMsg['shard'] = <int>[
this.id,
_ws._client._options.shardCount
];

this.send("IDENTIFY", identifyMsg);
} else if (resume) {
this.send("RESUME", <String, dynamic>{
Expand Down Expand Up @@ -410,6 +415,11 @@ class Shard implements Disposable {
case 4010:
exit(1);
break;
case 4013:
_logger.shout("Cannot connect to gateway due intent value is invalid. "
"Check https://discordapp.com/developers/docs/topics/gateway#gateway-intents for more info.");
exit(1);
break;
case 4007:
case 4009:
Future.delayed(const Duration(seconds: 3), () => this._connect(true));
Expand Down

0 comments on commit 5380c4e

Please sign in to comment.