Added permission functions for client in docs

This commit is contained in:
hydrabolt
2015-10-27 21:03:48 +00:00
parent a18823c47f
commit d21372e0f6
4 changed files with 339 additions and 192 deletions

View File

@@ -55,9 +55,9 @@ author = u'hydrabolt'
# built documents. # built documents.
# #
# The short X.Y version. # The short X.Y version.
version = '3.6' version = '3.10'
# The full version, including alpha/beta/rc tags. # The full version, including alpha/beta/rc tags.
release = '3.6.1' release = '3.10.1'
# The language for content autogenerated by Sphinx. Refer to documentation # The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages. # for a list of supported languages.

View File

@@ -359,6 +359,122 @@ Sets the topic of the specified channel
- **error** - An error if one occurred, otherwise it is null. - **error** - An error if one occurred, otherwise it is null.
createRole(server, data, `callback`)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Creates a role (or set of permissions) in a given server.
- **server** - A `Server Resolvable`_ where the role should be created.
- **data** - An `Object` containing values available in any `ServerPermissions`_ object.
- **callback** - A `function` that can take the following parameters:
- **error** - An error if one occurred, otherwise it is null.
- **role** - If no errors occurred, this will be a `ServerPermissions`_ object representing the created role.
.. code-block:: js
// example usage:
bot.createRole(server, {
name : "My Role",
color : Discord.Colors.RED,
sendMessages : false
});
createRoleIfNotExists(server, data, `callback`)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Identical to createRole_ but it only executes if a role with the same name, permissions and color exists in the given server.
- **server** - A `Server Resolvable`_ where the role should be created.
- **data** - An `Object` containing values available in any `ServerPermissions`_ object.
- **callback** - A `function` that can take the following parameters:
- **error** - An error if one occurred, otherwise it is null.
- **role** - If no errors occurred, this will be a `ServerPermissions`_ object representing the created role.
.. code-block:: js
// example usage:
bot.createRoleIfNotExists(server, {
name : "My Role",
color : Discord.Colors.RED,
sendMessages : false
}) // would execute, role doesn't exist in the server yet
.then(function(){
bot.createRoleIfNotExists(server, {
name : "My Role",
color : Discord.Colors.RED,
sendMessages : false
}); // would not execute, role already exists
});
.. warning :: Due to the asynchronous nature of the code, if you ran the same code twice without putting one in a callback, both would execute as at the point of execution, neither are existant.
deleteRole(role, `callback`)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Deletes a Role
- **role** - The `ServerPermissions` that you want to delete.
- **callback** - A `function` that can take the following parameters:
- **error** - An error if one occurred, otherwise it is null.
updateRole(role, data, `callback`)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Updates a role existing in a server to have new permissions, order, color, name or to have `hoist` (whether it should be its own category in the users list)
- **role** - A `ServerPermissions`_ which represents the role to edit.
- **data** - An `Object` containing values available in any `ServerPermissions`_ object.
- **callback** - A `function` that can take the following parameters:
- **error** - An error if one occurred, otherwise it is null.
- **role** - If no errors occurred, this will be a `ServerPermissions`_ object representing the edited role.
.. code-block:: js
bot.updateRole(alreadyExistantRole, {
name : "this is a new role name"
});
addMemberToRole(member, role, `callback`)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Adds a Member to a Server Role.
- **member** - The `Member`_ you would like to add to the role.
- **role** - The `ServerPermissions`_ (or role) you would like to add the member to.
- **callback** - A `function` that can take the following parameters:
- **error** - An error if one occurred, otherwise it is null.
removeMemberFromRole(member, role, `callback`)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Removes a Member from a Server Role.
- **member** - The `Member`_ you would like to remove from the role.
- **role** - The `ServerPermissions`_ (or role) you would like to remove the member from.
- **callback** - A `function` that can take the following parameters:
- **error** - An error if one occurred, otherwise it is null.
overwritePermissions(channel, role, overwrites, `callback`)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Overrides/overwrites a role in a specific channel.
- **channel** - A `Channel Resolvable`_ where the permissions should be overridden.
- **role** - A `ServerPermissions`_ role that you want to override
- **overwrites** - An `Object` containing any values in a `ChannelPermissions`_
- **callback** - A `function` that can take the following parameters:
- **error** - An error if one occurred, otherwise it is null.
getUser(key, value) getUser(key, value)
~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
@@ -620,3 +736,5 @@ Called when a WebSocket message is received and it gives you the message.
.. _Discord Game ID : https://raw.githubusercontent.com/hydrabolt/discord.js/master/ref/gameMap.json .. _Discord Game ID : https://raw.githubusercontent.com/hydrabolt/discord.js/master/ref/gameMap.json
.. _EventEmitter : https://nodejs.org/api/events.html#events_class_events_eventemitter .. _EventEmitter : https://nodejs.org/api/events.html#events_class_events_eventemitter
.. _createRole : #createRole

