Skip to content

Commit 8d0fc8e

Browse files
committed
Resilience to abusers of the API
For some reason there's a way to join text channels via the Discord API but not the Discord Client, so this commit prevents the Client from crashing by checking to see if the channels are voice channels.
1 parent f9f7a56 commit 8d0fc8e

27 files changed

+5052
-241
lines changed

lib/Client/Client.js

Lines changed: 704 additions & 72 deletions
Large diffs are not rendered by default.

lib/Client/ConnectionState.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
"use strict";exports.__esModule = true;exports["default"] = {IDLE:0,LOGGING_IN:1,LOGGED_IN:2,READY:3,DISCONNECTED:4};module.exports = exports["default"];
1+
"use strict";
2+
3+
exports.__esModule = true;
4+
exports["default"] = {
5+
IDLE: 0,
6+
LOGGING_IN: 1,
7+
LOGGED_IN: 2,
8+
READY: 3,
9+
DISCONNECTED: 4
10+
};
11+
module.exports = exports["default"];

lib/Client/InternalClient.js

Lines changed: 1647 additions & 75 deletions
Large diffs are not rendered by default.

lib/Client/Resolver/Resolver.js

Lines changed: 230 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,230 @@
1-
"use strict"; /* global Buffer */exports.__esModule = true;function _interopRequireDefault(obj){return obj && obj.__esModule?obj:{"default":obj};}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var _fs=require("fs");var _fs2=_interopRequireDefault(_fs);var _StructuresUser=require("../../Structures/User");var _StructuresUser2=_interopRequireDefault(_StructuresUser);var _StructuresChannel=require("../../Structures/Channel");var _StructuresChannel2=_interopRequireDefault(_StructuresChannel);var _StructuresTextChannel=require("../../Structures/TextChannel");var _StructuresTextChannel2=_interopRequireDefault(_StructuresTextChannel);var _StructuresVoiceChannel=require("../../Structures/VoiceChannel");var _StructuresVoiceChannel2=_interopRequireDefault(_StructuresVoiceChannel);var _StructuresServerChannel=require("../../Structures/ServerChannel");var _StructuresServerChannel2=_interopRequireDefault(_StructuresServerChannel);var _StructuresPMChannel=require("../../Structures/PMChannel");var _StructuresPMChannel2=_interopRequireDefault(_StructuresPMChannel);var _StructuresServer=require("../../Structures/Server");var _StructuresServer2=_interopRequireDefault(_StructuresServer);var _StructuresMessage=require("../../Structures/Message");var _StructuresMessage2=_interopRequireDefault(_StructuresMessage);var _StructuresInvite=require("../../Structures/Invite");var _StructuresInvite2=_interopRequireDefault(_StructuresInvite);var Resolver=(function(){function Resolver(internal){_classCallCheck(this,Resolver);this.internal = internal;}Resolver.prototype.resolveToBase64 = function resolveToBase64(resource){if(resource instanceof Buffer){resource = resource.toString("base64");resource = "data:image/jpg;base64," + resource;}return resource;};Resolver.prototype.resolveInviteID = function resolveInviteID(resource){if(resource instanceof _StructuresInvite2["default"]){return resource.id;}if(typeof resource === "string" || resource instanceof String){if(resource.indexOf("http") === 0){var split=resource.split("/");return split.pop();}return resource;}return null;};Resolver.prototype.resolveServer = function resolveServer(resource){if(resource instanceof _StructuresServer2["default"]){return resource;}if(resource instanceof _StructuresServerChannel2["default"]){return resource.server;}if(resource instanceof String || typeof resource === "string"){return this.internal.servers.get("id",resource);}if(resource instanceof _StructuresMessage2["default"]){if(resource.channel instanceof _StructuresTextChannel2["default"]){return resource.server;}}return null;};Resolver.prototype.resolveFile = function resolveFile(resource){if(typeof resource === "string" || resource instanceof String){return _fs2["default"].createReadStream(resource);}return resource;};Resolver.prototype.resolveMentions = function resolveMentions(resource){ // resource is a string
2-
var _mentions=[];var changed=resource;for(var _iterator=resource.match(/<@[^>]*>/g) || [],_isArray=Array.isArray(_iterator),_i=0,_iterator=_isArray?_iterator:_iterator[Symbol.iterator]();;) {var _ref;if(_isArray){if(_i >= _iterator.length)break;_ref = _iterator[_i++];}else {_i = _iterator.next();if(_i.done)break;_ref = _i.value;}var mention=_ref;var userID=mention.substring(2,mention.length - 1);_mentions.push(userID);changed = changed.replace(new RegExp(mention,"g"),"@" + this.internal.client.users.get("id",userID).username);}return [_mentions,changed];};Resolver.prototype.resolveString = function resolveString(resource){ // accepts Array, Channel, Server, User, Message, String and anything
3-
// toString()-able
4-
var final=resource;if(resource instanceof Array){final = resource.join("\n");}return final.toString();};Resolver.prototype.resolveUser = function resolveUser(resource){ /*
5-
accepts a Message, Channel, Server, String ID, User, PMChannel
6-
*/if(resource instanceof _StructuresUser2["default"]){return resource;}if(resource instanceof _StructuresMessage2["default"]){return resource.author;}if(resource instanceof _StructuresTextChannel2["default"]){var lmsg=resource.lastMessage;if(lmsg){return lmsg.author;}}if(resource instanceof _StructuresServer2["default"]){return resource.owner;}if(resource instanceof _StructuresPMChannel2["default"]){return resource.recipient;}if(resource instanceof String || typeof resource === "string"){return this.internal.users.get("id",resource);}return null;};Resolver.prototype.resolveMessage = function resolveMessage(resource){ // accepts a Message, PMChannel & TextChannel
7-
if(resource instanceof _StructuresTextChannel2["default"] || resource instanceof _StructuresPMChannel2["default"]){return resource.lastMessage;}if(resource instanceof _StructuresMessage2["default"]){return resource;}return null;};Resolver.prototype.resolveVoiceChannel = function resolveVoiceChannel(resource){ // resolveChannel will also work but this is more apt
8-
if(resource instanceof _StructuresVoiceChannel2["default"]){return resource;}return null;};Resolver.prototype.resolveChannel = function resolveChannel(resource){ /*
9-
accepts a Message, Channel, Server, String ID, User
10-
*/if(resource instanceof _StructuresMessage2["default"]){return Promise.resolve(resource.channel);}if(resource instanceof _StructuresChannel2["default"]){return Promise.resolve(resource);}if(resource instanceof _StructuresServer2["default"]){return Promise.resolve(resource.channels.get("id",resource.id));}if(resource instanceof String || typeof resource === "string"){return Promise.resolve(this.internal.channels.get("id",resource));}if(resource instanceof _StructuresUser2["default"]){ // see if a PM exists
11-
var chatFound=this.internal.private_channels.find(function(pmchat){return pmchat.recipient.equals(resource);});if(chatFound){ // a PM already exists!
12-
return Promise.resolve(chatFound);} // PM does not exist :\
13-
return this.internal.startPM(resource);}var error=new Error("Could not resolve channel");error.resource = resource;return Promise.reject(error);};return Resolver;})();exports["default"] = Resolver;module.exports = exports["default"];
1+
"use strict";
2+
/* global Buffer */
3+
4+
exports.__esModule = true;
5+
6+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
7+
8+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
9+
10+
var _fs = require("fs");
11+
12+
var _fs2 = _interopRequireDefault(_fs);
13+
14+
var _StructuresUser = require("../../Structures/User");
15+
16+
var _StructuresUser2 = _interopRequireDefault(_StructuresUser);
17+
18+
var _StructuresChannel = require("../../Structures/Channel");
19+
20+
var _StructuresChannel2 = _interopRequireDefault(_StructuresChannel);
21+
22+
var _StructuresTextChannel = require("../../Structures/TextChannel");
23+
24+
var _StructuresTextChannel2 = _interopRequireDefault(_StructuresTextChannel);
25+
26+
var _StructuresVoiceChannel = require("../../Structures/VoiceChannel");
27+
28+
var _StructuresVoiceChannel2 = _interopRequireDefault(_StructuresVoiceChannel);
29+
30+
var _StructuresServerChannel = require("../../Structures/ServerChannel");
31+
32+
var _StructuresServerChannel2 = _interopRequireDefault(_StructuresServerChannel);
33+
34+
var _StructuresPMChannel = require("../../Structures/PMChannel");
35+
36+
var _StructuresPMChannel2 = _interopRequireDefault(_StructuresPMChannel);
37+
38+
var _StructuresServer = require("../../Structures/Server");
39+
40+
var _StructuresServer2 = _interopRequireDefault(_StructuresServer);
41+
42+
var _StructuresMessage = require("../../Structures/Message");
43+
44+
var _StructuresMessage2 = _interopRequireDefault(_StructuresMessage);
45+
46+
var _StructuresInvite = require("../../Structures/Invite");
47+
48+
var _StructuresInvite2 = _interopRequireDefault(_StructuresInvite);
49+
50+
var Resolver = (function () {
51+
function Resolver(internal) {
52+
_classCallCheck(this, Resolver);
53+
54+
this.internal = internal;
55+
}
56+
57+
Resolver.prototype.resolveToBase64 = function resolveToBase64(resource) {
58+
if (resource instanceof Buffer) {
59+
resource = resource.toString("base64");
60+
resource = "data:image/jpg;base64," + resource;
61+
}
62+
return resource;
63+
};
64+
65+
Resolver.prototype.resolveInviteID = function resolveInviteID(resource) {
66+
if (resource instanceof _StructuresInvite2["default"]) {
67+
return resource.id;
68+
}
69+
if (typeof resource === "string" || resource instanceof String) {
70+
if (resource.indexOf("http") === 0) {
71+
var split = resource.split("/");
72+
return split.pop();
73+
}
74+
return resource;
75+
}
76+
return null;
77+
};
78+
79+
Resolver.prototype.resolveServer = function resolveServer(resource) {
80+
if (resource instanceof _StructuresServer2["default"]) {
81+
return resource;
82+
}
83+
if (resource instanceof _StructuresServerChannel2["default"]) {
84+
return resource.server;
85+
}
86+
if (resource instanceof String || typeof resource === "string") {
87+
return this.internal.servers.get("id", resource);
88+
}
89+
if (resource instanceof _StructuresMessage2["default"]) {
90+
if (resource.channel instanceof _StructuresTextChannel2["default"]) {
91+
return resource.server;
92+
}
93+
}
94+
return null;
95+
};
96+
97+
Resolver.prototype.resolveFile = function resolveFile(resource) {
98+
if (typeof resource === "string" || resource instanceof String) {
99+
return _fs2["default"].createReadStream(resource);
100+
}
101+
return resource;
102+
};
103+
104+
Resolver.prototype.resolveMentions = function resolveMentions(resource) {
105+
// resource is a string
106+
var _mentions = [];
107+
var changed = resource;
108+
for (var _iterator = resource.match(/<@[^>]*>/g) || [], _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
109+
var _ref;
110+
111+
if (_isArray) {
112+
if (_i >= _iterator.length) break;
113+
_ref = _iterator[_i++];
114+
} else {
115+
_i = _iterator.next();
116+
if (_i.done) break;
117+
_ref = _i.value;
118+
}
119+
120+
var mention = _ref;
121+
122+
var userID = mention.substring(2, mention.length - 1);
123+
_mentions.push(userID);
124+
changed = changed.replace(new RegExp(mention, "g"), "@" + this.internal.client.users.get("id", userID).username);
125+
}
126+
return [_mentions, changed];
127+
};
128+
129+
Resolver.prototype.resolveString = function resolveString(resource) {
130+
131+
// accepts Array, Channel, Server, User, Message, String and anything
132+
// toString()-able
133+
134+
var final = resource;
135+
if (resource instanceof Array) {
136+
final = resource.join("\n");
137+
}
138+
139+
return final.toString();
140+
};
141+
142+
Resolver.prototype.resolveUser = function resolveUser(resource) {
143+
/*
144+
accepts a Message, Channel, Server, String ID, User, PMChannel
145+
*/
146+
if (resource instanceof _StructuresUser2["default"]) {
147+
return resource;
148+
}
149+
if (resource instanceof _StructuresMessage2["default"]) {
150+
return resource.author;
151+
}
152+
if (resource instanceof _StructuresTextChannel2["default"]) {
153+
var lmsg = resource.lastMessage;
154+
if (lmsg) {
155+
return lmsg.author;
156+
}
157+
}
158+
if (resource instanceof _StructuresServer2["default"]) {
159+
return resource.owner;
160+
}
161+
if (resource instanceof _StructuresPMChannel2["default"]) {
162+
return resource.recipient;
163+
}
164+
if (resource instanceof String || typeof resource === "string") {
165+
return this.internal.users.get("id", resource);
166+
}
167+
168+
return null;
169+
};
170+
171+
Resolver.prototype.resolveMessage = function resolveMessage(resource) {
172+
// accepts a Message, PMChannel & TextChannel
173+
174+
if (resource instanceof _StructuresTextChannel2["default"] || resource instanceof _StructuresPMChannel2["default"]) {
175+
return resource.lastMessage;
176+
}
177+
if (resource instanceof _StructuresMessage2["default"]) {
178+
return resource;
179+
}
180+
181+
return null;
182+
};
183+
184+
Resolver.prototype.resolveVoiceChannel = function resolveVoiceChannel(resource) {
185+
// resolveChannel will also work but this is more apt
186+
if (resource instanceof _StructuresVoiceChannel2["default"]) {
187+
return resource;
188+
}
189+
return null;
190+
};
191+
192+
Resolver.prototype.resolveChannel = function resolveChannel(resource) {
193+
/*
194+
accepts a Message, Channel, Server, String ID, User
195+
*/
196+
197+
if (resource instanceof _StructuresMessage2["default"]) {
198+
return Promise.resolve(resource.channel);
199+
}
200+
if (resource instanceof _StructuresChannel2["default"]) {
201+
return Promise.resolve(resource);
202+
}
203+
if (resource instanceof _StructuresServer2["default"]) {
204+
return Promise.resolve(resource.channels.get("id", resource.id));
205+
}
206+
if (resource instanceof String || typeof resource === "string") {
207+
return Promise.resolve(this.internal.channels.get("id", resource));
208+
}
209+
if (resource instanceof _StructuresUser2["default"]) {
210+
// see if a PM exists
211+
var chatFound = this.internal.private_channels.find(function (pmchat) {
212+
return pmchat.recipient.equals(resource);
213+
});
214+
if (chatFound) {
215+
// a PM already exists!
216+
return Promise.resolve(chatFound);
217+
}
218+
// PM does not exist :\
219+
return this.internal.startPM(resource);
220+
}
221+
var error = new Error("Could not resolve channel");
222+
error.resource = resource;
223+
return Promise.reject(error);
224+
};
225+
226+
return Resolver;
227+
})();
228+
229+
exports["default"] = Resolver;
230+
module.exports = exports["default"];

0 commit comments

Comments
 (0)