Resolve role IDs

This commit is contained in:
abalabahaha
2016-01-29 17:14:40 -08:00
parent ee2172c1d5
commit 8726b23730
4 changed files with 139 additions and 55 deletions

View File

@@ -751,6 +751,7 @@ var InternalClient = (function () {
InternalClient.prototype.updateRole = function updateRole(role, data) {
var _this22 = this;
role = this.resolver.resolveRole(role);
var server = this.resolver.resolveServer(role.server);
var newData = {
@@ -798,6 +799,8 @@ var InternalClient = (function () {
//def addMemberToRole
InternalClient.prototype.addMemberToRole = function addMemberToRole(member, roles) {
var _this23 = this;
member = this.resolver.resolveUser(member);
if (!member) {
@@ -805,11 +808,16 @@ var InternalClient = (function () {
}
if (!Array.isArray(roles) || roles.length === 0) {
if (roles instanceof _StructuresRole2["default"]) {
roles = this.resolve.resolveRole(roles);
if (roles) {
roles = [roles];
} else {
return Promise.reject(new Error("invalid array of roles"));
}
} else {
roles = roles.map(function (r) {
return _this23.resolver.resolveRole(r);
});
}
if (roles.some(function (role) {
@@ -834,8 +842,12 @@ var InternalClient = (function () {
};
InternalClient.prototype.memberHasRole = function memberHasRole(member, role) {
role = this.resolver.resolveRole(role);
member = this.resolver.resolveUser(member);
if (!role) {
throw new Error("invalid role");
}
if (!member) {
throw new Error("user not found");
}
@@ -848,6 +860,8 @@ var InternalClient = (function () {
//def removeMemberFromRole
InternalClient.prototype.removeMemberFromRole = function removeMemberFromRole(member, roles) {
var _this24 = this;
member = this.resolver.resolveUser(member);
if (!member) {
@@ -855,11 +869,16 @@ var InternalClient = (function () {
}
if (!Array.isArray(roles) || roles.length === 0) {
if (roles instanceof _StructuresRole2["default"]) {
roles = this.resolve.resolveRole(roles);
if (roles) {
roles = [roles];
} else {
return Promise.reject(new Error("invalid array of roles"));
}
} else {
roles = roles.map(function (r) {
return _this24.resolver.resolveRole(r);
});
}
var roleIDS = roles[0].server.memberMap[member.id].roles.map(function (r) {
@@ -899,7 +918,7 @@ var InternalClient = (function () {
// def createInvite
InternalClient.prototype.createInvite = function createInvite(chanServ, options) {
var _this23 = this;
var _this25 = this;
if (chanServ instanceof _StructuresChannel2["default"]) {
// do something
@@ -932,7 +951,7 @@ var InternalClient = (function () {
}
return this.apiRequest("post", epoint, true, options).then(function (res) {
return new _StructuresInvite2["default"](res, _this23.channels.get("id", res.channel.id), _this23.client);
return new _StructuresInvite2["default"](res, _this25.channels.get("id", res.channel.id), _this25.client);
});
};
@@ -949,7 +968,7 @@ var InternalClient = (function () {
//def getInvite
InternalClient.prototype.getInvite = function getInvite(invite) {
var _this24 = this;
var _this26 = this;
invite = this.resolver.resolveInviteID(invite);
if (!invite) {
@@ -957,11 +976,11 @@ var InternalClient = (function () {
}
return this.apiRequest("get", _Constants.Endpoints.INVITE(invite), true).then(function (res) {
if (!_this24.channels.has("id", res.channel.id)) {
return new _StructuresInvite2["default"](res, null, _this24.client);
if (!_this26.channels.has("id", res.channel.id)) {
return new _StructuresInvite2["default"](res, null, _this26.client);
}
return _this24.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(res.channel.id), true, { validate: invite }).then(function (res2) {
return new _StructuresInvite2["default"](res2, _this24.channels.get("id", res.channel.id), _this24.client);
return _this26.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(res.channel.id), true, { validate: invite }).then(function (res2) {
return new _StructuresInvite2["default"](res2, _this26.channels.get("id", res.channel.id), _this26.client);
});
});
};
@@ -969,22 +988,22 @@ var InternalClient = (function () {
//def getInvites
InternalClient.prototype.getInvites = function getInvites(channel) {
var _this25 = this;
var _this27 = this;
if (!(channel instanceof _StructuresChannel2["default"])) {
var server = this.resolver.resolveServer(channel);
if (server) {
return this.apiRequest("get", _Constants.Endpoints.SERVER_INVITES(server.id), true).then(function (res) {
return res.map(function (data) {
return new _StructuresInvite2["default"](data, _this25.channels.get("id", data.channel.id), _this25.client);
return new _StructuresInvite2["default"](data, _this27.channels.get("id", data.channel.id), _this27.client);
});
});
}
}
return this.resolver.resolveChannel(channel).then(function (channel) {
return _this25.apiRequest("get", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true).then(function (res) {
return _this27.apiRequest("get", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true).then(function (res) {
return res.map(function (data) {
return new _StructuresInvite2["default"](data, _this25.channels.get("id", data.channel.id), _this25.client);
return new _StructuresInvite2["default"](data, _this27.channels.get("id", data.channel.id), _this27.client);
});
});
});
@@ -993,12 +1012,14 @@ var InternalClient = (function () {
//def overwritePermissions
InternalClient.prototype.overwritePermissions = function overwritePermissions(channel, role, updated) {
var _this26 = this;
var _this28 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
var user;
if (role instanceof _StructuresUser2["default"]) {
user = role;
} else {
role = _this28.resolver.resolveRole(role);
}
var data = {};
@@ -1034,7 +1055,7 @@ var InternalClient = (function () {
}
}
return _this26.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data);
return _this28.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data);
});
};
@@ -1070,49 +1091,49 @@ var InternalClient = (function () {
//def sendTyping
InternalClient.prototype.sendTyping = function sendTyping(channel) {
var _this27 = this;
var _this29 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
return _this27.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true);
return _this29.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true);
});
};
//def startTyping
InternalClient.prototype.startTyping = function startTyping(channel) {
var _this28 = this;
var _this30 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
if (_this28.intervals.typing[channel.id]) {
if (_this30.intervals.typing[channel.id]) {
// typing interval already exists, leave it alone
throw new Error("Already typing in that channel");
}
_this28.intervals.typing[channel.id] = setInterval(function () {
return _this28.sendTyping(channel)["catch"](function (error) {
return _this28.emit("error", error);
_this30.intervals.typing[channel.id] = setInterval(function () {
return _this30.sendTyping(channel)["catch"](function (error) {
return _this30.emit("error", error);
});
}, 4000);
return _this28.sendTyping(channel);
return _this30.sendTyping(channel);
});
};
//def stopTyping
InternalClient.prototype.stopTyping = function stopTyping(channel) {
var _this29 = this;
var _this31 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
if (!_this29.intervals.typing[channel.id]) {
if (!_this31.intervals.typing[channel.id]) {
// typing interval doesn"t exist
throw new Error("Not typing in that channel");
}
clearInterval(_this29.intervals.typing[channel.id]);
_this29.intervals.typing[channel.id] = false;
clearInterval(_this31.intervals.typing[channel.id]);
_this31.intervals.typing[channel.id] = false;
});
};
@@ -1146,12 +1167,12 @@ var InternalClient = (function () {
//def setChannelTopic
InternalClient.prototype.setChannelTopic = function setChannelTopic(chann) {
var _this30 = this;
var _this32 = this;
var topic = arguments.length <= 1 || arguments[1] === undefined ? "" : arguments[1];
return this.resolver.resolveChannel(chann).then(function (channel) {
return _this30.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
return _this32.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
name: channel.name,
position: channel.position,
topic: topic
@@ -1164,12 +1185,12 @@ var InternalClient = (function () {
//def setChannelName
InternalClient.prototype.setChannelName = function setChannelName(chann) {
var _this31 = this;
var _this33 = this;
var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1];
return this.resolver.resolveChannel(chann).then(function (channel) {
return _this31.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
return _this33.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
name: name,
position: channel.position,
topic: channel.topic
@@ -1182,13 +1203,13 @@ var InternalClient = (function () {
//def setChannelNameAndTopic
InternalClient.prototype.setChannelNameAndTopic = function setChannelNameAndTopic(chann) {
var _this32 = this;
var _this34 = this;
var name = arguments.length <= 1 || arguments[1] === undefined ? "discordjs_is_the_best" : arguments[1];
var topic = arguments.length <= 2 || arguments[2] === undefined ? "" : arguments[2];
return this.resolver.resolveChannel(chann).then(function (channel) {
return _this32.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
return _this34.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
name: name,
position: channel.position,
topic: topic
@@ -1202,12 +1223,12 @@ var InternalClient = (function () {
//def setTopic
InternalClient.prototype.setChannelPosition = function setChannelPosition(chann) {
var _this33 = this;
var _this35 = this;
var position = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
return this.resolver.resolveChannel(chann).then(function (channel) {
return _this33.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
return _this35.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, {
name: channel.name,
position: position,
topic: channel.topic

View File

@@ -35,6 +35,10 @@ var _StructuresPMChannel = require("../../Structures/PMChannel");
var _StructuresPMChannel2 = _interopRequireDefault(_StructuresPMChannel);
var _StructuresRole = require("../../Structures/Role");
var _StructuresRole2 = _interopRequireDefault(_StructuresRole);
var _StructuresServer = require("../../Structures/Server");
var _StructuresServer2 = _interopRequireDefault(_StructuresServer);
@@ -94,6 +98,36 @@ var Resolver = (function () {
return null;
};
Resolver.prototype.resolveRole = function resolveRole(resource) {
if (resource instanceof _StructuresRole2["default"]) {
return resource;
}
if (resource instanceof String || typeof resource === "string") {
var role = null;
for (var _iterator = this.internal.servers, _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 server = _ref;
if (role = server.roles.find(function (r) {
return r.id == resource;
})) {
return role;
}
}
}
return null;
};
Resolver.prototype.resolveFile = function resolveFile(resource) {
if (typeof resource === "string" || resource instanceof String) {
return _fs2["default"].createReadStream(resource);
@@ -105,19 +139,19 @@ var Resolver = (function () {
// resource is a string
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;
for (var _iterator2 = resource.match(/<@[^>]*>/g) || [], _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref2 = _iterator2[_i2++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
_i2 = _iterator2.next();
if (_i2.done) break;
_ref2 = _i2.value;
}
var mention = _ref;
var mention = _ref2;
var userID = mention.substring(2, mention.length - 1);
_mentions.push(userID);
@@ -209,19 +243,19 @@ var Resolver = (function () {
if (resource instanceof _StructuresUser2["default"]) {
// see if a PM exists
var chatFound = false;
for (var _iterator2 = this.internal.private_channels, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
for (var _iterator3 = this.internal.private_channels, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
var _ref3;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref2 = _iterator2[_i2++];
if (_isArray3) {
if (_i3 >= _iterator3.length) break;
_ref3 = _iterator3[_i3++];
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref2 = _i2.value;
_i3 = _iterator3.next();
if (_i3.done) break;
_ref3 = _i3.value;
}
var pmchat = _ref2;
var pmchat = _ref3;
if (pmchat.recipient.equals(resource)) {
chatFound = pmchat;

View File

@@ -621,6 +621,7 @@ export default class InternalClient {
// def updateRole
updateRole(role, data) {
role = this.resolver.resolveRole(role);
var server = this.resolver.resolveServer(role.server);
var newData = {
@@ -661,11 +662,14 @@ export default class InternalClient {
}
if (!Array.isArray(roles) || roles.length === 0) {
if (roles instanceof Role) {
roles = this.resolve.resolveRole(roles);
if (roles) {
roles = [roles];
} else {
return Promise.reject(new Error("invalid array of roles"));
}
} else {
roles = roles.map(r => this.resolver.resolveRole(r));
}
if (roles.some(role => !role.server.memberMap[member.id])) {
@@ -691,8 +695,12 @@ export default class InternalClient {
}
memberHasRole(member, role) {
role = this.resolver.resolveRole(role);
member = this.resolver.resolveUser(member);
if (!role) {
throw new Error("invalid role");
}
if (!member) {
throw new Error("user not found");
}
@@ -709,11 +717,14 @@ export default class InternalClient {
}
if (!Array.isArray(roles) || roles.length === 0) {
if (roles instanceof Role) {
roles = this.resolve.resolveRole(roles);
if (roles) {
roles = [roles];
} else {
return Promise.reject(new Error("invalid array of roles"));
}
} else {
roles = roles.map(r => this.resolver.resolveRole(r));
}
var roleIDS = roles[0].server.memberMap[member.id].roles.map(r => r.id);
@@ -829,6 +840,8 @@ export default class InternalClient {
var user;
if (role instanceof User) {
user = role;
} else {
role = this.resolver.resolveRole(role);
}
var data = {};

View File

@@ -9,6 +9,7 @@ import TextChannel from "../../Structures/TextChannel";
import VoiceChannel from "../../Structures/VoiceChannel";
import ServerChannel from "../../Structures/ServerChannel";
import PMChannel from "../../Structures/PMChannel";
import Role from "../../Structures/Role";
import Server from "../../Structures/Server";
import Message from "../../Structures/Message";
import Invite from "../../Structures/Invite";
@@ -58,6 +59,21 @@ export default class Resolver {
return null;
}
resolveRole(resource) {
if (resource instanceof Role) {
return resource;
}
if (resource instanceof String || typeof resource === "string") {
var role = null;
for (var server of this.internal.servers) {
if (role = server.roles.find(r => r.id == resource)) {
return role;
}
}
}
return null;
}
resolveFile(resource) {
if (typeof resource === "string" || resource instanceof String) {
return fs.createReadStream(resource);