Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds new constants and unify Constants usage #569

Merged
merged 8 commits into from Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion index.d.ts
Expand Up @@ -356,7 +356,31 @@ declare namespace Eris {
INTEGRATION_UPDATE: 81;
INTEGRATION_DELETE: 82;
};
}
MessageTypes: {
DEFAULT: 0;
RECIPIENT_ADD: 1;
RECIPIENT_REMOVE: 2;
CALL: 3;
CHANNEL_NAME_CHANGE: 4;
CHANNEL_ICON_CHANGE: 5;
CHANNEL_PINNED_MESSAGE: 6;
GUILD_MEMBER_JOIN: 7;
USER_PREMIUM_GUILD_SUBSCRIPTION: 8;
USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1: 9;
USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2: 10;
USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3: 11;
CHANNEL_FOLLOW_ADD: 12;
};
ChannelTypes: {
GUILD_TEXT: 0;
DM: 1;
GUILD_VOICE: 2;
GROUP_DM: 3;
GUILD_CATEGORY: 4;
GUILD_NEWS: 5;
GUILD_STORE: 6;
};
}

export const Constants: Constants;

Expand Down
26 changes: 26 additions & 0 deletions lib/Constants.js
Expand Up @@ -172,3 +172,29 @@ module.exports.MessageFlags = {
IS_CROSSPOST: 1 << 1,
SUPPRESS_EMBEDS: 1 << 2
};

module.exports.MessageTypes = {
DEFAULT: 0,
RECIPIENT_ADD: 1,
RECIPIENT_REMOVE: 2,
CALL: 3,
CHANNEL_NAME_CHANGE: 4,
CHANNEL_ICON_CHANGE: 5,
CHANNEL_PINNED_MESSAGE: 6,
GUILD_MEMBER_JOIN: 7,
USER_PREMIUM_GUILD_SUBSCRIPTION: 8,
USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1: 9,
USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2: 10,
USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3: 11,
CHANNEL_FOLLOW_ADD: 12
};

module.exports.ChannelTypes = {
GUILD_TEXT: 0,
DM: 1,
GUILD_VOICE: 2,
GROUP_DM: 3,
GUILD_CATEGORY: 4,
GUILD_NEWS: 5,
GUILD_STORE: 6
};
45 changes: 22 additions & 23 deletions lib/gateway/Shard.js
Expand Up @@ -6,9 +6,8 @@ const Channel = require("../structures/Channel");
const GroupChannel = require("../structures/GroupChannel");
const GuildChannel = require("../structures/GuildChannel");
const PrivateChannel = require("../structures/PrivateChannel");
const Constants = require("../Constants");
const {GATEWAY_VERSION, GatewayOPCodes, ChannelTypes} = require("../Constants");
const ExtendedUser = require("../structures/ExtendedUser");
const OPCodes = Constants.GatewayOPCodes;
const User = require("../structures/User");
let WebSocket = typeof window !== "undefined" ? window.WebSocket : require("ws");

