diff --git a/docs/docs_client.rst b/docs/docs_client.rst index f24c8ba35..6b732ed6d 100644 --- a/docs/docs_client.rst +++ b/docs/docs_client.rst @@ -1,652 +1,662 @@ -.. include:: ./vars.rst - -Client -====== - -**extends** EventEmitter_ - -This page contains documentation on the `Discord.Client` class. This should be used when you want to start creating things with the API. - --------- - -Attributes ----------- - -users -~~~~~ - -A Cache_ of User_ objects that the client has cached. - -channels -~~~~~~~~ - -A Cache_ of ServerChannel_ objects that the client has cached. - -privateChannels -~~~~~~~~~~~~~~~ - -A Cache_ of PMChannel_ objects that the client has cached. These are all the Private/Direct Chats the client is in. - -servers -~~~~~~~ - -A Cache_ of Server_ objects that the client has cached. - -voiceConnection -~~~~~~~~~~~~~~~ - -A VoiceConnection_ object that is the current voice connection (if any). - -readyTime -~~~~~~~~~ - -A `Number` unix timestamp dating to when the Client emitted `ready`. - -uptime -~~~~~~ - -A `Number` in milliseconds representing how long the Client has been ready for. - -user -~~~~ - -A User_ object representing the logged in client's user. - ------ - -Functions ---------- - -.. note :: Any functions used here that take callbacks as an optional parameter can also be used as Promises_. Promises take the exact same parameters for each use case, except errors are moved to catch statements instead of then. For example, you can do: - - .. code-block:: js - - bot.login(email, password).then(success).catch(err); - - function success(token){ - // handle success - } - - function err(error){ - // handle error - } - - or use callbacks: - - .. code-block:: js - - bot.login(email, password, function(error, token){ - // handle error and success - }); - -login(email, password, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Logs the client in so it can begin initialising. Use this `after` registering your events to ensure they are called! - -- **email** - The e-mail used to sign in, `String`. -- **password** - The password used to sign in, `String`. -- **callback** - `function` that takes the following parameters: - - **error** - An error if any occurred - - **token** - The token received after logging in, `String`. - -logout(`callback`) -~~~~~~~~~~~~~~~~~~ - -Logs the client out and closes the WebSocket connections. - -- **callback** - `function` that takes the following parameter: - - **error** - An error if any occurred - -sendMessage(channel, content, `options`, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Sends a message to the specified channel. - -- **channel** - a `Channel Resolvable`_ -- **content** - a `String Resolvable`_ - the message you want to send -- **options** - `object` containing: - - **tts** - `Boolean`, should message be text-to-speech -- **callback** - `function` that takes the following parameters: - - **error** - error object if any occurred - - **message** - the sent Message_ - -sendFile(channel, attachment, name, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Sends a file to the specified channel. - -- **channel** - a `Channel Resolvable`_ -- **attachment** - A ReadableStream, String or Buffer -- **name** - `String`, name of the file containing the extension -- **callback** - `function` taking the following: - - **error** - error if any occurred - - **message** - the sent Message_ - -reply(message, content, `options`, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Shortcut to `sendMessage` but prepends a mention to the sender of the original message to the start of your message. - -- **message** - The Message_ to reply to -- **content** - a `String Resolvable`_ - the message you want to send -- **options** - `object` containing: - - **tts** - `Boolean`, should message be text-to-speech -- **callback** - `function` that takes the following parameters: - - **error** - error object if any occurred - - **message** - the sent Message_ - -updateMessage(message, content, `options`, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Updates the content of a previously sent message - -- **message** - The Message_ to update -- **content** - a `String Resolvable`_ - the content you want to update the message with -- **options** - `object` containing: - - **tts** - `Boolean`, should message be text-to-speech -- **callback** - `function` that takes the following parameters: - - **error** - error object if any occurred - - **message** - the sent Message_ - -deleteMessage(message, `options`, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Attempts to delete a message - -- **message** - The Message_ to delete -- **options** - `object` containing the following: - - **wait** - Milliseconds as a `number` to wait before deleting the message -- **callback** - - **error** - error object if any occurred - -getChannelLogs(channel, `limit`, `options`, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Gets a list of previously sent messages in a channel. - -- **channel** - The Channel_ to get messages from -- **limit** - The maximum amount of messages to retrieve - defaults to 500. A `Number` -- **options** - An `object` containing either of the following: - - **before** - A `Message Resolvable`_ - gets messages before this message. - - **after** - A `Message Resolvable`_ - gets messages after this message. -- **callback** - `function` taking the following: - - **error** - error if any occurred - - **messages** - `array` of Message_ objects sent in channel - -getBans(server, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Gets a list of banned users in a server. - -- **server** - `Server Resolvable`_ - The server to get banned users of -- **callback** - `function` taking the following: - - **error** - error if any occurred - - **users** - `array` of banned users in the server - -joinServer(invite, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Joins a server from the given invite - -- **invite** - an `Invite Resolvable`_ -- **callback** - `function` taking the following: - - **error** - error if any occurred - - **server** - the joined Server_ - -createServer(name, region, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Creates a server - -- **name** - `String`, name of the server -- **region** - `String`, region of the server, currently **us-west, us-east, singapore, london, sydney** or **amsterdam** -- **callback** - `function` taking the following: - - **error** - error if any occurred - - **server** - the created Server_ - -leaveServer(server, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Leaves/deletes a server that the client is in - -- **server** - a `Server Resolvable`_ -- **callback** - `function` taking the following: - - **error** - error if any occurred - -createChannel(server, name, `type`, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Creates a channel in a server - -- **server** - a `Server Resolvable`_ -- **name** - `String`, name of the channel. Spaces not allowed. -- **type** - defaults to `text`, but can also be `voice` -- **callback** - `function` taking the following: - - **error** - error if any occurred - - **channel** - the created ServerChannel_ - -deleteChannel(channel, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Deletes a channel in a server. - -- **channel** - a `Channel Resolvable`_ to delete -- **callback** - `function` taking the following: - - **error** - error if any occurred. - -banMember(user, server, `length`, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Bans a user from a server. - -- **user** - A `User Resolvable`_ to ban -- **server** - A `Server Resolvable`_ to ban the user from -- **length** - `Number`, how many days to go back and delete messages from that user -- **callback** - `function` taking the following: - - **error** - error if any occurred. - -unbanMember(user, server, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Unbans a user from a server. - -- **user** - A `User Resolvable`_ to unban -- **server** - A `Server Resolvable`_ to unban the user from -- **callback** - `function` taking the following: - - **error** - error if any occurred. - -kickMember(user, server, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Removes a user from a server - -- **user** - A `User Resolvable`_ to kick -- **server** - A `Server Resolvable`_ to kick the user from -- **callback** - `function` taking the following: - - **error** - error if any occurred. - -createInvite(channel, `options`, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Creates an invite for the specified channel (or server) - -- **channel** - A `Channel Resolvable`_ -- **options** - `object` containing: - - **maxAge** - `Number` for maximum time in seconds for invite's validity - - **maxUses** - `Number`, maximum uses of invite - - **temporary** - `Boolean`, whether the invite should be temporary - - **xkcd** - `Boolean`, whether the invite should be human-readable-friendly. -- **callback** - `function` taking the following: - - **error** - error if any occurred - - **invite** - the created Invite_ - -deleteInvite(invite, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Deletes an invite - -- **invite** - An `Invite ID Resolvable`_ -- **callback** - a `function` taking the following: - - **error** - error if any occurred - -setStatus(status, `game`, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Sets the Discord Status of the Client - -- **status** - `String`, either ``online, here, active, available`` or ``idle, away`` -- **game** - `String/Number`, ID of Discord Game being played -- **callback** - `function` taking the following: - - **error** - error if any occurred - -setStatusIdle() -~~~~~~~~~~~~~~~ - -**Alias:** `setStatusAway` - -Sets the status of the Client to Idle/Away - -setStatusOnline() -~~~~~~~~~~~~~~~ - -**Aliases:** `setStatusHere`, `setStatusActive`, `setStatusAvailable` - -Sets the status of the Client to Online - -setTopic(channel, topic, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Sets the topic of a channel - -- **channel** - A `Channel Resolvable`_ -- **topic** - A `String` -- **callback** - `function` taking the following: - - **error** - error if any occurred - -setChannelName(channel, name, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Sets the name of a channel - -- **channel** - A `Channel Resolvable`_ -- **name** - A `String` -- **callback** - `function` taking the following: - - **error** - error if any occurred - -setChannelNameAndTopic(channel, name, topic, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Sets the name and topic of a channel - -- **channel** - A `Channel Resolvable`_ -- **name** - A `String` -- **topic** - A `String` -- **callback** - `function` taking the following: - - **error** - error if any occurred - -startTyping(channel, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Marks the client as typing in a channel. - -- **channel** - A `Channel Resolvable`_ -- **callback** - `function` taking the following: - - **error** - error if any occurred - -stopTyping(channel, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Marks the client as not typing in a channel (takes a few seconds to go active). - -- **channel** - A `Channel Resolvable`_ -- **callback** - `function` taking the following: - - **error** - error if any occurred - -updateDetails(details, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Updates the details of the client - -- **details** - `object` containing any of the following: - - **avatar** - `Base64 Resolvable`_, new avatar of the client - - **email** - `String`, new email of the client - - **newPassword** - `String`, new password of the client - - **username** - `String`, new username of the client -- **callback** - `function` taking the following: - - **error** - error if any occurred - -setAvatar(avatar, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Sets the avatar of the client - -- **avatar** - `Base64 Resolvable`_, new avatar of the client -- **callback** - `function` taking the following: - - **error** - error if any occurred - -setUsername(name, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Sets the username of the client - -- **username** - `String`, new username of the Client -- **callback** - `function` taking the following: - - **error** - error if any occurred - -joinVoiceChannel(channel, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Joins a Voice Channel to begin transmitting audio - -- **channel** - A `VoiceChannel Resolvable`_ -- **callback** - `function` that takes the following: - - **error** - error if any occurred - - **connection** - VoiceConnection_, the created Voice Connection. - -leaveVoiceChannel(`callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Leaves the currently connected Voice Channel if connected - -- **callback** - `function` that takes the following: - - **error** - error if any occurred - -createRole(server, `data`, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Creates a new role in a server. - -- **server** - a `Server Resolvable`_ -- **data** - `object` containing the structure below -- **callback** - `function` that takes the following: - - **error** - error if any occurred - - **role** - the created Role_ - -.. code-block:: js - - // structure of data parameter (all attrs optional): - { - color : 0xFF0000, - hoist : false, - name : "A New Role!", - permissions : [ - // see the constants documentation for full permissions - "attachFiles", "sendMessages" - ] - } - -updateRole(role, data, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Updates a role in a server. - -- **role** - a Role_ -- **data** - an `object` taking the structure shown below -- **callback** - a `function` taking the following: - - **error** - error if any occurred - - **role** - the updated Role_ - -.. code-block:: js - - // structure of data parameter (all attrs optional): - { - color : 0xFF0000, - hoist : false, - name : "A New Role!", - permissions : [ - // see the constants documentation for full permissions - "attachFiles", "sendMessages" - ] - } - -deleteRole(role, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Deletes a role from a server - -- **role** - The Role_ to delete -- **callback** - `function` that takes the following: - - **error** - error if any occurred - -addMemberToRole(member, role, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -**Aliases** : `addUserToRole` - -Adds a member of a server to a role in the server - -- **member** - A `User Resolvable`_ -- **role** - A Role_ -- **callback** - `function` that takes the following: - - **error** - error if any occurred - -removeMemberFromRole(member, role, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -**Aliases** : `removeUserFromRole` - -Removes a member of a server from a role in the server - -- **member** - A `User Resolvable`_ -- **role** - A Role_ -- **callback** - `function` that takes the following: - - **error** - error if any occurred - -overwritePermissions(channel, roleOrUser, options, `callback`) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Overwrites the permissions of a role or a user in a channel - -- **channel** - a `Channel Resolvable`_ -- **roleOrUser** - a Role_ or a User_ object -- **options** - an `object` containing a structure as shown below -- **callback** - `function` that takes the following: - - **error** - error if any occurred - -.. code-block:: js - - { - "sendMessages" : false, - "attachFiles" : true - } - -Events ------- - -`Discord.Client` is an EventEmitter, so you can use `.on()` and `.off()` to add and remove events. - -ready -~~~~~ - -Emitted when the client is ready to use - -debug -~~~~~ - -Emitted when the client debugs or wants to log something internally - -message -~~~~~~~ - -Emitted when the client receives a message, supplies a Message_ object. - -warn -~~~~ - -Emitted when the client has encountered a small error that can be avoided. - -messageDeleted -~~~~~~~~~~~~~~ - -Emitted when a message has been deleted and the Client finds out, supplies a Message_ object IF available, and a Channel_ object. - -messageUpdated -~~~~~~~~~~~~~~ - -Emitted when a message has been updated and the client finds out. Supplies two Message_ objects, the first being the new updated messages, the latter being the old message. - -disconnected -~~~~~~~~~~~~ - -Emitted when the client is disconnected from the Discord server. - -error -~~~~~ - -Emitted when the client runs into a big problem, supplies an error object. - -raw -~~~ - -Emitted when a message over WebSocket is received, it supplies one `object` containing the raw data from the WebSocket. - -serverCreated -~~~~~~~~~~~~~ - -Emitted when a server is joined by the Client, supplies a Server_ object. - -serverDeleted -~~~~~~~~~~~~~ - -Emitted when the client leaves a server, supplies a Server_ object. - -serverUpdated -~~~~~~~~~~~~~ - -Emitted when a server is updated (e.g. name change). Supplies two Server_ objects, the first being the server before the update, the second being the new, updated server. - -channelCreated -~~~~~~~~~~~~~~ - -Emitted when a channel is created, supplies a Channel_ object (includes PM chats as well as server channels). - -channelDeleted -~~~~~~~~~~~~~~ - -Emitted when a channel is deleted, supplies a Channel_ object. - -channelUpdated -~~~~~~~~~~~~~~ - -Emitted when a channel is updated (e.g. name/topic change). Supplies a Channel_ object. - -serverRoleCreated -~~~~~~~~~~~~~~~~~ - -Emitted when a role is created in a server, supplies a Role_ object. - -serverRoleDeleted -~~~~~~~~~~~~~~~~~ - -Emitted when a role is deleted from a server, supplies a Role_ object. - -serverRoleUpdated -~~~~~~~~~~~~~~~~~ - -Emitted when a role is updated in a server, supplies two Role_ objects. The first is the old role, the second is the updated role. - -serverNewMember -~~~~~~~~~~~~~~~ - -Emitted when a user joins a server, supplies a Server_ object and a User_ object. - -serverMemberRemoved -~~~~~~~~~~~~~~~~~~~ - -Emitted when a member is removed from a server. Supplies a Server_ object and a User_ object. - -serverMemberUpdated -~~~~~~~~~~~~~~~~~~~ - -Emitted when a member in a server is updated. Supplies a Server_ object and a User_ object. - -presence -~~~~~~~~ - -Emitted when a user goes online/offline/idle or starts/stops playing a game. Supplies 3 parameters, the first being the User_ object, the second being the status, the third being the game ID they are playing. - -userUpdated -~~~~~~~~~~~ - -Emitted when a user changes their name, avatar or similar. Supplies two User_ objects, the first being the user before being updated, the second being the updated user. - -userTypingStarted -~~~~~~~~~~~~~~~ - -Emitted when a user starts typing in a channel. Supplies two parameters, a User_ object and a Channel_ object. - -userTypingStopped -~~~~~~~~~~~~~~ - -Emitted when a user stop typing in a channel. Supplies two parameters, a User_ object and a Channel_ object. - -userBanned -~~~~~~~~~~ - -Emitted when a user is banned from a server. Supplies two parameters, a User_ object and a Server_ object. - -userUnbanned -~~~~~~~~~~ - -Emitted when a user is unbanned from a server. Supplies two parameters, a User_ object and a Server_ object. \ No newline at end of file +.. include:: ./vars.rst + +Client +====== + +**extends** EventEmitter_ + +This page contains documentation on the `Discord.Client` class. This should be used when you want to start creating things with the API. + +-------- + +Attributes +---------- + +users +~~~~~ + +A Cache_ of User_ objects that the client has cached. + +channels +~~~~~~~~ + +A Cache_ of ServerChannel_ objects that the client has cached. + +privateChannels +~~~~~~~~~~~~~~~ + +A Cache_ of PMChannel_ objects that the client has cached. These are all the Private/Direct Chats the client is in. + +servers +~~~~~~~ + +A Cache_ of Server_ objects that the client has cached. + +voiceConnection +~~~~~~~~~~~~~~~ + +A VoiceConnection_ object that is the current voice connection (if any). + +readyTime +~~~~~~~~~ + +A `Number` unix timestamp dating to when the Client emitted `ready`. + +uptime +~~~~~~ + +A `Number` in milliseconds representing how long the Client has been ready for. + +user +~~~~ + +A User_ object representing the logged in client's user. + +userAgent +~~~~~~~~~ + +An object containing `url`, `version` and `full`. +Setting this property allows the discord developers to keep track of active bots, +it defaults to the discord.js git repo and the current version of the package. +`url` should be the repository/homepage of the creator. +`version` should be the version of your bot. +`full` is read only and will be automatically generated upon setting. + +----- + +Functions +--------- + +.. note :: Any functions used here that take callbacks as an optional parameter can also be used as Promises_. Promises take the exact same parameters for each use case, except errors are moved to catch statements instead of then. For example, you can do: + + .. code-block:: js + + bot.login(email, password).then(success).catch(err); + + function success(token){ + // handle success + } + + function err(error){ + // handle error + } + + or use callbacks: + + .. code-block:: js + + bot.login(email, password, function(error, token){ + // handle error and success + }); + +login(email, password, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Logs the client in so it can begin initialising. Use this `after` registering your events to ensure they are called! + +- **email** - The e-mail used to sign in, `String`. +- **password** - The password used to sign in, `String`. +- **callback** - `function` that takes the following parameters: + - **error** - An error if any occurred + - **token** - The token received after logging in, `String`. + +logout(`callback`) +~~~~~~~~~~~~~~~~~~ + +Logs the client out and closes the WebSocket connections. + +- **callback** - `function` that takes the following parameter: + - **error** - An error if any occurred + +sendMessage(channel, content, `options`, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Sends a message to the specified channel. + +- **channel** - a `Channel Resolvable`_ +- **content** - a `String Resolvable`_ - the message you want to send +- **options** - `object` containing: + - **tts** - `Boolean`, should message be text-to-speech +- **callback** - `function` that takes the following parameters: + - **error** - error object if any occurred + - **message** - the sent Message_ + +sendFile(channel, attachment, name, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Sends a file to the specified channel. + +- **channel** - a `Channel Resolvable`_ +- **attachment** - A ReadableStream, String or Buffer +- **name** - `String`, name of the file containing the extension +- **callback** - `function` taking the following: + - **error** - error if any occurred + - **message** - the sent Message_ + +reply(message, content, `options`, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Shortcut to `sendMessage` but prepends a mention to the sender of the original message to the start of your message. + +- **message** - The Message_ to reply to +- **content** - a `String Resolvable`_ - the message you want to send +- **options** - `object` containing: + - **tts** - `Boolean`, should message be text-to-speech +- **callback** - `function` that takes the following parameters: + - **error** - error object if any occurred + - **message** - the sent Message_ + +updateMessage(message, content, `options`, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Updates the content of a previously sent message + +- **message** - The Message_ to update +- **content** - a `String Resolvable`_ - the content you want to update the message with +- **options** - `object` containing: + - **tts** - `Boolean`, should message be text-to-speech +- **callback** - `function` that takes the following parameters: + - **error** - error object if any occurred + - **message** - the sent Message_ + +deleteMessage(message, `options`, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Attempts to delete a message + +- **message** - The Message_ to delete +- **options** - `object` containing the following: + - **wait** - Milliseconds as a `number` to wait before deleting the message +- **callback** + - **error** - error object if any occurred + +getChannelLogs(channel, `limit`, `options`, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Gets a list of previously sent messages in a channel. + +- **channel** - The Channel_ to get messages from +- **limit** - The maximum amount of messages to retrieve - defaults to 500. A `Number` +- **options** - An `object` containing either of the following: + - **before** - A `Message Resolvable`_ - gets messages before this message. + - **after** - A `Message Resolvable`_ - gets messages after this message. +- **callback** - `function` taking the following: + - **error** - error if any occurred + - **messages** - `array` of Message_ objects sent in channel + +getBans(server, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Gets a list of banned users in a server. + +- **server** - `Server Resolvable`_ - The server to get banned users of +- **callback** - `function` taking the following: + - **error** - error if any occurred + - **users** - `array` of banned users in the server + +joinServer(invite, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Joins a server from the given invite + +- **invite** - an `Invite Resolvable`_ +- **callback** - `function` taking the following: + - **error** - error if any occurred + - **server** - the joined Server_ + +createServer(name, region, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Creates a server + +- **name** - `String`, name of the server +- **region** - `String`, region of the server, currently **us-west, us-east, singapore, london, sydney** or **amsterdam** +- **callback** - `function` taking the following: + - **error** - error if any occurred + - **server** - the created Server_ + +leaveServer(server, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Leaves/deletes a server that the client is in + +- **server** - a `Server Resolvable`_ +- **callback** - `function` taking the following: + - **error** - error if any occurred + +createChannel(server, name, `type`, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Creates a channel in a server + +- **server** - a `Server Resolvable`_ +- **name** - `String`, name of the channel. Spaces not allowed. +- **type** - defaults to `text`, but can also be `voice` +- **callback** - `function` taking the following: + - **error** - error if any occurred + - **channel** - the created ServerChannel_ + +deleteChannel(channel, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Deletes a channel in a server. + +- **channel** - a `Channel Resolvable`_ to delete +- **callback** - `function` taking the following: + - **error** - error if any occurred. + +banMember(user, server, `length`, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Bans a user from a server. + +- **user** - A `User Resolvable`_ to ban +- **server** - A `Server Resolvable`_ to ban the user from +- **length** - `Number`, how many days to go back and delete messages from that user +- **callback** - `function` taking the following: + - **error** - error if any occurred. + +unbanMember(user, server, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Unbans a user from a server. + +- **user** - A `User Resolvable`_ to unban +- **server** - A `Server Resolvable`_ to unban the user from +- **callback** - `function` taking the following: + - **error** - error if any occurred. + +kickMember(user, server, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Removes a user from a server + +- **user** - A `User Resolvable`_ to kick +- **server** - A `Server Resolvable`_ to kick the user from +- **callback** - `function` taking the following: + - **error** - error if any occurred. + +createInvite(channel, `options`, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Creates an invite for the specified channel (or server) + +- **channel** - A `Channel Resolvable`_ +- **options** - `object` containing: + - **maxAge** - `Number` for maximum time in seconds for invite's validity + - **maxUses** - `Number`, maximum uses of invite + - **temporary** - `Boolean`, whether the invite should be temporary + - **xkcd** - `Boolean`, whether the invite should be human-readable-friendly. +- **callback** - `function` taking the following: + - **error** - error if any occurred + - **invite** - the created Invite_ + +deleteInvite(invite, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Deletes an invite + +- **invite** - An `Invite ID Resolvable`_ +- **callback** - a `function` taking the following: + - **error** - error if any occurred + +setStatus(status, `game`, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Sets the Discord Status of the Client + +- **status** - `String`, either ``online, here, active, available`` or ``idle, away`` +- **game** - `String/Number`, ID of Discord Game being played +- **callback** - `function` taking the following: + - **error** - error if any occurred + +setStatusIdle() +~~~~~~~~~~~~~~~ + +**Alias:** `setStatusAway` + +Sets the status of the Client to Idle/Away + +setStatusOnline() +~~~~~~~~~~~~~~~ + +**Aliases:** `setStatusHere`, `setStatusActive`, `setStatusAvailable` + +Sets the status of the Client to Online + +setTopic(channel, topic, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Sets the topic of a channel + +- **channel** - A `Channel Resolvable`_ +- **topic** - A `String` +- **callback** - `function` taking the following: + - **error** - error if any occurred + +setChannelName(channel, name, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Sets the name of a channel + +- **channel** - A `Channel Resolvable`_ +- **name** - A `String` +- **callback** - `function` taking the following: + - **error** - error if any occurred + +setChannelNameAndTopic(channel, name, topic, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Sets the name and topic of a channel + +- **channel** - A `Channel Resolvable`_ +- **name** - A `String` +- **topic** - A `String` +- **callback** - `function` taking the following: + - **error** - error if any occurred + +startTyping(channel, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Marks the client as typing in a channel. + +- **channel** - A `Channel Resolvable`_ +- **callback** - `function` taking the following: + - **error** - error if any occurred + +stopTyping(channel, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Marks the client as not typing in a channel (takes a few seconds to go active). + +- **channel** - A `Channel Resolvable`_ +- **callback** - `function` taking the following: + - **error** - error if any occurred + +updateDetails(details, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Updates the details of the client + +- **details** - `object` containing any of the following: + - **avatar** - `Base64 Resolvable`_, new avatar of the client + - **email** - `String`, new email of the client + - **newPassword** - `String`, new password of the client + - **username** - `String`, new username of the client +- **callback** - `function` taking the following: + - **error** - error if any occurred + +setAvatar(avatar, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Sets the avatar of the client + +- **avatar** - `Base64 Resolvable`_, new avatar of the client +- **callback** - `function` taking the following: + - **error** - error if any occurred + +setUsername(name, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Sets the username of the client + +- **username** - `String`, new username of the Client +- **callback** - `function` taking the following: + - **error** - error if any occurred + +joinVoiceChannel(channel, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Joins a Voice Channel to begin transmitting audio + +- **channel** - A `VoiceChannel Resolvable`_ +- **callback** - `function` that takes the following: + - **error** - error if any occurred + - **connection** - VoiceConnection_, the created Voice Connection. + +leaveVoiceChannel(`callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Leaves the currently connected Voice Channel if connected + +- **callback** - `function` that takes the following: + - **error** - error if any occurred + +createRole(server, `data`, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Creates a new role in a server. + +- **server** - a `Server Resolvable`_ +- **data** - `object` containing the structure below +- **callback** - `function` that takes the following: + - **error** - error if any occurred + - **role** - the created Role_ + +.. code-block:: js + + // structure of data parameter (all attrs optional): + { + color : 0xFF0000, + hoist : false, + name : "A New Role!", + permissions : [ + // see the constants documentation for full permissions + "attachFiles", "sendMessages" + ] + } + +updateRole(role, data, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Updates a role in a server. + +- **role** - a Role_ +- **data** - an `object` taking the structure shown below +- **callback** - a `function` taking the following: + - **error** - error if any occurred + - **role** - the updated Role_ + +.. code-block:: js + + // structure of data parameter (all attrs optional): + { + color : 0xFF0000, + hoist : false, + name : "A New Role!", + permissions : [ + // see the constants documentation for full permissions + "attachFiles", "sendMessages" + ] + } + +deleteRole(role, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Deletes a role from a server + +- **role** - The Role_ to delete +- **callback** - `function` that takes the following: + - **error** - error if any occurred + +addMemberToRole(member, role, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +**Aliases** : `addUserToRole` + +Adds a member of a server to a role in the server + +- **member** - A `User Resolvable`_ +- **role** - A Role_ +- **callback** - `function` that takes the following: + - **error** - error if any occurred + +removeMemberFromRole(member, role, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +**Aliases** : `removeUserFromRole` + +Removes a member of a server from a role in the server + +- **member** - A `User Resolvable`_ +- **role** - A Role_ +- **callback** - `function` that takes the following: + - **error** - error if any occurred + +overwritePermissions(channel, roleOrUser, options, `callback`) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Overwrites the permissions of a role or a user in a channel + +- **channel** - a `Channel Resolvable`_ +- **roleOrUser** - a Role_ or a User_ object +- **options** - an `object` containing a structure as shown below +- **callback** - `function` that takes the following: + - **error** - error if any occurred + +.. code-block:: js + + { + "sendMessages" : false, + "attachFiles" : true + } + +Events +------ + +`Discord.Client` is an EventEmitter, so you can use `.on()` and `.off()` to add and remove events. + +ready +~~~~~ + +Emitted when the client is ready to use + +debug +~~~~~ + +Emitted when the client debugs or wants to log something internally + +message +~~~~~~~ + +Emitted when the client receives a message, supplies a Message_ object. + +warn +~~~~ + +Emitted when the client has encountered a small error that can be avoided. + +messageDeleted +~~~~~~~~~~~~~~ + +Emitted when a message has been deleted and the Client finds out, supplies a Message_ object IF available, and a Channel_ object. + +messageUpdated +~~~~~~~~~~~~~~ + +Emitted when a message has been updated and the client finds out. Supplies two Message_ objects, the first being the new updated messages, the latter being the old message. + +disconnected +~~~~~~~~~~~~ + +Emitted when the client is disconnected from the Discord server. + +error +~~~~~ + +Emitted when the client runs into a big problem, supplies an error object. + +raw +~~~ + +Emitted when a message over WebSocket is received, it supplies one `object` containing the raw data from the WebSocket. + +serverCreated +~~~~~~~~~~~~~ + +Emitted when a server is joined by the Client, supplies a Server_ object. + +serverDeleted +~~~~~~~~~~~~~ + +Emitted when the client leaves a server, supplies a Server_ object. + +serverUpdated +~~~~~~~~~~~~~ + +Emitted when a server is updated (e.g. name change). Supplies two Server_ objects, the first being the server before the update, the second being the new, updated server. + +channelCreated +~~~~~~~~~~~~~~ + +Emitted when a channel is created, supplies a Channel_ object (includes PM chats as well as server channels). + +channelDeleted +~~~~~~~~~~~~~~ + +Emitted when a channel is deleted, supplies a Channel_ object. + +channelUpdated +~~~~~~~~~~~~~~ + +Emitted when a channel is updated (e.g. name/topic change). Supplies a Channel_ object. + +serverRoleCreated +~~~~~~~~~~~~~~~~~ + +Emitted when a role is created in a server, supplies a Role_ object. + +serverRoleDeleted +~~~~~~~~~~~~~~~~~ + +Emitted when a role is deleted from a server, supplies a Role_ object. + +serverRoleUpdated +~~~~~~~~~~~~~~~~~ + +Emitted when a role is updated in a server, supplies two Role_ objects. The first is the old role, the second is the updated role. + +serverNewMember +~~~~~~~~~~~~~~~ + +Emitted when a user joins a server, supplies a Server_ object and a User_ object. + +serverMemberRemoved +~~~~~~~~~~~~~~~~~~~ + +Emitted when a member is removed from a server. Supplies a Server_ object and a User_ object. + +serverMemberUpdated +~~~~~~~~~~~~~~~~~~~ + +Emitted when a member in a server is updated. Supplies a Server_ object and a User_ object. + +presence +~~~~~~~~ + +Emitted when a user goes online/offline/idle or starts/stops playing a game. Supplies 3 parameters, the first being the User_ object, the second being the status, the third being the game ID they are playing. + +userUpdated +~~~~~~~~~~~ + +Emitted when a user changes their name, avatar or similar. Supplies two User_ objects, the first being the user before being updated, the second being the updated user. + +userTypingStarted +~~~~~~~~~~~~~~~ + +Emitted when a user starts typing in a channel. Supplies two parameters, a User_ object and a Channel_ object. + +userTypingStopped +~~~~~~~~~~~~~~ + +Emitted when a user stop typing in a channel. Supplies two parameters, a User_ object and a Channel_ object. + +userBanned +~~~~~~~~~~ + +Emitted when a user is banned from a server. Supplies two parameters, a User_ object and a Server_ object. + +userUnbanned +~~~~~~~~~~ + +Emitted when a user is unbanned from a server. Supplies two parameters, a User_ object and a Server_ object. diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index bb458c423..3b7ecd8c2 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -24,24 +24,6 @@ import VoiceConnection from "../Voice/VoiceConnection"; var zlib; var libVersion = require('../../package.json').version; -//todo: move this somewhere else -var originalEnd = request.Request.prototype.end; -request.Request.prototype.end = function(callback) { - this.set('User-Agent', 'DiscordBot (https://github.com/hydrabolt/discord.js, ' + libVersion + ')'); - return new Promise((resolve, reject) => { - originalEnd.call(this, (err, response) => { - if (callback) { - callback(err, response); - } - - if (err) { - return reject(err); - } - resolve(response); - }); - }); -}; - function waitFor(condition, value = condition, interval = 20) { return new Promise(resolve => { var int = setInterval(() => { @@ -67,11 +49,37 @@ export default class InternalClient { this.setup(discordClient); } + apiRequest(method, url, useAuth, data, file) { + let ret = request[method](url); + if(useAuth) { + ret.set("authorization", this.token); + } + if(data) { + ret.send(data); + } + if(file) { + ret.attach("file", file.file, file.name); + } + ret.set('User-Agent', this.userAgentInfo.full); + return new Promise((resolve, reject) => { + ret.end((error, data) => { + if(error) { + return reject(error); + } + resolve(data.body); + }); + }); + } + setup(discordClient) { discordClient = discordClient || this.client; this.client = discordClient; this.state = ConnectionState.IDLE; this.websocket = null; + this.userAgent = { + url: 'https://github.com/hydrabolt/discord.js', + version: require('../../package.json').version + }; if (this.client.options.compress) { zlib = require("zlib"); @@ -121,6 +129,15 @@ export default class InternalClient { return (this.readyTime ? Date.now() - this.readyTime : null); } + set userAgent(info) { + info.full = `DiscordBot (${info.url}, ${info.version})`; + this.userAgentInfo = info; + } + + get userAgent() { + return this.userAgentInfo; + } + //def leaveVoiceChannel leaveVoiceChannel() { if (this.voiceConnection) { @@ -201,14 +218,10 @@ export default class InternalClient { createServer(name, region = "london") { name = this.resolver.resolveString(name); - return request - .post(Endpoints.SERVERS) - .set("authorization", this.token) - .send({ name, region }) - .end() + return this.apiRequest('post', Endpoints.SERVERS, true, { name, region }) .then(res => { // valid server, wait until it is cached - return waitFor(() => this.servers.get("id", res.body.id)); + return waitFor(() => this.servers.get("id", res.id)); }); } @@ -218,13 +231,10 @@ export default class InternalClient { if(!invite) { return Promise.reject(new Error("Not a valid invite")); } - return request - .post(Endpoints.INVITE(invite)) - .set("authorization", this.token) - .end() + return this.apiRequest("post", Endpoints.INVITE(invite), true) .then(res => { // valid server, wait until it is received via ws and cached - return waitFor(() => this.servers.get("id", res.body.guild.id)); + return waitFor(() => this.servers.get("id", res.guild.id)); }); } @@ -236,10 +246,7 @@ export default class InternalClient { return Promise.reject(new Error("server did not resolve")); } - return request - .del(Endpoints.SERVER(server.id)) - .set("authorization", this.token) - .end() + return this.apiRequest("del", Endpoints.SERVER(server.id), true) .then(() => { // remove channels of server then the server for (var chan of server.channels) { @@ -259,15 +266,12 @@ export default class InternalClient { this.state = ConnectionState.LOGGING_IN; - return request - .post(Endpoints.LOGIN) - .send({ + return this.apiRequest("post", Endpoints.LOGIN, false, { email, password }) - .end() .then(res => { - var token = res.body.token; + var token = res.token; this.state = ConnectionState.LOGGED_IN; this.token = token; this.email = email; @@ -295,10 +299,7 @@ export default class InternalClient { return Promise.reject(new Error("Client is not logged in!")); } - return request - .post(Endpoints.LOGOUT) - .set("authorization", this.token) - .end() + return this.apiRequest("post", Endpoints.LOGOUT, true) .then(() => { if (this.websocket) { this.websocket.close(); @@ -318,25 +319,18 @@ export default class InternalClient { return Promise.reject(new Error("Unable to resolve resUser to a User")); } // start the PM - return request - .post(`${Endpoints.USER_CHANNELS(user.id) }`) - .set("authorization", this.token) - .send({ + return this.apiRequest("post", Endpoints.USER_CHANNELS(user.id), true, { recipient_id: user.id }) - .end() .then(res => { - return this.private_channels.add(new PMChannel(res.body, this.client)); + return this.private_channels.add(new PMChannel(res, this.client)); }); } // def getGateway getGateway() { - return request - .get(Endpoints.GATEWAY) - .set("authorization", this.token) - .end() - .then(res => res.body.url); + return this.apiRequest("get", Endpoints.GATEWAY, true) + .then(res => res.url); } // def sendMessage @@ -348,17 +342,13 @@ export default class InternalClient { var content = this.resolver.resolveString(_content); var mentions = this.resolver.resolveMentions(content); - return request - .post(Endpoints.CHANNEL_MESSAGES(destination.id)) - .set("authorization", this.token) - .send({ + return this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(destination.id), true, { content: content, mentions: mentions, tts: options.tts }) - .end() .then(res => - destination.messages.add(new Message(res.body, destination, this.client)) + destination.messages.add(new Message(res, destination, this.client)) ); }); @@ -373,10 +363,7 @@ export default class InternalClient { var chain = options.wait ? delay(options.wait) : Promise.resolve(); return chain.then(() => - request - .del(Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id)) - .set("authorization", this.token) - .end() + this.apiRequest("del", Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true) ) .then(() => message.channel.messages.remove(message)); } @@ -393,18 +380,19 @@ export default class InternalClient { var content = this.resolver.resolveString(_content); var mentions = this.resolver.resolveMentions(content); - return request - .patch(Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id)) - .set("authorization", this.token) - .send({ - content: content, - tts: options.tts, - mentions: mentions - }) - .end() + return this.apiRequest( + "patch", + Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), + true, + { + content: content, + tts: options.tts, + mentions: mentions + } + ) .then(res => message.channel.messages.update( message, - new Message(res.body, message.channel, this.client) + new Message(res, message.channel, this.client) )); } @@ -412,12 +400,11 @@ export default class InternalClient { sendFile(where, _file, name = "image.png") { return this.resolver.resolveChannel(where) .then(channel => - request - .post(Endpoints.CHANNEL_MESSAGES(channel.id)) - .set("authorization", this.token) - .attach("file", this.resolver.resolveFile(_file), name) - .end() - .then(res => channel.messages.add(new Message(res.body, channel, this.client))) + this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(channel.id), true, null, { + name, + file: this.resolver.resolveFile(_file) + }) + .then(res => channel.messages.add(new Message(res, channel, this.client))) ); } @@ -439,11 +426,12 @@ export default class InternalClient { } } - return request - .get(Endpoints.CHANNEL_MESSAGES(channel.id) + "?" + qs.stringify(qsObject)) - .set("authorization", this.token) - .end() - .then(res => res.body.map( + return this.apiRequest( + "get", + `Endpoints.CHANNEL_MESSAGES(channel.id)?${qs.stringify(qsObject)}`, + true + ) + .then(res => res.map( msg => channel.messages.add(new Message(msg, channel, this.client)) )); }); @@ -453,12 +441,9 @@ export default class InternalClient { getBans(server) { server = this.resolver.resolveServer(server); - return request - .get(`${Endpoints.SERVER_BANS(server.id) }`) - .set("authorization", this.token) - .end() + return this.apiRequest("get", Endpoints.SERVER_BANS(server.id), true) .then(res => { - res.body.map(ban => { + res.map(ban => { return this.users.add(new User(ban.user, this.client)); }); }); @@ -469,20 +454,16 @@ export default class InternalClient { server = this.resolver.resolveServer(server); - return request - .post(Endpoints.SERVER_CHANNELS(server.id)) - .set("authorization", this.token) - .send({ + return this.apiRequest("post", Endpoints.SERVER_CHANNELS(server.id), true, { name, type }) - .end() .then(res => { var channel; - if (res.body.type === "text") { - channel = new TextChannel(res.body, this.client, server); - }else{ - channel = new VoiceChannel(res.body, this.client, server); + if (res.type === "text") { + channel = new TextChannel(res, this.client, server); + } else { + channel = new VoiceChannel(res, this.client, server); } return server.channels.add(this.channels.add(channel)); }); @@ -493,17 +474,12 @@ export default class InternalClient { return this.resolver.resolveChannel(_channel) .then(channel => - request - .del(Endpoints.CHANNEL(channel.id)) - .set("authorization", this.token) - .end() + this.apiRequest("del", Endpoints.CHANNEL(channel.id), true) .then(() => { channel.server.channels.remove(channel); this.channels.remove(channel); - }), error => { - error.message = "Couldn't resolve to channel - " + error.toString(); - throw error; - }); + }) + ); } // def banMember @@ -511,10 +487,11 @@ export default class InternalClient { user = this.resolver.resolveUser(user); server = this.resolver.resolveServer(server); - return request - .put(`${Endpoints.SERVER_BANS(server.id)}/${user.id}?delete-message-days=${length}`) - .set("authorization", this.token) - .end();//will expose api result, probably not bad tho + return this.apiRequest( + "put", + `${Endpoints.SERVER_BANS(server.id)}/${user.id}?delete-message-days=${length}`, + true + ); } // def unbanMember @@ -523,10 +500,7 @@ export default class InternalClient { server = this.resolver.resolveServer(server); user = this.resolver.resolveUser(user); - return request - .del(`${Endpoints.SERVER_BANS(server.id) }/${user.id}`) - .set("authorization", this.token) - .end();//will expose api result, probably not bad tho + return this.apiRequest("del", `${Endpoints.SERVER_BANS(server.id)}/${user.id}`, true) } // def kickMember @@ -534,28 +508,21 @@ export default class InternalClient { user = this.resolver.resolveUser(user); server = this.resolver.resolveServer(server); - return request - .del(`${Endpoints.SERVER_MEMBERS(server.id) }/${user.id}`) - .set("authorization", this.token) - .end(); + return this.apiRequest("del", `${Endpoints.SERVER_MEMBERS(server.id) }/${user.id}`, true); } // def createRole createRole(server, data) { server = this.resolver.resolveServer(server); - return request - .post(Endpoints.SERVER_ROLES(server.id)) - .set("authorization", this.token) - .end() + return this.apiRequest("post", Endpoints.SERVER_ROLES(server.id), true) .then(res => { - var role = server.roles.add(new Role(res.body, server, this.client)); + var role = server.roles.add(new Role(res, server, this.client)); if (data) { return this.updateRole(role, data); } return role; - }); } // def updateRole @@ -581,22 +548,15 @@ export default class InternalClient { } } - return request - .patch(Endpoints.SERVER_ROLES(server.id) + "/" + role.id) - .set("authorization", this.token) - .send(newData) - .end() + return this.apiRequest("patch", `${Endpoints.SERVER_ROLES(server.id)}/${role.id}`, true, newData) .then(res => { - return server.roles.update(role, new Role(res.body, server, this.client)); + return server.roles.update(role, new Role(res, server, this.client)); }); } // def deleteRole deleteRole(role) { - return request - .del(Endpoints.SERVER_ROLES(role.server.id) + "/" + role.id) - .set("authorization", this.token) - .end(); + return this.apiRequest("del", `${Endpoints.SERVER_ROLES(role.server.id)}/${role.id}`, true) } //def addMemberToRole @@ -613,13 +573,14 @@ export default class InternalClient { var roleIDS = role.server.memberMap[member.id].roles.map(r => r.id).concat(role.id); - return request - .patch(Endpoints.SERVER_MEMBERS(role.server.id) + "/" + member.id) - .set("authorization", this.token) - .send({ - roles: roleIDS - }) - .end(); + return this.apiRequest( + "patch", + `${Endpoints.SERVER_MEMBERS(role.server.id)}/${member.id}`, + true, + { + roles: roleIDS + } + ); } //def addMemberToRole @@ -643,13 +604,14 @@ export default class InternalClient { roleIDS.concat(role.id); } - return request - .patch(Endpoints.SERVER_MEMBERS(role.server.id) + "/" + member.id) - .set("authorization", this.token) - .send({ - roles: roleIDS - }) - .end(); + return this.apiRequest( + "patch", + `${Endpoints.SERVER_MEMBERS(role.server.id)}/${member.id}`, + true, + { + roles: roleIDS + } + ); } //def removeMemberFromRole @@ -673,13 +635,14 @@ export default class InternalClient { } } - return request - .patch(Endpoints.SERVER_MEMBERS(role.server.id) + "/" + member.id) - .set("authorization", this.token) - .send({ - roles: roleIDS - }) - .end(); + return this.apiRequest( + "patch", + `${Endpoints.SERVER_MEMBERS(role.server.id)}/${member.id}`, + true, + { + roles: roleIDS + } + ); } //def removeMemberFromRoles @@ -708,13 +671,14 @@ export default class InternalClient { } } - return request - .patch(Endpoints.SERVER_MEMBERS(role.server.id) + "/" + member.id) - .set("authorization", this.token) - .send({ - roles: roleIDS - }) - .end(); + return this.apiRequest( + "patch", + `${Endpoints.SERVER_MEMBERS(role.server.id)}/${member.id}`, + true, + { + roles: roleIDS + } + ); } // def createInvite @@ -749,12 +713,8 @@ export default class InternalClient { epoint = Endpoints.SERVER_INVITES(chanServ.id); } - return request - .post(epoint) - .set("authorization", this.token) - .send(options) - .end() - .then(res => new Invite(res.body, this.channels.get("id", res.body.channel.id), this.client)); + return this.apiRequest("post", epoint, true, options) + .then(res => new Invite(res, this.channels.get("id", res.channel.id), this.client)); } //def deleteInvite @@ -764,10 +724,7 @@ export default class InternalClient { if(!invite) { throw new Error("Not a valid invite"); } - return request - .del(Endpoints.INVITE(invite)) - .set("authorization", this.token) - .end(); + return this.apiRequest("del", Endpoints.INVITE(invite), true); } //def overwritePermissions @@ -812,11 +769,12 @@ export default class InternalClient { } } - return request - .put(Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id) - .set("authorization", this.token) - .send(data) - .end(); + return this.apiRequest( + "put", + `${Endpoints.CHANNEL_PERMISSIONS(channel.id)}/${data.id}`, + true, + data + ); }); } @@ -852,10 +810,7 @@ export default class InternalClient { //def sendTyping sendTyping(channel) { return this.resolver.resolveChannel(channel).then(channel => - request - .post(Endpoints.CHANNEL(channel.id) + "/typing") - .set("authorization", this.token) - .end() + this.apiRequest("post", Endpoints.CHANNEL(channel.id) + "/typing", true) ); } @@ -886,7 +841,7 @@ export default class InternalClient { .then(channel => { if(!this.intervals.typing[channel.id]){ - // typing interval doesn't exist + // typing interval doesn"t exist throw new Error("Not typing in that channel"); } @@ -898,17 +853,13 @@ export default class InternalClient { //def updateDetails updateDetails(data) { - return request - .patch(Endpoints.ME) - .set("authorization", this.token) - .send({ + return this.apiRequest("patch", Endpoints.ME, true, { avatar: this.resolver.resolveToBase64(data.avatar) || this.user.avatar, - email : data.email || this.email, - new_password : data.newPassword || null, - password : data.password || this.password, - username : data.username || this.user.username - }) - .end(); + email: data.email || this.email, + new_password: data.newPassword || null, + password: data.password || this.password, + username: data.username || this.user.username + }); } //def setAvatar @@ -925,16 +876,12 @@ export default class InternalClient { setChannelTopic(chann, topic = "") { return this.resolver.resolveChannel(chann) .then(channel => - request - .patch(Endpoints.CHANNEL(channel.id)) - .set("authorization", this.token) - .send({ + this.apiRequest("patch", Endpoints.CHANNEL(channel.id), true, { name: channel.name, position: channel.position, topic: topic }) - .end() - .then(res => channel.topic = res.body.topic) + .then(res => channel.topic = res.topic) ); } @@ -942,16 +889,12 @@ export default class InternalClient { setChannelName(chann, name = "discordjs_is_the_best") { return this.resolver.resolveChannel(chann) .then(channel => - request - .patch(Endpoints.CHANNEL(channel.id)) - .set("authorization", this.token) - .send({ + this.apiRequest("patch", Endpoints.CHANNEL(channel.id), true, { name: name, position: channel.position, topic: channel.topic }) - .end() - .then(res => channel.name = res.body.name) + .then(res => channel.name = res.name) ); } @@ -959,18 +902,14 @@ export default class InternalClient { setChannelNameAndTopic(chann, name = "discordjs_is_the_best", topic = "") { return this.resolver.resolveChannel(chann) .then(channel => - request - .patch(Endpoints.CHANNEL(channel.id)) - .set("authorization", this.token) - .send({ + this.apiRequest("patch", Endpoints.CHANNEL(channel.id), true, { name: name, position: channel.position, topic: topic }) - .end() .then(res => { - channel.name = res.body.name; - channel.topic = res.body.topic; + channel.name = res.name; + channel.topic = res.topic; }) ); } @@ -979,16 +918,12 @@ export default class InternalClient { setChannelPosition(chann, position = 0) { return this.resolver.resolveChannel(chann) .then(channel => - request - .patch(Endpoints.CHANNEL(channel.id)) - .set("authorization", this.token) - .send({ + this.apiRequest("patch", Endpoints.CHANNEL(channel.id), true, { name: channel.name, position: position, topic: channel.topic }) - .end() - .then(res => channel.position = res.body.position) + .then(res => channel.position = res.position) ); } @@ -1005,10 +940,7 @@ export default class InternalClient { Promise.reject(new Error("Message does not exist")); } - return request - .post(Endpoints.CHANNEL_MESSAGE(msg.channel.id, msg.id) + "/ack") - .set("authorization", this.token) - .end(); + return this.apiRequest("post", Endpoints.CHANNEL_MESSAGE(msg.channel.id, msg.id) + "/ack", true); } sendWS(object) {