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 1 commit
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,
Khaaz marked this conversation as resolved.
Show resolved Hide resolved
USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2: 10,
Khaaz marked this conversation as resolved.
Show resolved Hide resolved
USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3: 11,
Khaaz marked this conversation as resolved.
Show resolved Hide resolved
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
};
31 changes: 15 additions & 16 deletions lib/gateway/Shard.js
Expand Up @@ -3,10 +3,9 @@
const Bucket = require("../util/Bucket");
const Call = require("../structures/Call");
const CategoryChannel = require("../structures/CategoryChannel");
const Constants = require("../Constants");
const {GATEWAY_VERSION, GatewayOPCodes} = require("../Constants");
const ExtendedUser = require("../structures/ExtendedUser");
const GuildChannel = require("../structures/GuildChannel");
const OPCodes = Constants.GatewayOPCodes;
const TextChannel = require("../structures/TextChannel");
const NewsChannel = require("../structures/NewsChannel");
const User = require("../structures/User");
Expand Down Expand Up @@ -168,7 +167,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 @@ -182,7 +181,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 @@ -198,7 +197,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 @@ -1473,7 +1472,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 @@ -1519,7 +1518,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 @@ -1694,30 +1693,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 @@ -1743,7 +1742,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 @@ -1763,7 +1762,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 @@ -1777,7 +1776,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 @@ -1828,7 +1827,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