Expand Down Expand Up @@ -172,7 +171,7 @@ class Shard extends EventEmitter {

resume() {
this.status = "resuming";
this.sendWS(OPCodes.RESUME, {
this.sendWS(GatewayOPCodes.RESUME, {
token: this.client.token,
session_id: this.sessionID,
seq: this.seq
Expand All @@ -186,7 +185,7 @@ class Shard extends EventEmitter {
}
const identify = {
token: this.client.token,
v: Constants.GATEWAY_VERSION,
v: GATEWAY_VERSION,
compress: !!this.client.options.compress,
large_threshold: this.client.options.largeThreshold,
guild_subscriptions: !!this.client.options.guildSubscriptions,
Expand All @@ -202,7 +201,7 @@ class Shard extends EventEmitter {
if(this.presence.status) {
identify.presence = this.presence;
}
this.sendWS(OPCodes.IDENTIFY, identify, true);
this.sendWS(GatewayOPCodes.IDENTIFY, identify, true);
}

wsEvent(packet) {
Expand Down Expand Up @@ -326,7 +325,7 @@ class Shard extends EventEmitter {
}
const member = guild.members.get(packet.d.id = packet.d.user_id);
if(!member) {
const channel = guild.channels.find((channel) => channel.type === 2 && channel.voiceMembers.get(packet.d.id));
const channel = guild.channels.find((channel) => channel.type === ChannelTypes.GUILD_VOICE && channel.voiceMembers.get(packet.d.id));
if(channel) {
channel.voiceMembers.remove(packet.d);
this.emit("debug", "VOICE_STATE_UPDATE member null but in channel: " + packet.d.id, this.id);
Expand All @@ -353,7 +352,7 @@ class Shard extends EventEmitter {
if(oldChannelID) {
oldChannel = guild.channels.get(oldChannelID);
}
if(packet.d.channel_id && (newChannel = guild.channels.get(packet.d.channel_id)) && newChannel.type === 2) { // Welcome to Discord, where one can "join" text channels
if(packet.d.channel_id && (newChannel = guild.channels.get(packet.d.channel_id)) && newChannel.type === ChannelTypes.GUILD_VOICE) { // Welcome to Discord, where one can "join" text channels
if(oldChannel) {
/**
* Fired when a guild member switches voice channels
Expand Down Expand Up @@ -966,7 +965,7 @@ class Shard extends EventEmitter {
break;
}
case "CHANNEL_DELETE": {
if(packet.d.type === 1 || packet.d.type === undefined) {
if(packet.d.type === ChannelTypes.DM || packet.d.type === undefined) {
if(this.id === 0) {
const channel = this.client.privateChannels.remove(packet.d);
if(channel) {
Expand All @@ -990,13 +989,13 @@ class Shard extends EventEmitter {
if(!channel) {
break;
}
if(channel.type === 2) {
if(channel.type === ChannelTypes.GUILD_VOICE) {
channel.voiceMembers.forEach((member) => {
this.emit("voiceChannelLeave", channel.voiceMembers.remove(member), channel);
});
}
this.emit("channelDelete", channel);
} else if(packet.d.type === 3) {
} else if(packet.d.type === ChannelTypes.GROUP_DM) {
if(this.id === 0) {
this.emit("channelDelete", this.client.groupChannels.remove(packet.d));
}
Expand Down Expand Up @@ -1249,10 +1248,10 @@ class Shard extends EventEmitter {
});

packet.d.private_channels.forEach((channel) => {
if(channel.type === undefined || channel.type === 1) {
if(channel.type === undefined || channel.type === ChannelTypes.DM) {
this.client.privateChannelMap[channel.recipients[0].id] = channel.id;
this.client.privateChannels.add(channel, this.client, true);
} else if(channel.type === 3) {
} else if(channel.type === ChannelTypes.GROUP_DM) {
this.client.groupChannels.add(channel, this.client, true);
} else {
this.emit("warn", new Error("Unhandled READY private_channel type: " + JSON.stringify(channel, null, 2)));
Expand Down Expand Up @@ -1456,7 +1455,7 @@ class Shard extends EventEmitter {
}

requestGuildSync(guildID) {
this.sendWS(OPCodes.SYNC_GUILD, guildID);
this.sendWS(GatewayOPCodes.SYNC_GUILD, guildID);
}

createGuild(_guild) {
Expand Down Expand Up @@ -1502,7 +1501,7 @@ class Shard extends EventEmitter {
}

requestGuildMembers(guildID, query, limit) {
this.sendWS(OPCodes.GET_GUILD_MEMBERS, {
this.sendWS(GatewayOPCodes.GET_GUILD_MEMBERS, {
guild_id: guildID,
query: query || "",
limit: limit || 0
Expand Down Expand Up @@ -1681,30 +1680,30 @@ class Shard extends EventEmitter {
}

switch(packet.op) {
case OPCodes.EVENT: {
case GatewayOPCodes.EVENT: {
if(!this.client.options.disableEvents[packet.t]) {
this.wsEvent(packet);
}
break;
}
case OPCodes.HEARTBEAT: {
case GatewayOPCodes.HEARTBEAT: {
this.heartbeat();
break;
}
case OPCodes.INVALID_SESSION: {
case GatewayOPCodes.INVALID_SESSION: {
this.seq = 0;
this.sessionID = null;
this.emit("warn", "Invalid session, reidentifying!", this.id);
this.identify();
break;
}
case OPCodes.RECONNECT: {
case GatewayOPCodes.RECONNECT: {
this.disconnect({
reconnect: "auto"
});
break;
}
case OPCodes.HELLO: {
case GatewayOPCodes.HELLO: {
if(packet.d.heartbeat_interval > 0) {
if(this.heartbeatInterval) {
clearInterval(this.heartbeatInterval);
Expand All @@ -1730,7 +1729,7 @@ class Shard extends EventEmitter {
this.emit("hello", packet.d._trace, this.id);
break; /* eslint-enable no-unreachable */
}
case OPCodes.HEARTBEAT_ACK: {
case GatewayOPCodes.HEARTBEAT_ACK: {
this.lastHeartbeatAck = true;
this.lastHeartbeatReceived = new Date().getTime();
break;
Expand All @@ -1750,7 +1749,7 @@ class Shard extends EventEmitter {
}
this.lastHeartbeatAck = false;
this.lastHeartbeatSent = new Date().getTime();
this.sendWS(OPCodes.HEARTBEAT, this.seq, true);
this.sendWS(GatewayOPCodes.HEARTBEAT, this.seq, true);
}

sendWS(op, _data) {
Expand All @@ -1764,7 +1763,7 @@ class Shard extends EventEmitter {
this.emit("debug", JSON.stringify({op: op, d: _data}), this.id);
}
};
if(op === OPCodes.STATUS_UPDATE) {
if(op === GatewayOPCodes.STATUS_UPDATE) {
++waitFor;
this.presenceUpdateBucket.queue(func);
}
Expand Down Expand Up @@ -1815,7 +1814,7 @@ class Shard extends EventEmitter {
}

sendStatusUpdate() {
this.sendWS(OPCodes.STATUS_UPDATE, {
this.sendWS(GatewayOPCodes.STATUS_UPDATE, {
afk: !!this.presence.afk, // For push notifications
game: this.presence.game,
since: this.presence.status === "idle" ? Date.now() : 0,
Expand Down
4 changes: 2 additions & 2 deletions lib/rest/Endpoints.js
@@ -1,8 +1,8 @@
"use strict";

const Constants = require("../Constants");
const {REST_VERSION} = require("../Constants");

module.exports.BASE_URL = "/api/v" + Constants.REST_VERSION;
module.exports.BASE_URL = "/api/v" + REST_VERSION;
module.exports.CDN_URL = "https://cdn.discordapp.com";

module.exports.CHANNEL = (chanID) => `/channels/${chanID}`;
Expand Down
15 changes: 8 additions & 7 deletions lib/structures/Channel.js
@@ -1,6 +1,7 @@
"use strict";

const Base = require("./Base");
const {ChannelTypes} = require("../Constants");

/**
* Represents a channel. You also probably want to look at CategoryChannel, GroupChannel, NewsChannel, PrivateChannel, TextChannel, and VoiceChannel.
Expand All @@ -21,25 +22,25 @@ class Channel extends Base {

static from(data, client) {
switch(data.type) {
case 0: {
case ChannelTypes.GUILD_TEXT: {
return new TextChannel(data, client);
}
case 1: {
case ChannelTypes.DM: {
return new PrivateChannel(data, client);
}
case 2: {
case ChannelTypes.GUILD_VOICE: {
return new VoiceChannel(data, client);
}
case 3: {
case ChannelTypes.GROUP_DM: {
return new GroupChannel(data, client);
}
case 4: {
case ChannelTypes.GUILD_CATEGORY: {
return new CategoryChannel(data, client);
}
case 5: {
case ChannelTypes.GUILD_NEWS: {
return new NewsChannel(data, client);
}
case 6: {
case ChannelTypes.GUILD_STORE: {
return new StoreChannel(data, client);
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/structures/GroupChannel.js
@@ -1,7 +1,7 @@
"use strict";

const Collection = require("../util/Collection");
const Constants = require("../Constants");
const {ImageFormats, ImageSizeBoundaries} = require("../Constants");
const Endpoints = require("../rest/Endpoints");
const PrivateChannel = require("./PrivateChannel");
const User = require("./User");
Expand Down Expand Up @@ -75,12 +75,12 @@ class GroupChannel extends PrivateChannel { // (╯°□°)╯︵ ┻━┻
* @arg {Number} [size] The size of the icon (any power of two between 16 and 2048)
*/
dynamicIconURL(format, size) {
if(!format || !Constants.ImageFormats.includes(format.toLowerCase())) {
if(!format || !ImageFormats.includes(format.toLowerCase())) {
format = this._client.options.defaultImageFormat;
}
if(
size < Constants.ImageSizeBoundaries.MINIMUM ||
size > Constants.ImageSizeBoundaries.MAXIMUM ||
size < ImageSizeBoundaries.MINIMUM ||
size > ImageSizeBoundaries.MAXIMUM ||
(size & (size - 1))
) {
size = this._client.options.defaultImageSize;
Expand Down
20 changes: 10 additions & 10 deletions lib/structures/Guild.js
Expand Up @@ -3,7 +3,7 @@
const Base = require("./Base");
const Channel = require("./Channel");
const {CDN_URL} = require("../rest/Endpoints");
const Constants = require("../Constants");
const {ImageFormats, ImageSizeBoundaries} = require("../Constants");
const Collection = require("../util/Collection");
const GuildChannel = require("./GuildChannel");
const Member = require("./Member");
Expand Down Expand Up @@ -164,12 +164,12 @@ class Guild extends Base {
* @arg {Number} [size] The size of the icon (any power of two between 16 and 2048)
*/
dynamicIconURL(format, size) {
if(!format || !Constants.ImageFormats.includes(format.toLowerCase())) {
if(!format || !ImageFormats.includes(format.toLowerCase())) {
format = this.shard.client.options.defaultImageFormat;
}
if(
size < Constants.ImageSizeBoundaries.MINIMUM ||
size > Constants.ImageSizeBoundaries.MAXIMUM ||
size < ImageSizeBoundaries.MINIMUM ||
size > ImageSizeBoundaries.MAXIMUM ||
(size & (size - 1))
) {
size = this.shard.client.options.defaultImageSize;
Expand All @@ -191,12 +191,12 @@ class Guild extends Base {
* @param {Number} [size] The size of the icon (any power of two between 16 and 2048)
*/
dynamicSplashURL(format,size){
if(!format || !Constants.ImageFormats.includes(format.toLowerCase())){
if(!format || !ImageFormats.includes(format.toLowerCase())){
format = this.shard.client.options.defaultImageFormat;
}
if(
size < Constants.ImageSizeBoundaries.MINIMUM ||
size > Constants.ImageSizeBoundaries.MAXIMUM ||
size < ImageSizeBoundaries.MINIMUM ||
size > ImageSizeBoundaries.MAXIMUM ||
(size & (size - 1))
) {
size = this.shard.client.options.defaultImageSize;
Expand All @@ -210,12 +210,12 @@ class Guild extends Base {
* @param {Number} [size] The size of the icon (any power of two between 16 and 2048)
*/
dynamicBannerURL(format,size){
if(!format || !Constants.ImageFormats.includes(format.toLowerCase())){
if(!format || !ImageFormats.includes(format.toLowerCase())){
format = this.shard.client.options.defaultImageFormat;
}
if(
size < Constants.ImageSizeBoundaries.MINIMUM ||
size > Constants.ImageSizeBoundaries.MAXIMUM ||
size < ImageSizeBoundaries.MINIMUM ||
size > ImageSizeBoundaries.MAXIMUM ||
(size & (size - 1))
) {
size = this.shard.client.options.defaultImageSize;
Expand Down
4 changes: 2 additions & 2 deletions lib/structures/GuildAuditLogEntry.js
Expand Up @@ -2,7 +2,7 @@

const Base = require("./Base");
const Invite = require("./Invite");
const Constants = require("../Constants");
const {AuditLogActions} = require("../Constants");

/**
* Represents a guild audit log entry describing a moderation action
Expand Down Expand Up @@ -91,7 +91,7 @@ class GuildAuditLogEntry extends Base {
} else if(this.actionType < 20) { // Channel
return this.guild && this.guild.channels.get(this.targetID);
} else if(this.actionType < 30) { // Member
if(this.actionType === Constants.AuditLogActions.MEMBER_MOVE || this.actionType === Constants.AuditLogActions.MEMBER_DISCONNECT) { // MEMBER_MOVE / MEMBER_DISCONNECT
if(this.actionType === AuditLogActions.MEMBER_MOVE || this.actionType === AuditLogActions.MEMBER_DISCONNECT) { // MEMBER_MOVE / MEMBER_DISCONNECT
return null;
}
return this.guild && this.guild.members.get(this.targetID);
Expand Down