View File

@@ -818,20 +818,25 @@ var Client = (function (_EventEmitter) {
}); });
}; };
Client.prototype.updateRole = function updateRole(server, role) { Client.prototype.updateRole = function updateRole(role, data) {
var cb = arguments.length <= 2 || arguments[2] === undefined ? function (err, perm) {} : arguments[2]; var cb = arguments.length <= 2 || arguments[2] === undefined ? function (err, perm) {} : arguments[2];
var self = this; var self = this;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
server = self.resolveServerID(server); var server = role.server.id;
var modRole = role;
for (var key in data) {
modRole[key] = data[key];
}
request.patch(Endpoints.SERVERS + "/" + server + "/roles/" + role.id).set("authorization", self.token).send({ request.patch(Endpoints.SERVERS + "/" + server + "/roles/" + role.id).set("authorization", self.token).send({
color: role.color, color: modRole.color,
hoist: role.hoist, hoist: modRole.hoist,
name: role.name, name: modRole.name,
permissions: role.packed permissions: modRole.packed
}).end(function (err, res) { }).end(function (err, res) {
if (err) { if (err) {
cb(err); cb(err);
@@ -944,6 +949,15 @@ var Client = (function (_EventEmitter) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
function dieerror(e) {
reject(e);
callback(e);
}
self.resolveDestination(channel).then(next)["catch"](dieerror);
function next(channelID) {
var data; var data;
if (role instanceof ServerPermissions || role.type === "role") { if (role instanceof ServerPermissions || role.type === "role") {
@@ -956,7 +970,7 @@ var Client = (function (_EventEmitter) {
data.id = role.id; data.id = role.id;
data.type = "member"; data.type = "member";
} }
request.put(Endpoints.CHANNELS + "/" + channel.id + "/permissions/" + role.id).set("authorization", self.token).send(data).end(function (err) { request.put(Endpoints.CHANNELS + "/" + channelID + "/permissions/" + role.id).set("authorization", self.token).send(data).end(function (err) {
if (err) { if (err) {
reject(err); reject(err);
callback(err); callback(err);
@@ -965,6 +979,7 @@ var Client = (function (_EventEmitter) {
callback(); callback();
} }
}); });
}
}); });
function ad(data) { function ad(data) {

View File

@@ -867,22 +867,27 @@ class Client extends EventEmitter{
} }
updateRole(server, role, cb = function (err, perm) { }) { updateRole(role, data, cb = function (err, perm) { }) {
var self = this; var self = this;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
server = self.resolveServerID(server); var server = role.server.id;
var modRole = role;
for (var key in data) {
modRole[key] = data[key];
}
request request
.patch(`${Endpoints.SERVERS}/${server}/roles/${role.id}`) .patch(`${Endpoints.SERVERS}/${server}/roles/${role.id}`)
.set("authorization", self.token) .set("authorization", self.token)
.send({ .send({
color: role.color, color: modRole.color,
hoist: role.hoist, hoist: modRole.hoist,
name: role.name, name: modRole.name,
permissions: role.packed permissions: modRole.packed
}) })
.end(function (err, res) { .end(function (err, res) {
if (err) { if (err) {
@@ -1008,6 +1013,15 @@ class Client extends EventEmitter{
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
function dieerror(e) {
reject(e);
callback(e);
}
self.resolveDestination(channel).then(next).catch(dieerror);
function next(channelID) {
var data; var data;
if (role instanceof ServerPermissions || role.type === "role") { if (role instanceof ServerPermissions || role.type === "role") {
@@ -1022,7 +1036,7 @@ class Client extends EventEmitter{
} }
request request
.put(`${Endpoints.CHANNELS}/${channel.id}/permissions/${role.id}`) .put(`${Endpoints.CHANNELS}/${channelID}/permissions/${role.id}`)
.set("authorization", self.token) .set("authorization", self.token)
.send(data) .send(data)
.end(function (err) { .end(function (err) {
@@ -1034,7 +1048,7 @@ class Client extends EventEmitter{
callback(); callback();
} }
}); });
}
}); });
function ad(data) { function ad(data) {