chore: consistency/prettier (#3852)

* chore: consistency/prettier

* chore: rebase

* chore: rebase

* chore: include typings

* fix: include typings file in prettier lint-staged
This commit is contained in:
Crawl
2020-02-29 14:35:57 +01:00
committed by GitHub
parent 6650d50a56
commit c065156a88
127 changed files with 5198 additions and 4513 deletions

View File

@@ -1,5 +1,6 @@
{ {
"extends": "eslint:recommended", "extends": ["eslint:recommended", "plugin:prettier/recommended"],
"plugins": ["import"],
"parserOptions": { "parserOptions": {
"ecmaVersion": 2019 "ecmaVersion": 2019
}, },
@@ -7,42 +8,58 @@
"es6": true, "es6": true,
"node": true "node": true
}, },
"overrides": [ "overrides": [{ "files": ["*.browser.js"], "env": { "browser": true } }],
{ "files": ["*.browser.js"], "env": { "browser": true } }
],
"rules": { "rules": {
"import/order": [
"error",
{
"groups": ["builtin", "external", "internal", "index", "sibling", "parent"],
"alphabetize": {
"order": "asc"
}
}
],
"prettier/prettier": [
2,
{
"printWidth": 120,
"singleQuote": true,
"quoteProps": "as-needed",
"trailingComma": "all",
"endOfLine": "lf"
}
],
"strict": ["error", "global"], "strict": ["error", "global"],
"no-await-in-loop": "warn", "no-await-in-loop": "warn",
"no-compare-neg-zero": "error", "no-compare-neg-zero": "error",
"no-extra-parens": ["warn", "all", {
"nestedBinaryExpressions": false
}],
"no-template-curly-in-string": "error", "no-template-curly-in-string": "error",
"no-unsafe-negation": "error", "no-unsafe-negation": "error",
"valid-jsdoc": ["error", { "valid-jsdoc": [
"requireReturn": false, "error",
"requireReturnDescription": false, {
"prefer": { "requireReturn": false,
"return": "returns", "requireReturnDescription": false,
"arg": "param" "prefer": {
}, "return": "returns",
"preferType": { "arg": "param"
"String": "string", },
"Number": "number", "preferType": {
"Boolean": "boolean", "String": "string",
"Symbol": "symbol", "Number": "number",
"object": "Object", "Boolean": "boolean",
"function": "Function", "Symbol": "symbol",
"array": "Array", "object": "Object",
"date": "Date", "function": "Function",
"error": "Error", "array": "Array",
"null": "void" "date": "Date",
"error": "Error",
"null": "void"
}
} }
}], ],
"accessor-pairs": "warn", "accessor-pairs": "warn",
"array-callback-return": "error", "array-callback-return": "error",
"complexity": "warn",
"consistent-return": "error", "consistent-return": "error",
"curly": ["error", "multi-line", "consistent"], "curly": ["error", "multi-line", "consistent"],
"dot-location": ["error", "property"], "dot-location": ["error", "property"],
@@ -100,7 +117,6 @@
"func-names": "error", "func-names": "error",
"func-name-matching": "error", "func-name-matching": "error",
"func-style": ["error", "declaration", { "allowArrowFunctions": true }], "func-style": ["error", "declaration", { "allowArrowFunctions": true }],
"indent": ["error", 2, { "SwitchCase": 1 }],
"key-spacing": "error", "key-spacing": "error",
"keyword-spacing": "error", "keyword-spacing": "error",
"max-depth": "error", "max-depth": "error",
@@ -112,7 +128,6 @@
"no-array-constructor": "error", "no-array-constructor": "error",
"no-inline-comments": "error", "no-inline-comments": "error",
"no-lonely-if": "error", "no-lonely-if": "error",
"no-mixed-operators": "error",
"no-multiple-empty-lines": ["error", { "max": 2, "maxEOF": 1, "maxBOF": 0 }], "no-multiple-empty-lines": ["error", { "max": 2, "maxEOF": 1, "maxBOF": 0 }],
"no-new-object": "error", "no-new-object": "error",
"no-spaced-func": "error", "no-spaced-func": "error",
@@ -122,18 +137,20 @@
"nonblock-statement-body-position": "error", "nonblock-statement-body-position": "error",
"object-curly-spacing": ["error", "always"], "object-curly-spacing": ["error", "always"],
"operator-assignment": "error", "operator-assignment": "error",
"operator-linebreak": ["error", "after"],
"padded-blocks": ["error", "never"], "padded-blocks": ["error", "never"],
"quote-props": ["error", "as-needed"], "quote-props": ["error", "as-needed"],
"quotes": ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }], "quotes": ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
"semi-spacing": "error", "semi-spacing": "error",
"semi": "error", "semi": "error",
"space-before-blocks": "error", "space-before-blocks": "error",
"space-before-function-paren": ["error", { "space-before-function-paren": [
"anonymous": "never", "error",
"named": "never", {
"asyncArrow": "always" "anonymous": "never",
}], "named": "never",
"asyncArrow": "always"
}
],
"space-in-parens": "error", "space-in-parens": "error",
"space-infix-ops": "error", "space-infix-ops": "error",
"space-unary-ops": "error", "space-unary-ops": "error",

View File

@@ -7,6 +7,7 @@ pull request. We use ESLint to enforce a consistent coding style, so having that
is a great boon to your development process. is a great boon to your development process.
## Setup ## Setup
To get ready to work on the codebase, please do the following: To get ready to work on the codebase, please do the following:
1. Fork & clone the repository, and make sure you're on the **master** branch 1. Fork & clone the repository, and make sure you're on the **master** branch

View File

@@ -1,12 +1,11 @@
--- ---
name: Bug report name: Bug report
about: Report incorrect or unexpected behaviour of discord.js about: Report incorrect or unexpected behaviour of discord.js
title: '' title: ''
labels: 's: unverified, type: bug' labels: 's: unverified, type: bug'
assignees: '' assignees: ''
--- ---
<!-- <!--
If you need help with discord.js installation or usage, please go to the discord.js Discord server instead: If you need help with discord.js installation or usage, please go to the discord.js Discord server instead:
https://discord.gg/bRCvFy9 https://discord.gg/bRCvFy9
@@ -16,15 +15,14 @@ You won't receive any basic help here.
**Please describe the problem you are having in as much detail as possible:** **Please describe the problem you are having in as much detail as possible:**
**Include a reproducible code sample here, if possible:** **Include a reproducible code sample here, if possible:**
```js ```js
// Place your code here // Place your code here
``` ```
**Further details:** **Further details:**
- discord.js version: - discord.js version:
- Node.js version: - Node.js version:
- Operating system: - Operating system:

View File

@@ -1,12 +1,11 @@
--- ---
name: Feature request name: Feature request
about: Request a feature for the core discord.js library about: Request a feature for the core discord.js library
title: '' title: ''
labels: 'type: enhancement' labels: 'type: enhancement'
assignees: '' assignees: ''
--- ---
<!-- <!--
If you need help with discord.js installation or usage, please go to the discord.js Discord server instead: If you need help with discord.js installation or usage, please go to the discord.js Discord server instead:
https://discord.gg/bRCvFy9 https://discord.gg/bRCvFy9

View File

@@ -1,11 +1,12 @@
**Please describe the changes this PR makes and why it should be merged:** **Please describe the changes this PR makes and why it should be merged:**
**Status** **Status**
- [ ] Code changes have been tested against the Discord API, or there are no code changes - [ ] Code changes have been tested against the Discord API, or there are no code changes
- [ ] I know how to update typings and have done so, or typings don't need updating - [ ] I know how to update typings and have done so, or typings don't need updating
**Semantic versioning classification:** **Semantic versioning classification:**
- [ ] This PR changes the library's interface (methods or parameters added) - [ ] This PR changes the library's interface (methods or parameters added)
- [ ] This PR includes breaking changes (methods removed or renamed, parameters moved or removed) - [ ] This PR includes breaking changes (methods removed or renamed, parameters moved or removed)
- [ ] This PR **only** includes non-code changes, like changes to documentation, README, etc. - [ ] This PR **only** includes non-code changes, like changes to documentation, README, etc.

View File

@@ -1,12 +1,8 @@
{ {
"ecmaVersion": 7, "ecmaVersion": 7,
"libs": [], "libs": [],
"loadEagerly": [ "loadEagerly": ["./src/*.js"],
"./src/*.js" "dontLoad": ["node_modules/**"],
],
"dontLoad": [
"node_modules/**"
],
"plugins": { "plugins": {
"es_modules": {}, "es_modules": {},
"node": {}, "node": {},

View File

@@ -18,17 +18,19 @@
</div> </div>
## Table of contents ## Table of contents
- [About](#about) - [About](#about)
- [Installation](#installation) - [Installation](#installation)
- [Audio engines](#audio-engines) - [Audio engines](#audio-engines)
- [Optional packages](#optional-packages) - [Optional packages](#optional-packages)
- [Example Usage](#example-usage) - [Example Usage](#example-usage)
- [Links](#links) - [Links](#links)
- [Extensions](#extensions) - [Extensions](#extensions)
- [Contributing](#contributing) - [Contributing](#contributing)
- [Help](#help) - [Help](#help)
## About ## About
discord.js is a powerful [Node.js](https://nodejs.org) module that allows you to easily interact with the discord.js is a powerful [Node.js](https://nodejs.org) module that allows you to easily interact with the
[Discord API](https://discordapp.com/developers/docs/intro). [Discord API](https://discordapp.com/developers/docs/intro).
@@ -38,6 +40,7 @@ discord.js is a powerful [Node.js](https://nodejs.org) module that allows you to
- 100% coverage of the Discord API - 100% coverage of the Discord API
## Installation ## Installation
**Node.js 11.0.0 or newer is required.** **Node.js 11.0.0 or newer is required.**
Ignore any warnings about unmet peer dependencies, as they're all optional. Ignore any warnings about unmet peer dependencies, as they're all optional.
@@ -46,20 +49,23 @@ With voice support ([@discordjs/opus](https://www.npmjs.com/package/@discordjs/o
With voice support ([opusscript](https://www.npmjs.com/package/opusscript)): `npm install discordjs/discord.js opusscript` With voice support ([opusscript](https://www.npmjs.com/package/opusscript)): `npm install discordjs/discord.js opusscript`
### Audio engines ### Audio engines
The preferred audio engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus. The preferred audio engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus.
Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working. Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working.
For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers. For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers.
### Optional packages ### Optional packages
- [zlib-sync](https://www.npmjs.com/package/zlib-sync) for WebSocket data compression and inflation (`npm install zlib-sync`) - [zlib-sync](https://www.npmjs.com/package/zlib-sync) for WebSocket data compression and inflation (`npm install zlib-sync`)
- [erlpack](https://github.com/discordapp/erlpack) for significantly faster WebSocket data (de)serialisation (`npm install discordapp/erlpack`) - [erlpack](https://github.com/discordapp/erlpack) for significantly faster WebSocket data (de)serialisation (`npm install discordapp/erlpack`)
- One of the following packages can be installed for faster voice packet encryption and decryption: - One of the following packages can be installed for faster voice packet encryption and decryption:
- [sodium](https://www.npmjs.com/package/sodium) (`npm install sodium`) - [sodium](https://www.npmjs.com/package/sodium) (`npm install sodium`)
- [libsodium.js](https://www.npmjs.com/package/libsodium-wrappers) (`npm install libsodium-wrappers`) - [libsodium.js](https://www.npmjs.com/package/libsodium-wrappers) (`npm install libsodium-wrappers`)
- [bufferutil](https://www.npmjs.com/package/bufferutil) for a much faster WebSocket connection (`npm install bufferutil`) - [bufferutil](https://www.npmjs.com/package/bufferutil) for a much faster WebSocket connection (`npm install bufferutil`)
- [utf-8-validate](https://www.npmjs.com/package/utf-8-validate) in combination with `bufferutil` for much faster WebSocket processing (`npm install utf-8-validate`) - [utf-8-validate](https://www.npmjs.com/package/utf-8-validate) in combination with `bufferutil` for much faster WebSocket processing (`npm install utf-8-validate`)
## Example usage ## Example usage
```js ```js
const Discord = require('discord.js'); const Discord = require('discord.js');
const client = new Discord.Client(); const client = new Discord.Client();
@@ -78,24 +84,28 @@ client.login('token');
``` ```
## Links ## Links
* [Website](https://discord.js.org/) ([source](https://github.com/discordjs/website))
* [Documentation](https://discord.js.org/#/docs/main/master/general/welcome) - [Website](https://discord.js.org/) ([source](https://github.com/discordjs/website))
* [Guide](https://discordjs.guide/) ([source](https://github.com/discordjs/guide)) - this is still for stable - [Documentation](https://discord.js.org/#/docs/main/master/general/welcome)
- [Guide](https://discordjs.guide/) ([source](https://github.com/discordjs/guide)) - this is still for stable
See also the [Update Guide](https://discordjs.guide/additional-info/changes-in-v12.html), including updated and removed items in the library. See also the [Update Guide](https://discordjs.guide/additional-info/changes-in-v12.html), including updated and removed items in the library.
* [Discord.js Discord server](https://discord.gg/bRCvFy9) - [Discord.js Discord server](https://discord.gg/bRCvFy9)
* [Discord API Discord server](https://discord.gg/discord-api) - [Discord API Discord server](https://discord.gg/discord-api)
* [GitHub](https://github.com/discordjs/discord.js) - [GitHub](https://github.com/discordjs/discord.js)
* [NPM](https://www.npmjs.com/package/discord.js) - [NPM](https://www.npmjs.com/package/discord.js)
* [Related libraries](https://discordapi.com/unofficial/libs.html) - [Related libraries](https://discordapi.com/unofficial/libs.html)
### Extensions ### Extensions
* [RPC](https://www.npmjs.com/package/discord-rpc) ([source](https://github.com/discordjs/RPC))
- [RPC](https://www.npmjs.com/package/discord-rpc) ([source](https://github.com/discordjs/RPC))
## Contributing ## Contributing
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
[documentation](https://discord.js.org/#/docs). [documentation](https://discord.js.org/#/docs).
See [the contribution guide](https://github.com/discordjs/discord.js/blob/master/.github/CONTRIBUTING.md) if you'd like to submit a PR. See [the contribution guide](https://github.com/discordjs/discord.js/blob/master/.github/CONTRIBUTING.md) if you'd like to submit a PR.
## Help ## Help
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle
nudge in the right direction, please don't hesitate to join our official [Discord.js Server](https://discord.gg/bRCvFy9). nudge in the right direction, please don't hesitate to join our official [Discord.js Server](https://discord.gg/bRCvFy9).

View File

@@ -24,13 +24,13 @@ client.on('ready', () => {
}); });
client.on('message', message => { client.on('message', message => {
// If the message is '!rip' // If the message is '!rip'
if (message.content === '!rip') { if (message.content === '!rip') {
// Create the attachment using MessageAttachment // Create the attachment using MessageAttachment
const attachment = new MessageAttachment('https://i.imgur.com/w3duR07.png'); const attachment = new MessageAttachment('https://i.imgur.com/w3duR07.png');
// Send the attachment in the message channel // Send the attachment in the message channel
message.channel.send(attachment); message.channel.send(attachment);
} }
}); });
// Log our bot in using the token from https://discordapp.com/developers/applications/me // Log our bot in using the token from https://discordapp.com/developers/applications/me
@@ -59,13 +59,13 @@ client.on('ready', () => {
}); });
client.on('message', message => { client.on('message', message => {
// If the message is '!rip' // If the message is '!rip'
if (message.content === '!rip') { if (message.content === '!rip') {
// Create the attachment using MessageAttachment // Create the attachment using MessageAttachment
const attachment = new MessageAttachment('https://i.imgur.com/w3duR07.png'); const attachment = new MessageAttachment('https://i.imgur.com/w3duR07.png');
// Send the attachment in the message channel with a content // Send the attachment in the message channel with a content
message.channel.send(`${message.author},`, attachment); message.channel.send(`${message.author},`, attachment);
} }
}); });
// Log our bot in using the token from https://discordapp.com/developers/applications/me // Log our bot in using the token from https://discordapp.com/developers/applications/me
@@ -96,13 +96,13 @@ client.on('ready', () => {
}); });
client.on('message', message => { client.on('message', message => {
// If the message is '!rip' // If the message is '!rip'
if (message.content === '!rip') { if (message.content === '!rip') {
// Create the attachment using MessageAttachment // Create the attachment using MessageAttachment
const attachment = new MessageAttachment('./rip.png'); const attachment = new MessageAttachment('./rip.png');
// Send the attachment in the message channel with a content // Send the attachment in the message channel with a content
message.channel.send(`${message.author},`, attachment); message.channel.send(`${message.author},`, attachment);
} }
}); });
// Log our bot in using the token from https://discordapp.com/developers/applications/me // Log our bot in using the token from https://discordapp.com/developers/applications/me
@@ -137,21 +137,21 @@ client.on('ready', () => {
}); });
client.on('message', message => { client.on('message', message => {
// If the message is '!memes' // If the message is '!memes'
if (message.content === '!memes') { if (message.content === '!memes') {
// Get the buffer from the 'memes.txt', assuming that the file exists // Get the buffer from the 'memes.txt', assuming that the file exists
const buffer = fs.readFileSync('./memes.txt'); const buffer = fs.readFileSync('./memes.txt');
/** /**
* Create the attachment using MessageAttachment, * Create the attachment using MessageAttachment,
* overwritting the default file name to 'memes.txt' * overwritting the default file name to 'memes.txt'
* Read more about it over at * Read more about it over at
* http://discord.js.org/#/docs/main/master/class/MessageAttachment * http://discord.js.org/#/docs/main/master/class/MessageAttachment
*/ */
const attachment = new MessageAttachment(buffer, 'memes.txt'); const attachment = new MessageAttachment(buffer, 'memes.txt');
// Send the attachment in the message channel with a content // Send the attachment in the message channel with a content
message.channel.send(`${message.author}, here are your memes!`, attachment); message.channel.send(`${message.author}, here are your memes!`, attachment);
} }
}); });
// Log our bot in using the token from https://discordapp.com/developers/applications/me // Log our bot in using the token from https://discordapp.com/developers/applications/me

View File

@@ -1,3 +1,5 @@
'use strict';
/** /**
* An example of how you can send embeds * An example of how you can send embeds
*/ */
@@ -26,7 +28,7 @@ client.on('message', message => {
// Set the title of the field // Set the title of the field
.setTitle('A slick little embed') .setTitle('A slick little embed')
// Set the color of the embed // Set the color of the embed
.setColor(0xFF0000) .setColor(0xff0000)
// Set the main content of the embed // Set the main content of the embed
.setDescription('Hello, this is a slick embed!'); .setDescription('Hello, this is a slick embed!');
// Send the embed to the same channel as the message // Send the embed to the same channel as the message

View File

@@ -4,7 +4,7 @@ In here, you'll see some basic examples for kicking and banning a member.
## Kicking a member ## Kicking a member
Let's say you have a member that you'd like to kick. Here is an example of how you *can* do it. Let's say you have a member that you'd like to kick. Here is an example of how you _can_ do it.
```js ```js
// Import the discord.js module // Import the discord.js module
@@ -41,24 +41,27 @@ client.on('message', message => {
* Make sure you run this on a member, not a user! * Make sure you run this on a member, not a user!
* There are big differences between a user and a member * There are big differences between a user and a member
*/ */
member.kick('Optional reason that will display in the audit logs').then(() => { member
// We let the message author know we were able to kick the person .kick('Optional reason that will display in the audit logs')
message.reply(`Successfully kicked ${user.tag}`); .then(() => {
}).catch(err => { // We let the message author know we were able to kick the person
// An error happened message.reply(`Successfully kicked ${user.tag}`);
// This is generally due to the bot not being able to kick the member, })
// either due to missing permissions or role hierarchy .catch(err => {
message.reply('I was unable to kick the member'); // An error happened
// Log the error // This is generally due to the bot not being able to kick the member,
console.error(err); // either due to missing permissions or role hierarchy
}); message.reply('I was unable to kick the member');
// Log the error
console.error(err);
});
} else { } else {
// The mentioned user isn't in this guild // The mentioned user isn't in this guild
message.reply('That user isn\'t in this guild!'); message.reply("That user isn't in this guild!");
} }
// Otherwise, if no user was mentioned // Otherwise, if no user was mentioned
} else { } else {
message.reply('You didn\'t mention the user to kick!'); message.reply("You didn't mention the user to kick!");
} }
} }
}); });
@@ -112,26 +115,29 @@ client.on('message', message => {
* Read more about what ban options there are over at * Read more about what ban options there are over at
* https://discord.js.org/#/docs/main/master/class/GuildMember?scrollTo=ban * https://discord.js.org/#/docs/main/master/class/GuildMember?scrollTo=ban
*/ */
member.ban({ member
reason: 'They were bad!', .ban({
}).then(() => { reason: 'They were bad!',
// We let the message author know we were able to ban the person })
message.reply(`Successfully banned ${user.tag}`); .then(() => {
}).catch(err => { // We let the message author know we were able to ban the person
// An error happened message.reply(`Successfully banned ${user.tag}`);
// This is generally due to the bot not being able to ban the member, })
// either due to missing permissions or role hierarchy .catch(err => {
message.reply('I was unable to ban the member'); // An error happened
// Log the error // This is generally due to the bot not being able to ban the member,
console.error(err); // either due to missing permissions or role hierarchy
}); message.reply('I was unable to ban the member');
// Log the error
console.error(err);
});
} else { } else {
// The mentioned user isn't in this guild // The mentioned user isn't in this guild
message.reply('That user isn\'t in this guild!'); message.reply("That user isn't in this guild!");
} }
} else { } else {
// Otherwise, if no user was mentioned // Otherwise, if no user was mentioned
message.reply('You didn\'t mention the user to ban!'); message.reply("You didn't mention the user to ban!");
} }
} }
}); });

View File

@@ -1,22 +1,26 @@
# Frequently Asked Questions # Frequently Asked Questions
These questions are some of the most frequently asked. These questions are some of the most frequently asked.
## No matter what, I get `SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode`‽ ## No matter what, I get `SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode`‽
Update to Node.js 11.0.0 or newer. Update to Node.js 11.0.0 or newer.
## How do I get voice working? ## How do I get voice working?
- Install FFMPEG. - Install FFMPEG.
- Install either the `@discordjs/opus` package or the `opusscript` package. - Install either the `@discordjs/opus` package or the `opusscript` package.
@discordjs/opus is greatly preferred, due to it having significantly better performance. @discordjs/opus is greatly preferred, due to it having significantly better performance.
## How do I install FFMPEG? ## How do I install FFMPEG?
- **npm:** `npm install ffmpeg-binaries` - **npm:** `npm install ffmpeg-binaries`
- **Ubuntu 16.04:** `sudo apt install ffmpeg` - **Ubuntu 16.04:** `sudo apt install ffmpeg`
- **Ubuntu 14.04:** `sudo apt-get install libav-tools` - **Ubuntu 14.04:** `sudo apt-get install libav-tools`
- **Windows:** `npm install ffmpeg-binaries` or see the [FFMPEG section of AoDude's guide](https://github.com/bdistin/OhGodMusicBot/blob/master/README.md#download-ffmpeg). - **Windows:** `npm install ffmpeg-binaries` or see the [FFMPEG section of AoDude's guide](https://github.com/bdistin/OhGodMusicBot/blob/master/README.md#download-ffmpeg).
## How do I set up @discordjs/opus? ## How do I set up @discordjs/opus?
- **Ubuntu:** Simply run `npm install @discordjs/opus`, and it's done. Congrats! - **Ubuntu:** Simply run `npm install @discordjs/opus`, and it's done. Congrats!
- **Windows:** Run `npm install --global --production windows-build-tools` in an admin command prompt or PowerShell. - **Windows:** Run `npm install --global --production windows-build-tools` in an admin command prompt or PowerShell.
Then, running `npm install @discordjs/opus` in your bot's directory should successfully build it. Woo! Then, running `npm install @discordjs/opus` in your bot's directory should successfully build it. Woo!

View File

@@ -1,72 +1,87 @@
# Version 11.1.0 # Version 11.1.0
v11.1.0 features improved voice and gateway stability, as well as support for new features such as audit logs and searching for messages. v11.1.0 features improved voice and gateway stability, as well as support for new features such as audit logs and searching for messages.
See [the changelog](https://github.com/discordjs/discord.js/releases/tag/11.1.0) for a full list of changes, including See [the changelog](https://github.com/discordjs/discord.js/releases/tag/11.1.0) for a full list of changes, including
information about deprecations. information about deprecations.
# Version 11 # Version 11
Version 11 contains loads of new and improved features, optimisations, and bug fixes. Version 11 contains loads of new and improved features, optimisations, and bug fixes.
See [the changelog](https://github.com/discordjs/discord.js/releases/tag/11.0.0) for a full list of changes. See [the changelog](https://github.com/discordjs/discord.js/releases/tag/11.0.0) for a full list of changes.
## Significant additions ## Significant additions
* Message Reactions and Embeds (rich text)
* Support for uws and erlpack for better performance - Message Reactions and Embeds (rich text)
* OAuthApplication support - Support for uws and erlpack for better performance
* Web distributions - OAuthApplication support
- Web distributions
## Breaking changes ## Breaking changes
### Client.login() no longer supports logging in with email + password ### Client.login() no longer supports logging in with email + password
Logging in with an email and password has always been heavily discouraged since the advent of proper token support, but in v11 we have made the decision to completely remove the functionality, since Hammer & Chisel have [officially stated](https://github.com/hammerandchisel/discord-api-docs/issues/69#issuecomment-223886862) it simply shouldn't be done. Logging in with an email and password has always been heavily discouraged since the advent of proper token support, but in v11 we have made the decision to completely remove the functionality, since Hammer & Chisel have [officially stated](https://github.com/hammerandchisel/discord-api-docs/issues/69#issuecomment-223886862) it simply shouldn't be done.
User accounts can still log in with tokens just like bot accounts. To obtain the token for a user account, you can log in to Discord with that account, and use Ctrl + Shift + I to open the developer tools. In the console tab, evaluating `localStorage.token` will give you the token for that account. User accounts can still log in with tokens just like bot accounts. To obtain the token for a user account, you can log in to Discord with that account, and use Ctrl + Shift + I to open the developer tools. In the console tab, evaluating `localStorage.token` will give you the token for that account.
### ClientUser.setEmail()/setPassword() now require the current password, as well as setUsername() on user accounts ### ClientUser.setEmail()/setPassword() now require the current password, as well as setUsername() on user accounts
Since you can no longer log in with email and password, you must provide the current account password to the `setEmail()`, `setPassword()`, and `setUsername()` methods for user accounts (self-bots). Since you can no longer log in with email and password, you must provide the current account password to the `setEmail()`, `setPassword()`, and `setUsername()` methods for user accounts (self-bots).
### Removed TextBasedChannel.sendTTSMessage() ### Removed TextBasedChannel.sendTTSMessage()
This method was deemed to be an entirely pointless shortcut that virtually nobody even used. This method was deemed to be an entirely pointless shortcut that virtually nobody even used.
The same results can be achieved by passing options to `send()` or `sendMessage()`. The same results can be achieved by passing options to `send()` or `sendMessage()`.
Example: Example:
```js ```js
channel.send('Hi there', { tts: true }); channel.send('Hi there', { tts: true });
``` ```
### Using Collection.find()/exists() with IDs will throw an error ### Using Collection.find()/exists() with IDs will throw an error
This is simply to help prevent a common mistake that is made frequently. This is simply to help prevent a common mistake that is made frequently.
To find something or check its existence using an ID, you should use `.get()` and `.has()` which are part of the [ES6 Map class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map), which Collection is an extension of. To find something or check its existence using an ID, you should use `.get()` and `.has()` which are part of the [ES6 Map class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map), which Collection is an extension of.
# Version 10 # Version 10
Version 10's non-BC changes focus on cleaning up some inconsistencies that exist in previous versions. Version 10's non-BC changes focus on cleaning up some inconsistencies that exist in previous versions.
Upgrading from v9 should be quick and painless. Upgrading from v9 should be quick and painless.
## Client options ## Client options
All client options have been converted to camelCase rather than snake_case, and `max_message_cache` was renamed to `messageCacheMaxSize`. All client options have been converted to camelCase rather than snake_case, and `max_message_cache` was renamed to `messageCacheMaxSize`.
v9 code example: v9 code example:
```js ```js
const client = new Discord.Client({ const client = new Discord.Client({
disable_everyone: true, disable_everyone: true,
max_message_cache: 500, max_message_cache: 500,
message_cache_lifetime: 120, message_cache_lifetime: 120,
message_sweep_interval: 60 message_sweep_interval: 60,
}); });
``` ```
v10 code example: v10 code example:
```js ```js
const client = new Discord.Client({ const client = new Discord.Client({
disableEveryone: true, disableEveryone: true,
messageCacheMaxSize: 500, messageCacheMaxSize: 500,
messageCacheLifetime: 120, messageCacheLifetime: 120,
messageSweepInterval: 60 messageSweepInterval: 60,
}); });
``` ```
## Presences ## Presences
Presences have been completely restructured. Presences have been completely restructured.
Previous versions of discord.js assumed that users had the same presence amongst all guilds - with the introduction of sharding, however, this is no longer the case. Previous versions of discord.js assumed that users had the same presence amongst all guilds - with the introduction of sharding, however, this is no longer the case.
v9 discord.js code may look something like this: v9 discord.js code may look something like this:
```js ```js
User.status; // the status of the user User.status; // the status of the user
User.game; // the game that the user is playing User.game; // the game that the user is playing
@@ -81,6 +96,7 @@ Additionally, the introduction of the Presence class keeps all of the presence d
A user may have an entirely different presence between two different guilds.** A user may have an entirely different presence between two different guilds.**
v10 code: v10 code:
```js ```js
MemberOrUser.presence.status; // the status of the member or user MemberOrUser.presence.status; // the status of the member or user
MemberOrUser.presence.game; // the game that the member or user is playing MemberOrUser.presence.game; // the game that the member or user is playing
@@ -90,41 +106,46 @@ ClientUser.setPresence(fullPresence); // status and game combined
``` ```
## Voice ## Voice
Voice has been rewritten internally, but in a backwards-compatible manner. Voice has been rewritten internally, but in a backwards-compatible manner.
There is only one breaking change here; the `disconnected` event was renamed to `disconnect`. There is only one breaking change here; the `disconnected` event was renamed to `disconnect`.
Several more events have been made available to a VoiceConnection, so see the documentation. Several more events have been made available to a VoiceConnection, so see the documentation.
## Events ## Events
Many events have been renamed or had their arguments change. Many events have been renamed or had their arguments change.
### Client events ### Client events
| Version 9 | Version 10 |
|------------------------------------------------------|-----------------------------------------------| | Version 9 | Version 10 |
| guildMemberAdd(guild, member) | guildMemberAdd(member) | | ---------------------------------------------- | --------------------------------------- |
| guildMemberAvailable(guild, member) | guildMemberAvailable(member) | | guildMemberAdd(guild, member) | guildMemberAdd(member) |
| guildMemberRemove(guild, member) | guildMemberRemove(member) | | guildMemberAvailable(guild, member) | guildMemberAvailable(member) |
| guildMembersChunk(guild, members) | guildMembersChunk(members) | | guildMemberRemove(guild, member) | guildMemberRemove(member) |
| guildMemberUpdate(guild, oldMember, newMember) | guildMemberUpdate(oldMember, newMember) | | guildMembersChunk(guild, members) | guildMembersChunk(members) |
| guildRoleCreate(guild, role) | roleCreate(role) | | guildMemberUpdate(guild, oldMember, newMember) | guildMemberUpdate(oldMember, newMember) |
| guildRoleDelete(guild, role) | roleDelete(role) | | guildRoleCreate(guild, role) | roleCreate(role) |
| guildRoleUpdate(guild, oldRole, newRole) | roleUpdate(oldRole, newRole) | | guildRoleDelete(guild, role) | roleDelete(role) |
| guildRoleUpdate(guild, oldRole, newRole) | roleUpdate(oldRole, newRole) |
The guild parameter that has been dropped from the guild-related events can still be derived using `member.guild` or `role.guild`. The guild parameter that has been dropped from the guild-related events can still be derived using `member.guild` or `role.guild`.
### VoiceConnection events ### VoiceConnection events
| Version 9 | Version 10 | | Version 9 | Version 10 |
|--------------|------------| | ------------ | ---------- |
| disconnected | disconnect | | disconnected | disconnect |
## Dates and timestamps ## Dates and timestamps
All dates/timestamps on the structures have been refactored to have a consistent naming scheme and availability. All dates/timestamps on the structures have been refactored to have a consistent naming scheme and availability.
All of them are named similarly to this: All of them are named similarly to this:
**Date:** `Message.createdAt` **Date:** `Message.createdAt`
**Timestamp:** `Message.createdTimestamp` **Timestamp:** `Message.createdTimestamp`
See the docs for each structure to see which date/timestamps are available on them. See the docs for each structure to see which date/timestamps are available on them.
# Version 9 # Version 9
The version 9 (v9) rewrite takes a much more object-oriented approach than previous versions, The version 9 (v9) rewrite takes a much more object-oriented approach than previous versions,
which allows your code to be much more readable and manageable. which allows your code to be much more readable and manageable.
It's been rebuilt from the ground up and should be much more stable, fixing caching issues that affected It's been rebuilt from the ground up and should be much more stable, fixing caching issues that affected
@@ -137,18 +158,21 @@ into other more relevant classes where they belong.
Because of this, you will need to convert most of your calls over to the new methods. Because of this, you will need to convert most of your calls over to the new methods.
Here are a few examples of methods that have changed: Here are a few examples of methods that have changed:
* `Client.sendMessage(channel, message)` ==> `TextChannel.sendMessage(message)`
* `Client.sendMessage(user, message)` ==> `User.sendMessage(message)` - `Client.sendMessage(channel, message)` ==> `TextChannel.sendMessage(message)`
* `Client.updateMessage(message, "New content")` ==> `Message.edit("New Content")` - `Client.sendMessage(user, message)` ==> `User.sendMessage(message)`
* `Client.getChannelLogs(channel, limit)` ==> `TextChannel.fetchMessages({options})` - `Client.updateMessage(message, "New content")` ==> `Message.edit("New Content")`
* `Server.detailsOfUser(User)` ==> `Server.members.get(User).properties` (retrieving a member gives a GuildMember object) - `Client.getChannelLogs(channel, limit)` ==> `TextChannel.fetchMessages({options})`
* `Client.joinVoiceChannel(voicechannel)` => `VoiceChannel.join()` - `Server.detailsOfUser(User)` ==> `Server.members.get(User).properties` (retrieving a member gives a GuildMember object)
- `Client.joinVoiceChannel(voicechannel)` => `VoiceChannel.join()`
A couple more important details: A couple more important details:
* `Client.loginWithToken("token")` ==> `client.login("token")`
* `Client.servers.length` ==> `client.guilds.size` (all instances of `server` are now `guild`) - `Client.loginWithToken("token")` ==> `client.login("token")`
- `Client.servers.length` ==> `client.guilds.size` (all instances of `server` are now `guild`)
## No more callbacks! ## No more callbacks!
Version 9 eschews callbacks in favour of Promises. This means all code relying on callbacks must be changed. Version 9 eschews callbacks in favour of Promises. This means all code relying on callbacks must be changed.
For example, the following code: For example, the following code:
@@ -159,7 +183,7 @@ client.getChannelLogs(channel, 100, function(messages) {
``` ```
```js ```js
channel.fetchMessages({limit: 100}).then(messages => { channel.fetchMessages({ limit: 100 }).then(messages => {
console.log(`${messages.size} messages found`); console.log(`${messages.size} messages found`);
}); });
``` ```

View File

@@ -18,12 +18,14 @@
</div> </div>
# Welcome! # Welcome!
Welcome to the discord.js v12 documentation. Welcome to the discord.js v12 documentation.
v12 is still very much a work-in-progress, as we're aiming to make it the best it can possibly be before releasing. v12 is still very much a work-in-progress, as we're aiming to make it the best it can possibly be before releasing.
Only use it if you are fond of living life on the bleeding edge. Only use it if you are fond of living life on the bleeding edge.
## About ## About
discord.js is a powerful [Node.js](https://nodejs.org) module that allows you to easily interact with the discord.js is a powerful [Node.js](https://nodejs.org) module that allows you to easily interact with the
[Discord API](https://discordapp.com/developers/docs/intro). [Discord API](https://discordapp.com/developers/docs/intro).
@@ -33,6 +35,7 @@ discord.js is a powerful [Node.js](https://nodejs.org) module that allows you to
- 100% coverage of the Discord API - 100% coverage of the Discord API
## Installation ## Installation
**Node.js 11.0.0 or newer is required.** **Node.js 11.0.0 or newer is required.**
Ignore any warnings about unmet peer dependencies, as they're all optional. Ignore any warnings about unmet peer dependencies, as they're all optional.
@@ -41,20 +44,23 @@ With voice support ([@discordjs/opus](https://www.npmjs.com/package/@discordjs/o
With voice support ([opusscript](https://www.npmjs.com/package/opusscript)): `npm install discordjs/discord.js opusscript` With voice support ([opusscript](https://www.npmjs.com/package/opusscript)): `npm install discordjs/discord.js opusscript`
### Audio engines ### Audio engines
The preferred audio engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus. The preferred audio engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus.
Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working. Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working.
For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers. For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers.
### Optional packages ### Optional packages
- [zlib-sync](https://www.npmjs.com/package/zlib-sync) for WebSocket data compression and inflation (`npm install zlib-sync`) - [zlib-sync](https://www.npmjs.com/package/zlib-sync) for WebSocket data compression and inflation (`npm install zlib-sync`)
- [erlpack](https://github.com/discordapp/erlpack) for significantly faster WebSocket data (de)serialisation (`npm install discordapp/erlpack`) - [erlpack](https://github.com/discordapp/erlpack) for significantly faster WebSocket data (de)serialisation (`npm install discordapp/erlpack`)
- One of the following packages can be installed for faster voice packet encryption and decryption: - One of the following packages can be installed for faster voice packet encryption and decryption:
- [sodium](https://www.npmjs.com/package/sodium) (`npm install sodium`) - [sodium](https://www.npmjs.com/package/sodium) (`npm install sodium`)
- [libsodium.js](https://www.npmjs.com/package/libsodium-wrappers) (`npm install libsodium-wrappers`) - [libsodium.js](https://www.npmjs.com/package/libsodium-wrappers) (`npm install libsodium-wrappers`)
- [bufferutil](https://www.npmjs.com/package/bufferutil) for a much faster WebSocket connection (`npm install bufferutil`) - [bufferutil](https://www.npmjs.com/package/bufferutil) for a much faster WebSocket connection (`npm install bufferutil`)
- [utf-8-validate](https://www.npmjs.com/package/utf-8-validate) in combination with `bufferutil` for much faster WebSocket processing (`npm install utf-8-validate`) - [utf-8-validate](https://www.npmjs.com/package/utf-8-validate) in combination with `bufferutil` for much faster WebSocket processing (`npm install utf-8-validate`)
## Example usage ## Example usage
```js ```js
const Discord = require('discord.js'); const Discord = require('discord.js');
const client = new Discord.Client(); const client = new Discord.Client();
@@ -73,24 +79,28 @@ client.login('token');
``` ```
## Links ## Links
* [Website](https://discord.js.org/) ([source](https://github.com/discordjs/website))
* [Documentation](https://discord.js.org/#/docs/main/master/general/welcome) - [Website](https://discord.js.org/) ([source](https://github.com/discordjs/website))
* [Guide](https://discordjs.guide/) ([source](https://github.com/discordjs/guide)) - this is still for stable - [Documentation](https://discord.js.org/#/docs/main/master/general/welcome)
- [Guide](https://discordjs.guide/) ([source](https://github.com/discordjs/guide)) - this is still for stable
See also the WIP [Update Guide](https://discordjs.guide/additional-info/changes-in-v12.html) also including updated and removed items in the library. See also the WIP [Update Guide](https://discordjs.guide/additional-info/changes-in-v12.html) also including updated and removed items in the library.
* [Discord.js Discord server](https://discord.gg/bRCvFy9) - [Discord.js Discord server](https://discord.gg/bRCvFy9)
* [Discord API Discord server](https://discord.gg/discord-api) - [Discord API Discord server](https://discord.gg/discord-api)
* [GitHub](https://github.com/discordjs/discord.js) - [GitHub](https://github.com/discordjs/discord.js)
* [NPM](https://www.npmjs.com/package/discord.js) - [NPM](https://www.npmjs.com/package/discord.js)
* [Related libraries](https://discordapi.com/unofficial/libs.html) - [Related libraries](https://discordapi.com/unofficial/libs.html)
### Extensions ### Extensions
* [RPC](https://www.npmjs.com/package/discord-rpc) ([source](https://github.com/discordjs/RPC))
- [RPC](https://www.npmjs.com/package/discord-rpc) ([source](https://github.com/discordjs/RPC))
## Contributing ## Contributing
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
[documentation](https://discord.js.org/#/docs). [documentation](https://discord.js.org/#/docs).
See [the contribution guide](https://github.com/discordjs/discord.js/blob/master/.github/CONTRIBUTING.md) if you'd like to submit a PR. See [the contribution guide](https://github.com/discordjs/discord.js/blob/master/.github/CONTRIBUTING.md) if you'd like to submit a PR.
## Help ## Help
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle
nudge in the right direction, please don't hesitate to join our official [Discord.js Server](https://discord.gg/bRCvFy9). nudge in the right direction, please don't hesitate to join our official [Discord.js Server](https://discord.gg/bRCvFy9).

View File

@@ -36,7 +36,7 @@ client.on('messageDelete', message => {
if (!message.partial) { if (!message.partial) {
console.log(`It had content: "${message.content}"`); console.log(`It had content: "${message.content}"`);
} }
}) });
// You can also try to upgrade partials to full instances: // You can also try to upgrade partials to full instances:
client.on('messageReactionAdd', async (reaction, user) => { client.on('messageReactionAdd', async (reaction, user) => {

View File

@@ -1,20 +1,23 @@
# Introduction to Voice # Introduction to Voice
Voice in discord.js can be used for many things, such as music bots, recording or relaying audio. Voice in discord.js can be used for many things, such as music bots, recording or relaying audio.
In discord.js, you can use voice by connecting to a `VoiceChannel` to obtain a `VoiceConnection`, where you can start streaming and receiving audio. In discord.js, you can use voice by connecting to a `VoiceChannel` to obtain a `VoiceConnection`, where you can start streaming and receiving audio.
To get started, make sure you have: To get started, make sure you have:
* FFmpeg - `npm install ffmpeg-static`
* an opus encoder, choose one from below: - FFmpeg - `npm install ffmpeg-static`
* `npm install @discordjs/opus` (better performance) - an opus encoder, choose one from below:
* `npm install opusscript` - `npm install @discordjs/opus` (better performance)
* a good network connection - `npm install opusscript`
- a good network connection
The preferred opus engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus. The preferred opus engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus.
Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working. Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working.
For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers. For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers.
## Joining a voice channel ## Joining a voice channel
The example below reacts to a message and joins the sender's voice channel, catching any errors. This is important The example below reacts to a message and joins the sender's voice channel, catching any errors. This is important
as it allows us to obtain a `VoiceConnection` that we can start to stream audio with. as it allows us to obtain a `VoiceConnection` that we can start to stream audio with.
@@ -41,10 +44,12 @@ client.on('message', async message => {
``` ```
## Streaming to a Voice Channel ## Streaming to a Voice Channel
In the previous example, we looked at how to join a voice channel in order to obtain a `VoiceConnection`. Now that we In the previous example, we looked at how to join a voice channel in order to obtain a `VoiceConnection`. Now that we
have obtained a voice connection, we can start streaming audio to it. have obtained a voice connection, we can start streaming audio to it.
### Introduction to playing on voice connections ### Introduction to playing on voice connections
The most basic example of playing audio over a connection would be playing a local file: The most basic example of playing audio over a connection would be playing a local file:
```js ```js
@@ -70,7 +75,7 @@ We can also pass in options when we first play the stream:
```js ```js
const dispatcher = connection.play('/home/discord/audio.mp3', { const dispatcher = connection.play('/home/discord/audio.mp3', {
volume: 0.5 volume: 0.5,
}); });
``` ```
@@ -81,9 +86,7 @@ Discord.js allows you to play a lot of things:
```js ```js
// ReadableStreams, in this example YouTube audio // ReadableStreams, in this example YouTube audio
const ytdl = require('ytdl-core'); const ytdl = require('ytdl-core');
connection.play(ytdl( connection.play(ytdl('https://www.youtube.com/watch?v=ZlAU_w7-Xp8', { filter: 'audioonly' }));
'https://www.youtube.com/watch?v=ZlAU_w7-Xp8',
{ filter: 'audioonly' }));
// Files on the internet // Files on the internet
connection.play('http://www.sample-videos.com/audio/mp3/wave.mp3'); connection.play('http://www.sample-videos.com/audio/mp3/wave.mp3');
@@ -96,11 +99,11 @@ New to v12 is the ability to play OggOpus and WebmOpus streams with much better
```js ```js
connection.play(fs.createReadStream('./media.webm'), { connection.play(fs.createReadStream('./media.webm'), {
type: 'webm/opus' type: 'webm/opus',
}); });
connection.play(fs.createReadStream('./media.ogg'), { connection.play(fs.createReadStream('./media.ogg'), {
type: 'ogg/opus' type: 'ogg/opus',
}); });
``` ```
@@ -119,7 +122,7 @@ broadcast.on('subscribe', dispatcher => {
broadcast.on('unsubscribe', dispatcher => { broadcast.on('unsubscribe', dispatcher => {
console.log('Channel unsubscribed from broadcast :('); console.log('Channel unsubscribed from broadcast :(');
}) });
``` ```
`broadcast` is an instance of `VoiceBroadcast`, which has the same `play` method you are used to with regular VoiceConnections: `broadcast` is an instance of `VoiceBroadcast`, which has the same `play` method you are used to with regular VoiceConnections:
@@ -133,4 +136,5 @@ connection.play(broadcast);
It's important to note that the `dispatcher` stored above is a `BroadcastDispatcher` - it controls all the dispatcher subscribed to the broadcast, e.g. setting the volume of this dispatcher affects the volume of all subscribers. It's important to note that the `dispatcher` stored above is a `BroadcastDispatcher` - it controls all the dispatcher subscribed to the broadcast, e.g. setting the volume of this dispatcher affects the volume of all subscribers.
## Voice Receive ## Voice Receive
coming soon&trade; coming soon&trade;

View File

@@ -1,26 +1,32 @@
# Web builds # Web builds
In addition to your usual Node applications, discord.js has special distributions available that are capable of running in web browsers. In addition to your usual Node applications, discord.js has special distributions available that are capable of running in web browsers.
This is useful for client-side web apps that need to interact with the Discord API. This is useful for client-side web apps that need to interact with the Discord API.
[Webpack 3](https://webpack.js.org/) is used to build these. [Webpack 3](https://webpack.js.org/) is used to build these.
## Restrictions ## Restrictions
- Any voice-related functionality is unavailable, as there is currently no audio encoding/decoding capabilities without external native libraries, - Any voice-related functionality is unavailable, as there is currently no audio encoding/decoding capabilities without external native libraries,
which web browsers do not support. which web browsers do not support.
- The ShardingManager cannot be used, since it relies on being able to spawn child processes for shards. - The ShardingManager cannot be used, since it relies on being able to spawn child processes for shards.
- None of the native optional packages are usable. - None of the native optional packages are usable.
### Require Library ### Require Library
If you are making your own webpack project, you can require `discord.js/browser` wherever you need to use discord.js, like so: If you are making your own webpack project, you can require `discord.js/browser` wherever you need to use discord.js, like so:
```js ```js
const Discord = require('discord.js/browser'); const Discord = require('discord.js/browser');
// do something with Discord like you normally would // do something with Discord like you normally would
``` ```
### Webpack File ### Webpack File
You can obtain your desired version of discord.js' web build from the [webpack branch](https://github.com/discordjs/discord.js/tree/webpack) of the GitHub repository. You can obtain your desired version of discord.js' web build from the [webpack branch](https://github.com/discordjs/discord.js/tree/webpack) of the GitHub repository.
There is a file for each branch and version of the library, and the ones ending in `.min.js` are minified to substantially reduce the size of the source code. There is a file for each branch and version of the library, and the ones ending in `.min.js` are minified to substantially reduce the size of the source code.
Include the file on the page just as you would any other JS library, like so: Include the file on the page just as you would any other JS library, like so:
```html ```html
<script type="text/javascript" src="discord.VERSION.min.js"></script> <script type="text/javascript" src="discord.VERSION.min.js"></script>
``` ```
@@ -29,6 +35,7 @@ Rather than importing discord.js with `require('discord.js')`, the entire `Disco
The usage of the API isn't any different from using it in Node.js. The usage of the API isn't any different from using it in Node.js.
#### Example #### Example
```html ```html
<script type="text/javascript" src="discord.11.1.0.min.js"></script> <script type="text/javascript" src="discord.11.1.0.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">

View File

@@ -1,3 +1,3 @@
{ {
"plugins": ["node_modules/jsdoc-strip-async-await"] "plugins": ["node_modules/jsdoc-strip-async-await"]
} }

View File

@@ -8,9 +8,10 @@
"test": "npm run lint && npm run docs:test && npm run lint:typings", "test": "npm run lint && npm run docs:test && npm run lint:typings",
"docs": "docgen --source src --custom docs/index.yml --output docs/docs.json", "docs": "docgen --source src --custom docs/index.yml --output docs/docs.json",
"docs:test": "docgen --source src --custom docs/index.yml", "docs:test": "docgen --source src --custom docs/index.yml",
"lint": "eslint src *.js", "lint": "eslint src",
"lint:fix": "eslint --fix src", "lint:fix": "eslint src --fix",
"lint:typings": "tslint typings/index.d.ts", "lint:typings": "tslint typings/index.d.ts",
"prettier": "prettier --write --single-quote --print-width 120 --trailing-comma all --end-of-line lf src/**/*.js typings/**/*.ts",
"build:browser": "webpack", "build:browser": "webpack",
"prepublishOnly": "npm run test && NODE_ENV=production npm run build:browser" "prepublishOnly": "npm run test && NODE_ENV=production npm run build:browser"
}, },
@@ -80,9 +81,14 @@
"discord.js-docgen": "discordjs/docgen", "discord.js-docgen": "discordjs/docgen",
"dtslint": "^3.0.0", "dtslint": "^3.0.0",
"eslint": "^6.8.0", "eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-prettier": "^3.1.2",
"husky": "^4.2.3", "husky": "^4.2.3",
"jest": "^25.1.0", "jest": "^25.1.0",
"json-filter-loader": "^1.0.0", "json-filter-loader": "^1.0.0",
"lint-staged": "^10.0.8",
"prettier": "^1.19.1",
"terser-webpack-plugin": "^1.2.2", "terser-webpack-plugin": "^1.2.2",
"tslint": "^6.0.0", "tslint": "^6.0.0",
"typescript": "^3.8.2", "typescript": "^3.8.2",
@@ -126,10 +132,14 @@
}, },
"husky": { "husky": {
"hooks": { "hooks": {
"pre-commit": "npm test", "pre-commit": "lint-staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS" "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
} }
}, },
"lint-staged": {
"*.js": "eslint --fix",
"*.ts": "prettier --write --single-quote --print-width 120 --trailing-comma all --end-of-line lf"
},
"commitlint": { "commitlint": {
"extends": ["@commitlint/config-angular"], "extends": ["@commitlint/config-angular"],
"rules": { "rules": {

View File

@@ -3,8 +3,8 @@
require('setimmediate'); require('setimmediate');
const EventEmitter = require('events'); const EventEmitter = require('events');
const RESTManager = require('../rest/RESTManager'); const RESTManager = require('../rest/RESTManager');
const Util = require('../util/Util');
const { DefaultOptions } = require('../util/Constants'); const { DefaultOptions } = require('../util/Constants');
const Util = require('../util/Util');
/** /**
* The base class for all clients. * The base class for all clients.

View File

@@ -1,24 +1,24 @@
'use strict'; 'use strict';
const BaseClient = require('./BaseClient'); const BaseClient = require('./BaseClient');
const Permissions = require('../util/Permissions'); const ActionsManager = require('./actions/ActionsManager');
const ClientVoiceManager = require('./voice/ClientVoiceManager'); const ClientVoiceManager = require('./voice/ClientVoiceManager');
const WebSocketManager = require('./websocket/WebSocketManager'); const WebSocketManager = require('./websocket/WebSocketManager');
const ActionsManager = require('./actions/ActionsManager'); const { Error, TypeError, RangeError } = require('../errors');
const Collection = require('../util/Collection'); const ChannelManager = require('../managers/ChannelManager');
const GuildEmojiManager = require('../managers/GuildEmojiManager');
const GuildManager = require('../managers/GuildManager');
const UserManager = require('../managers/UserManager');
const ShardClientUtil = require('../sharding/ShardClientUtil');
const ClientApplication = require('../structures/ClientApplication');
const Invite = require('../structures/Invite');
const VoiceRegion = require('../structures/VoiceRegion'); const VoiceRegion = require('../structures/VoiceRegion');
const Webhook = require('../structures/Webhook'); const Webhook = require('../structures/Webhook');
const Invite = require('../structures/Invite'); const Collection = require('../util/Collection');
const ClientApplication = require('../structures/ClientApplication');
const ShardClientUtil = require('../sharding/ShardClientUtil');
const UserManager = require('../managers/UserManager');
const ChannelManager = require('../managers/ChannelManager');
const GuildManager = require('../managers/GuildManager');
const GuildEmojiManager = require('../managers/GuildEmojiManager');
const { Events, browser, DefaultOptions } = require('../util/Constants'); const { Events, browser, DefaultOptions } = require('../util/Constants');
const DataResolver = require('../util/DataResolver'); const DataResolver = require('../util/DataResolver');
const Permissions = require('../util/Permissions');
const Structures = require('../util/Structures'); const Structures = require('../util/Structures');
const { Error, TypeError, RangeError } = require('../errors');
/** /**
* The main hub for interacting with the Discord API, and the starting point for any bot. * The main hub for interacting with the Discord API, and the starting point for any bot.
@@ -63,9 +63,11 @@ class Client extends BaseClient {
if (typeofShards === 'number') this.options.shards = [this.options.shards]; if (typeofShards === 'number') this.options.shards = [this.options.shards];
if (Array.isArray(this.options.shards)) { if (Array.isArray(this.options.shards)) {
this.options.shards = [...new Set( this.options.shards = [
this.options.shards.filter(item => !isNaN(item) && item >= 0 && item < Infinity && item === (item | 0)), ...new Set(
)]; this.options.shards.filter(item => !isNaN(item) && item >= 0 && item < Infinity && item === (item | 0)),
),
];
} }
this._validateOptions(); this._validateOptions();
@@ -93,9 +95,10 @@ class Client extends BaseClient {
* Shard helpers for the client (only if the process was spawned from a {@link ShardingManager}) * Shard helpers for the client (only if the process was spawned from a {@link ShardingManager})
* @type {?ShardClientUtil} * @type {?ShardClientUtil}
*/ */
this.shard = !browser && process.env.SHARDING_MANAGER ? this.shard =
ShardClientUtil.singleton(this, process.env.SHARDING_MANAGER_MODE) : !browser && process.env.SHARDING_MANAGER
null; ? ShardClientUtil.singleton(this, process.env.SHARDING_MANAGER_MODE)
: null;
/** /**
* All of the {@link User} objects that have been cached at any point, mapped by their IDs * All of the {@link User} objects that have been cached at any point, mapped by their IDs
@@ -198,8 +201,12 @@ class Client extends BaseClient {
async login(token = this.token) { async login(token = this.token) {
if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID'); if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID');
this.token = token = token.replace(/^(Bot|Bearer)\s*/i, ''); this.token = token = token.replace(/^(Bot|Bearer)\s*/i, '');
this.emit(Events.DEBUG, this.emit(
`Provided token: ${token.split('.').map((val, i) => i > 1 ? val.replace(/./g, '*') : val).join('.')}`, Events.DEBUG,
`Provided token: ${token
.split('.')
.map((val, i) => (i > 1 ? val.replace(/./g, '*') : val))
.join('.')}`,
); );
if (this.options.presence) { if (this.options.presence) {
@@ -238,7 +245,9 @@ class Client extends BaseClient {
*/ */
fetchInvite(invite) { fetchInvite(invite) {
const code = DataResolver.resolveInviteCode(invite); const code = DataResolver.resolveInviteCode(invite);
return this.api.invites(code).get({ query: { with_counts: true } }) return this.api
.invites(code)
.get({ query: { with_counts: true } })
.then(data => new Invite(this, data)); .then(data => new Invite(this, data));
} }
@@ -253,7 +262,10 @@ class Client extends BaseClient {
* .catch(console.error); * .catch(console.error);
*/ */
fetchWebhook(id, token) { fetchWebhook(id, token) {
return this.api.webhooks(id, token).get().then(data => new Webhook(this, data)); return this.api
.webhooks(id, token)
.get()
.then(data => new Webhook(this, data));
} }
/** /**
@@ -289,7 +301,7 @@ class Client extends BaseClient {
throw new TypeError('INVALID_TYPE', 'lifetime', 'number'); throw new TypeError('INVALID_TYPE', 'lifetime', 'number');
} }
if (lifetime <= 0) { if (lifetime <= 0) {
this.emit(Events.DEBUG, 'Didn\'t sweep messages - lifetime is unlimited'); this.emit(Events.DEBUG, "Didn't sweep messages - lifetime is unlimited");
return -1; return -1;
} }
@@ -307,8 +319,10 @@ class Client extends BaseClient {
); );
} }
this.emit(Events.DEBUG, this.emit(
`Swept ${messages} messages older than ${lifetime} seconds in ${channels} text-based channels`); Events.DEBUG,
`Swept ${messages} messages older than ${lifetime} seconds in ${channels} text-based channels`,
);
return messages; return messages;
} }
@@ -317,7 +331,9 @@ class Client extends BaseClient {
* @returns {Promise<ClientApplication>} * @returns {Promise<ClientApplication>}
*/ */
fetchApplication() { fetchApplication() {
return this.api.oauth2.applications('@me').get() return this.api.oauth2
.applications('@me')
.get()
.then(app => new ClientApplication(this, app)); .then(app => new ClientApplication(this, app));
} }
@@ -364,14 +380,12 @@ class Client extends BaseClient {
* @param {ClientOptions} [options=this.options] Options to validate * @param {ClientOptions} [options=this.options] Options to validate
* @private * @private
*/ */
_validateOptions(options = this.options) { // eslint-disable-line complexity _validateOptions(options = this.options) {
if (typeof options.shardCount !== 'number' || isNaN(options.shardCount) || options.shardCount < 1) { if (typeof options.shardCount !== 'number' || isNaN(options.shardCount) || options.shardCount < 1) {
throw new TypeError('CLIENT_INVALID_OPTION', 'shardCount', 'a number greater than or equal to 1'); throw new TypeError('CLIENT_INVALID_OPTION', 'shardCount', 'a number greater than or equal to 1');
} }
if (options.shards && if (options.shards && !(options.shards === 'auto' || Array.isArray(options.shards))) {
!(options.shards === 'auto' || Array.isArray(options.shards)) throw new TypeError('CLIENT_INVALID_OPTION', 'shards', "'auto', a number or array of numbers");
) {
throw new TypeError('CLIENT_INVALID_OPTION', 'shards', '\'auto\', a number or array of numbers');
} }
if (options.shards && !options.shards.length) throw new RangeError('CLIENT_INVALID_PROVIDED_SHARDS'); if (options.shards && !options.shards.length) throw new RangeError('CLIENT_INVALID_PROVIDED_SHARDS');
if (typeof options.messageCacheMaxSize !== 'number' || isNaN(options.messageCacheMaxSize)) { if (typeof options.messageCacheMaxSize !== 'number' || isNaN(options.messageCacheMaxSize)) {

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const Webhook = require('../structures/Webhook');
const BaseClient = require('./BaseClient'); const BaseClient = require('./BaseClient');
const Webhook = require('../structures/Webhook');
/** /**
* The webhook client. * The webhook client.

View File

@@ -33,38 +33,65 @@ class GenericAction {
getChannel(data) { getChannel(data) {
const id = data.channel_id || data.id; const id = data.channel_id || data.id;
return data.channel || this.getPayload({ return (
id, data.channel ||
guild_id: data.guild_id, this.getPayload(
recipients: [data.author || { id: data.user_id }], {
}, this.client.channels, id, PartialTypes.CHANNEL); id,
guild_id: data.guild_id,
recipients: [data.author || { id: data.user_id }],
},
this.client.channels,
id,
PartialTypes.CHANNEL,
)
);
} }
getMessage(data, channel, cache) { getMessage(data, channel, cache) {
const id = data.message_id || data.id; const id = data.message_id || data.id;
return data.message || this.getPayload({ return (
id, data.message ||
channel_id: channel.id, this.getPayload(
guild_id: data.guild_id || (channel.guild ? channel.guild.id : null), {
}, channel.messages, id, PartialTypes.MESSAGE, cache); id,
channel_id: channel.id,
guild_id: data.guild_id || (channel.guild ? channel.guild.id : null),
},
channel.messages,
id,
PartialTypes.MESSAGE,
cache,
)
);
} }
getReaction(data, message, user) { getReaction(data, message, user) {
const id = data.emoji.id || decodeURIComponent(data.emoji.name); const id = data.emoji.id || decodeURIComponent(data.emoji.name);
return this.getPayload({ return this.getPayload(
emoji: data.emoji, {
count: message.partial ? null : 0, emoji: data.emoji,
me: user ? user.id === this.client.user.id : false, count: message.partial ? null : 0,
}, message.reactions, id, PartialTypes.REACTION); me: user ? user.id === this.client.user.id : false,
},
message.reactions,
id,
PartialTypes.REACTION,
);
} }
getMember(data, guild) { getMember(data, guild) {
const id = data.user.id; const id = data.user.id;
return this.getPayload({ return this.getPayload(
user: { {
id, user: {
id,
},
}, },
}, guild.members, id, PartialTypes.GUILD_MEMBER); guild.members,
id,
PartialTypes.GUILD_MEMBER,
);
} }
getUser(data) { getUser(data) {

View File

@@ -1,8 +1,8 @@
'use strict'; 'use strict';
const Action = require('./Action'); const Action = require('./Action');
const { Events } = require('../../util/Constants');
const DMChannel = require('../../structures/DMChannel'); const DMChannel = require('../../structures/DMChannel');
const { Events } = require('../../util/Constants');
class ChannelDeleteAction extends Action { class ChannelDeleteAction extends Action {
constructor(client) { constructor(client) {

View File

@@ -17,4 +17,3 @@ class GuildIntegrationsUpdate extends Action {
} }
module.exports = GuildIntegrationsUpdate; module.exports = GuildIntegrationsUpdate;

View File

@@ -27,5 +27,4 @@ class GuildMemberRemoveAction extends Action {
} }
} }
module.exports = GuildMemberRemoveAction; module.exports = GuildMemberRemoveAction;

View File

@@ -22,5 +22,4 @@ class GuildRoleCreate extends Action {
} }
} }
module.exports = GuildRoleCreate; module.exports = GuildRoleCreate;

View File

@@ -27,5 +27,4 @@ class GuildRoleDeleteAction extends Action {
} }
} }
module.exports = GuildRoleDeleteAction; module.exports = GuildRoleDeleteAction;

View File

@@ -36,5 +36,4 @@ class GuildRoleUpdateAction extends Action {
} }
} }
module.exports = GuildRoleUpdateAction; module.exports = GuildRoleUpdateAction;

View File

@@ -30,5 +30,4 @@ class GuildUpdateAction extends Action {
} }
} }
module.exports = GuildUpdateAction; module.exports = GuildUpdateAction;

View File

@@ -36,5 +36,4 @@ class MessageCreateAction extends Action {
} }
} }
module.exports = MessageCreateAction; module.exports = MessageCreateAction;

View File

@@ -26,5 +26,4 @@ class MessageDeleteAction extends Action {
} }
} }
module.exports = MessageDeleteAction; module.exports = MessageDeleteAction;

View File

@@ -13,10 +13,14 @@ class MessageDeleteBulkAction extends Action {
const ids = data.ids; const ids = data.ids;
const messages = new Collection(); const messages = new Collection();
for (const id of ids) { for (const id of ids) {
const message = this.getMessage({ const message = this.getMessage(
id, {
guild_id: data.guild_id, id,
}, channel, false); guild_id: data.guild_id,
},
channel,
false,
);
if (message) { if (message) {
message.deleted = true; message.deleted = true;
messages.set(message.id, message); messages.set(message.id, message);
@@ -36,5 +40,4 @@ class MessageDeleteBulkAction extends Action {
} }
} }
module.exports = MessageDeleteBulkAction; module.exports = MessageDeleteBulkAction;

View File

@@ -41,5 +41,4 @@ class MessageReactionRemove extends Action {
} }
} }
module.exports = MessageReactionRemove; module.exports = MessageReactionRemove;

View File

@@ -1,8 +1,8 @@
'use strict'; 'use strict';
const Action = require('./Action'); const Action = require('./Action');
const { Events } = require('../../util/Constants');
const VoiceState = require('../../structures/VoiceState'); const VoiceState = require('../../structures/VoiceState');
const { Events } = require('../../util/Constants');
class VoiceStateUpdate extends Action { class VoiceStateUpdate extends Action {
handle(data) { handle(data) {
@@ -10,9 +10,9 @@ class VoiceStateUpdate extends Action {
const guild = client.guilds.cache.get(data.guild_id); const guild = client.guilds.cache.get(data.guild_id);
if (guild) { if (guild) {
// Update the state // Update the state
const oldState = guild.voiceStates.cache.has(data.user_id) ? const oldState = guild.voiceStates.cache.has(data.user_id)
guild.voiceStates.cache.get(data.user_id)._clone() : ? guild.voiceStates.cache.get(data.user_id)._clone()
new VoiceState(guild, { user_id: data.user_id }); : new VoiceState(guild, { user_id: data.user_id });
const newState = guild.voiceStates.add(data); const newState = guild.voiceStates.add(data);
@@ -41,5 +41,4 @@ class VoiceStateUpdate extends Action {
} }
} }
module.exports = VoiceStateUpdate; module.exports = VoiceStateUpdate;

View File

@@ -16,5 +16,4 @@ class WebhooksUpdate extends Action {
} }
} }
module.exports = WebhooksUpdate; module.exports = WebhooksUpdate;

View File

@@ -1,9 +1,9 @@
'use strict'; 'use strict';
const Collection = require('../../util/Collection');
const VoiceConnection = require('./VoiceConnection');
const VoiceBroadcast = require('./VoiceBroadcast'); const VoiceBroadcast = require('./VoiceBroadcast');
const VoiceConnection = require('./VoiceConnection');
const { Error } = require('../../errors'); const { Error } = require('../../errors');
const Collection = require('../../util/Collection');
/** /**
* Manages voice connections for the client * Manages voice connections for the client
@@ -83,7 +83,8 @@ class ClientVoiceManager {
} else { } else {
connection = new VoiceConnection(this, channel); connection = new VoiceConnection(this, channel);
connection.on('debug', msg => connection.on('debug', msg =>
this.client.emit('debug', `[VOICE (${channel.guild.id}:${connection.status})]: ${msg}`)); this.client.emit('debug', `[VOICE (${channel.guild.id}:${connection.status})]: ${msg}`),
);
connection.authenticate(); connection.authenticate();
this.connections.set(channel.guild.id, connection); this.connections.set(channel.guild.id, connection);
} }

View File

@@ -2,8 +2,8 @@
const EventEmitter = require('events'); const EventEmitter = require('events');
const BroadcastAudioPlayer = require('./player/BroadcastAudioPlayer'); const BroadcastAudioPlayer = require('./player/BroadcastAudioPlayer');
const { Events } = require('../../util/Constants');
const PlayInterface = require('./util/PlayInterface'); const PlayInterface = require('./util/PlayInterface');
const { Events } = require('../../util/Constants');
/** /**
* A voice broadcast can be played across multiple voice connections for improved shared-stream efficiency. * A voice broadcast can be played across multiple voice connections for improved shared-stream efficiency.
@@ -59,8 +59,9 @@ class VoiceBroadcast extends EventEmitter {
* broadcast.play('http://www.sample-videos.com/audio/mp3/wave.mp3'); * broadcast.play('http://www.sample-videos.com/audio/mp3/wave.mp3');
* @returns {BroadcastDispatcher} * @returns {BroadcastDispatcher}
*/ */
play() { return null; } play() {
return null;
}
/** /**
* Ends the broadcast, unsubscribing all subscribed channels and deleting the broadcast * Ends the broadcast, unsubscribing all subscribed channels and deleting the broadcast

View File

@@ -1,16 +1,16 @@
'use strict'; 'use strict';
const VoiceWebSocket = require('./networking/VoiceWebSocket'); const EventEmitter = require('events');
const VoiceUDP = require('./networking/VoiceUDPClient'); const VoiceUDP = require('./networking/VoiceUDPClient');
const Util = require('../../util/Util'); const VoiceWebSocket = require('./networking/VoiceWebSocket');
const { OPCodes, VoiceOPCodes, VoiceStatus, Events } = require('../../util/Constants');
const AudioPlayer = require('./player/AudioPlayer'); const AudioPlayer = require('./player/AudioPlayer');
const VoiceReceiver = require('./receiver/Receiver'); const VoiceReceiver = require('./receiver/Receiver');
const EventEmitter = require('events');
const { Error } = require('../../errors');
const PlayInterface = require('./util/PlayInterface'); const PlayInterface = require('./util/PlayInterface');
const Speaking = require('../../util/Speaking');
const Silence = require('./util/Silence'); const Silence = require('./util/Silence');
const { Error } = require('../../errors');
const { OPCodes, VoiceOPCodes, VoiceStatus, Events } = require('../../util/Constants');
const Speaking = require('../../util/Speaking');
const Util = require('../../util/Util');
// Workaround for Discord now requiring silence to be sent before being able to receive audio // Workaround for Discord now requiring silence to be sent before being able to receive audio
class SingleSilence extends Silence { class SingleSilence extends Silence {
@@ -20,11 +20,7 @@ class SingleSilence extends Silence {
} }
} }
const SUPPORTED_MODES = [ const SUPPORTED_MODES = ['xsalsa20_poly1305_lite', 'xsalsa20_poly1305_suffix', 'xsalsa20_poly1305'];
'xsalsa20_poly1305_lite',
'xsalsa20_poly1305_suffix',
'xsalsa20_poly1305',
];
/** /**
* Represents a connection to a guild's voice server. * Represents a connection to a guild's voice server.
@@ -154,16 +150,18 @@ class VoiceConnection extends EventEmitter {
if (this.speaking.equals(value)) return; if (this.speaking.equals(value)) return;
if (this.status !== VoiceStatus.CONNECTED) return; if (this.status !== VoiceStatus.CONNECTED) return;
this.speaking = new Speaking(value).freeze(); this.speaking = new Speaking(value).freeze();
this.sockets.ws.sendPacket({ this.sockets.ws
op: VoiceOPCodes.SPEAKING, .sendPacket({
d: { op: VoiceOPCodes.SPEAKING,
speaking: this.speaking.bitfield, d: {
delay: 0, speaking: this.speaking.bitfield,
ssrc: this.authentication.ssrc, delay: 0,
}, ssrc: this.authentication.ssrc,
}).catch(e => { },
this.emit('debug', e); })
}); .catch(e => {
this.emit('debug', e);
});
} }
/** /**
@@ -181,19 +179,25 @@ class VoiceConnection extends EventEmitter {
* @private * @private
*/ */
sendVoiceStateUpdate(options = {}) { sendVoiceStateUpdate(options = {}) {
options = Util.mergeDefault({ options = Util.mergeDefault(
guild_id: this.channel.guild.id, {
channel_id: this.channel.id, guild_id: this.channel.guild.id,
self_mute: this.voice ? this.voice.selfMute : false, channel_id: this.channel.id,
self_deaf: this.voice ? this.voice.selfDeaf : false, self_mute: this.voice ? this.voice.selfMute : false,
}, options); self_deaf: this.voice ? this.voice.selfDeaf : false,
},
options,
);
this.emit('debug', `Sending voice state update: ${JSON.stringify(options)}`); this.emit('debug', `Sending voice state update: ${JSON.stringify(options)}`);
return this.channel.guild.shard.send({ return this.channel.guild.shard.send(
op: OPCodes.VOICE_STATE_UPDATE, {
d: options, op: OPCodes.VOICE_STATE_UPDATE,
}, true); d: options,
},
true,
);
} }
/** /**
@@ -318,8 +322,7 @@ class VoiceConnection extends EventEmitter {
*/ */
authenticate() { authenticate() {
this.sendVoiceStateUpdate(); this.sendVoiceStateUpdate();
this.connectTimeout = this.client.setTimeout( this.connectTimeout = this.client.setTimeout(() => this.authenticateFailed('VOICE_CONNECTION_TIMEOUT'), 15000);
() => this.authenticateFailed('VOICE_CONNECTION_TIMEOUT'), 15000);
} }
/** /**
@@ -371,7 +374,6 @@ class VoiceConnection extends EventEmitter {
this.emit('disconnect'); this.emit('disconnect');
} }
/** /**
* Cleans up after disconnect. * Cleans up after disconnect.
* @private * @private
@@ -513,7 +515,7 @@ class VoiceConnection extends EventEmitter {
} }
} }
play() { } // eslint-disable-line no-empty-function play() {} // eslint-disable-line no-empty-function
} }
PlayInterface.applyToClass(VoiceConnection); PlayInterface.applyToClass(VoiceConnection);

View File

@@ -1,16 +1,15 @@
'use strict'; 'use strict';
const VolumeInterface = require('../util/VolumeInterface');
const { Writable } = require('stream'); const { Writable } = require('stream');
const secretbox = require('../util/Secretbox'); const secretbox = require('../util/Secretbox');
const Silence = require('../util/Silence'); const Silence = require('../util/Silence');
const VolumeInterface = require('../util/VolumeInterface');
const FRAME_LENGTH = 20; const FRAME_LENGTH = 20;
const CHANNELS = 2; const CHANNELS = 2;
const TIMESTAMP_INC = (48000 / 100) * CHANNELS; const TIMESTAMP_INC = (48000 / 100) * CHANNELS;
const MAX_NONCE_SIZE = (2 ** 32) - 1; const MAX_NONCE_SIZE = 2 ** 32 - 1;
const nonce = Buffer.alloc(24); const nonce = Buffer.alloc(24);
/** /**
@@ -31,10 +30,7 @@ const nonce = Buffer.alloc(24);
* @extends {WritableStream} * @extends {WritableStream}
*/ */
class StreamDispatcher extends Writable { class StreamDispatcher extends Writable {
constructor( constructor(player, { seek = 0, volume = 1, fec, plp, bitrate = 96, highWaterMark = 12 } = {}, streams) {
player,
{ seek = 0, volume = 1, fec, plp, bitrate = 96, highWaterMark = 12 } = {},
streams) {
const streamOptions = { seek, volume, fec, plp, bitrate, highWaterMark }; const streamOptions = { seek, volume, fec, plp, bitrate, highWaterMark };
super(streamOptions); super(streamOptions);
/** /**
@@ -146,7 +142,9 @@ class StreamDispatcher extends Writable {
* @type {boolean} * @type {boolean}
* @readonly * @readonly
*/ */
get paused() { return Boolean(this.pausedSince); } get paused() {
return Boolean(this.pausedSince);
}
/** /**
* Total time that this dispatcher has been paused in milliseconds * Total time that this dispatcher has been paused in milliseconds
@@ -233,7 +231,7 @@ class StreamDispatcher extends Writable {
done(); done();
}; };
if (!this.streams.broadcast) { if (!this.streams.broadcast) {
const next = FRAME_LENGTH + (this.count * FRAME_LENGTH) - (Date.now() - this.startTime - this._pausedTime); const next = FRAME_LENGTH + this.count * FRAME_LENGTH - (Date.now() - this.startTime - this._pausedTime);
setTimeout(() => { setTimeout(() => {
if ((!this.pausedSince || this._silence) && this._writeCallback) this._writeCallback(); if ((!this.pausedSince || this._silence) && this._writeCallback) this._writeCallback();
}, next); }, next);
@@ -261,10 +259,7 @@ class StreamDispatcher extends Writable {
this._nonce++; this._nonce++;
if (this._nonce > MAX_NONCE_SIZE) this._nonce = 0; if (this._nonce > MAX_NONCE_SIZE) this._nonce = 0;
this._nonceBuffer.writeUInt32BE(this._nonce, 0); this._nonceBuffer.writeUInt32BE(this._nonce, 0);
return [ return [secretbox.methods.close(buffer, this._nonceBuffer, secret_key), this._nonceBuffer.slice(0, 4)];
secretbox.methods.close(buffer, this._nonceBuffer, secret_key),
this._nonceBuffer.slice(0, 4),
];
} else if (mode === 'xsalsa20_poly1305_suffix') { } else if (mode === 'xsalsa20_poly1305_suffix') {
const random = secretbox.methods.random(24); const random = secretbox.methods.random(24);
return [secretbox.methods.close(buffer, random, secret_key), random]; return [secretbox.methods.close(buffer, random, secret_key), random];
@@ -297,11 +292,10 @@ class StreamDispatcher extends Writable {
this.emit('debug', 'Failed to send a packet - no UDP socket'); this.emit('debug', 'Failed to send a packet - no UDP socket');
return; return;
} }
this.player.voiceConnection.sockets.udp.send(packet) this.player.voiceConnection.sockets.udp.send(packet).catch(e => {
.catch(e => { this._setSpeaking(0);
this._setSpeaking(0); this.emit('debug', `Failed to send a packet - ${e}`);
this.emit('debug', `Failed to send a packet - ${e}`); });
});
} }
_setSpeaking(value) { _setSpeaking(value) {
@@ -316,14 +310,18 @@ class StreamDispatcher extends Writable {
this.emit('speaking', value); this.emit('speaking', value);
} }
get volumeEditable() { return Boolean(this.streams.volume); } get volumeEditable() {
return Boolean(this.streams.volume);
}
/** /**
* Whether or not the Opus bitrate of this stream is editable * Whether or not the Opus bitrate of this stream is editable
* @type {boolean} * @type {boolean}
* @readonly * @readonly
*/ */
get bitrateEditable() { return this.streams.opus && this.streams.opus.setBitrate; } get bitrateEditable() {
return this.streams.opus && this.streams.opus.setBitrate;
}
// Volume // Volume
get volume() { get volume() {

View File

@@ -1,9 +1,9 @@
'use strict'; 'use strict';
const udp = require('dgram'); const udp = require('dgram');
const { VoiceOPCodes } = require('../../../util/Constants');
const EventEmitter = require('events'); const EventEmitter = require('events');
const { Error } = require('../../../errors'); const { Error } = require('../../../errors');
const { VoiceOPCodes } = require('../../../util/Constants');
/** /**
* Represents a UDP client for a Voice Connection. * Represents a UDP client for a Voice Connection.
@@ -90,7 +90,7 @@ class VoiceConnectionUDPClient extends EventEmitter {
async createUDPSocket(address) { async createUDPSocket(address) {
this.discordAddress = address; this.discordAddress = address;
const socket = this.socket = udp.createSocket('udp4'); const socket = (this.socket = udp.createSocket('udp4'));
socket.on('error', e => { socket.on('error', e => {
this.emit('debug', `[UDP] Error: ${e}`); this.emit('debug', `[UDP] Error: ${e}`);
this.emit('error', e); this.emit('error', e);

View File

@@ -1,9 +1,9 @@
'use strict'; 'use strict';
const { OPCodes, VoiceOPCodes } = require('../../../util/Constants');
const EventEmitter = require('events'); const EventEmitter = require('events');
const { Error } = require('../../../errors');
const WebSocket = require('../../../WebSocket'); const WebSocket = require('../../../WebSocket');
const { Error } = require('../../../errors');
const { OPCodes, VoiceOPCodes } = require('../../../util/Constants');
/** /**
* Represents a Voice Connection's WebSocket. * Represents a Voice Connection's WebSocket.
@@ -92,7 +92,8 @@ class VoiceWebSocket extends EventEmitter {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) throw new Error('WS_NOT_OPEN', data); if (!this.ws || this.ws.readyState !== WebSocket.OPEN) throw new Error('WS_NOT_OPEN', data);
this.ws.send(data, null, error => { this.ws.send(data, null, error => {
if (error) reject(error); else resolve(data); if (error) reject(error);
else resolve(data);
}); });
}); });
} }

View File

@@ -1,17 +1,11 @@
'use strict'; 'use strict';
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events');
const { Readable: ReadableStream } = require('stream'); const { Readable: ReadableStream } = require('stream');
const prism = require('prism-media'); const prism = require('prism-media');
const StreamDispatcher = require('../dispatcher/StreamDispatcher'); const StreamDispatcher = require('../dispatcher/StreamDispatcher');
const FFMPEG_ARGUMENTS = [ const FFMPEG_ARGUMENTS = ['-analyzeduration', '0', '-loglevel', '0', '-f', 's16le', '-ar', '48000', '-ac', '2'];
'-analyzeduration', '0',
'-loglevel', '0',
'-f', 's16le',
'-ar', '48000',
'-ac', '2',
];
/** /**
* An Audio Player for a Voice Connection. * An Audio Player for a Voice Connection.
@@ -61,7 +55,7 @@ class BasePlayer extends EventEmitter {
playPCMStream(stream, options, streams = {}) { playPCMStream(stream, options, streams = {}) {
this.destroyDispatcher(); this.destroyDispatcher();
const opus = streams.opus = new prism.opus.Encoder({ channels: 2, rate: 48000, frameSize: 960 }); const opus = (streams.opus = new prism.opus.Encoder({ channels: 2, rate: 48000, frameSize: 960 }));
if (options && options.volume === false) { if (options && options.volume === false) {
stream.pipe(opus); stream.pipe(opus);
return this.playOpusStream(opus, options, streams); return this.playOpusStream(opus, options, streams);
@@ -90,7 +84,7 @@ class BasePlayer extends EventEmitter {
createDispatcher(options, streams, broadcast) { createDispatcher(options, streams, broadcast) {
this.destroyDispatcher(); this.destroyDispatcher();
const dispatcher = this.dispatcher = new StreamDispatcher(this, options, streams, broadcast); const dispatcher = (this.dispatcher = new StreamDispatcher(this, options, streams, broadcast));
return dispatcher; return dispatcher;
} }
} }

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const BroadcastDispatcher = require('../dispatcher/BroadcastDispatcher');
const BasePlayer = require('./BasePlayer'); const BasePlayer = require('./BasePlayer');
const BroadcastDispatcher = require('../dispatcher/BroadcastDispatcher');
/** /**
* An Audio Player for a Voice Connection. * An Audio Player for a Voice Connection.
@@ -20,7 +20,7 @@ class AudioPlayer extends BasePlayer {
createDispatcher(options, streams) { createDispatcher(options, streams) {
this.destroyDispatcher(); this.destroyDispatcher();
const dispatcher = this.dispatcher = new BroadcastDispatcher(this, options, streams); const dispatcher = (this.dispatcher = new BroadcastDispatcher(this, options, streams));
return dispatcher; return dispatcher;
} }
} }

View File

@@ -1,13 +1,15 @@
'use strict'; 'use strict';
const secretbox = require('../util/Secretbox');
const EventEmitter = require('events'); const EventEmitter = require('events');
const secretbox = require('../util/Secretbox');
// The delay between packets when a user is considered to have stopped speaking // The delay between packets when a user is considered to have stopped speaking
// https://github.com/discordjs/discord.js/issues/3524#issuecomment-540373200 // https://github.com/discordjs/discord.js/issues/3524#issuecomment-540373200
const DISCORD_SPEAKING_DELAY = 250; const DISCORD_SPEAKING_DELAY = 250;
class Readable extends require('stream').Readable { _read() {} } // eslint-disable-line no-empty-function class Readable extends require('stream').Readable {
_read() {} // eslint-disable-line no-empty-function
}
class PacketHandler extends EventEmitter { class PacketHandler extends EventEmitter {
constructor(receiver) { constructor(receiver) {
@@ -59,7 +61,7 @@ class PacketHandler extends EventEmitter {
packet = Buffer.from(packet); packet = Buffer.from(packet);
// Strip RTP Header Extensions (one-byte only) // Strip RTP Header Extensions (one-byte only)
if (packet[0] === 0xBE && packet[1] === 0xDE && packet.length > 4) { if (packet[0] === 0xbe && packet[1] === 0xde && packet.length > 4) {
const headerExtensionLength = packet.readUInt16BE(2); const headerExtensionLength = packet.readUInt16BE(2);
let offset = 4; let offset = 4;
for (let i = 0; i < headerExtensionLength; i++) { for (let i = 0; i < headerExtensionLength; i++) {

View File

@@ -85,12 +85,12 @@ class PlayInterface {
static applyToClass(structure) { static applyToClass(structure) {
for (const prop of ['play']) { for (const prop of ['play']) {
Object.defineProperty(structure.prototype, prop, Object.defineProperty(structure.prototype, prop, Object.getOwnPropertyDescriptor(PlayInterface.prototype, prop));
Object.getOwnPropertyDescriptor(PlayInterface.prototype, prop));
} }
} }
} }
module.exports = PlayInterface; module.exports = PlayInterface;
// eslint-disable-next-line import/order
const Broadcast = require('../VoiceBroadcast'); const Broadcast = require('../VoiceBroadcast');

View File

@@ -2,7 +2,7 @@
const { Readable } = require('stream'); const { Readable } = require('stream');
const SILENCE_FRAME = Buffer.from([0xF8, 0xFF, 0xFE]); const SILENCE_FRAME = Buffer.from([0xf8, 0xff, 0xfe]);
class Silence extends Readable { class Silence extends Readable {
_read() { _read() {

View File

@@ -94,19 +94,10 @@ class VolumeInterface extends EventEmitter {
} }
} }
const props = [ const props = ['volumeDecibels', 'volumeLogarithmic', 'setVolumeDecibels', 'setVolumeLogarithmic'];
'volumeDecibels',
'volumeLogarithmic',
'setVolumeDecibels',
'setVolumeLogarithmic',
];
exports.applyToClass = function applyToClass(structure) { exports.applyToClass = function applyToClass(structure) {
for (const prop of props) { for (const prop of props) {
Object.defineProperty( Object.defineProperty(structure.prototype, prop, Object.getOwnPropertyDescriptor(VolumeInterface.prototype, prop));
structure.prototype,
prop,
Object.getOwnPropertyDescriptor(VolumeInterface.prototype, prop),
);
} }
}; };

View File

@@ -1,12 +1,12 @@
'use strict'; 'use strict';
const EventEmitter = require('events'); const EventEmitter = require('events');
const WebSocketShard = require('./WebSocketShard');
const PacketHandlers = require('./handlers');
const { Error: DJSError } = require('../../errors'); const { Error: DJSError } = require('../../errors');
const Collection = require('../../util/Collection'); const Collection = require('../../util/Collection');
const Util = require('../../util/Util');
const WebSocketShard = require('./WebSocketShard');
const { Events, ShardEvents, Status, WSCodes, WSEvents } = require('../../util/Constants'); const { Events, ShardEvents, Status, WSCodes, WSEvents } = require('../../util/Constants');
const PacketHandlers = require('./handlers'); const Util = require('../../util/Util');
const BeforeReadyWhitelist = [ const BeforeReadyWhitelist = [
WSEvents.READY, WSEvents.READY,
@@ -18,7 +18,9 @@ const BeforeReadyWhitelist = [
WSEvents.GUILD_MEMBER_REMOVE, WSEvents.GUILD_MEMBER_REMOVE,
]; ];
const UNRECOVERABLE_CLOSE_CODES = Object.keys(WSCodes).slice(1).map(Number); const UNRECOVERABLE_CLOSE_CODES = Object.keys(WSCodes)
.slice(1)
.map(Number);
const UNRESUMABLE_CLOSE_CODES = [1000, 4006, 4007]; const UNRESUMABLE_CLOSE_CODES = [1000, 4006, 4007];
/** /**

View File

@@ -237,14 +237,15 @@ class WebSocketShard extends EventEmitter {
Gateway : ${gateway} Gateway : ${gateway}
Version : ${client.options.ws.version} Version : ${client.options.ws.version}
Encoding : ${WebSocket.encoding} Encoding : ${WebSocket.encoding}
Compression: ${zlib ? 'zlib-stream' : 'none'}`); Compression: ${zlib ? 'zlib-stream' : 'none'}`,
);
this.status = this.status === Status.DISCONNECTED ? Status.RECONNECTING : Status.CONNECTING; this.status = this.status === Status.DISCONNECTED ? Status.RECONNECTING : Status.CONNECTING;
this.setHelloTimeout(); this.setHelloTimeout();
this.connectedAt = Date.now(); this.connectedAt = Date.now();
const ws = this.connection = WebSocket.create(gateway, wsQuery); const ws = (this.connection = WebSocket.create(gateway, wsQuery));
ws.onopen = this.onOpen.bind(this); ws.onopen = this.onOpen.bind(this);
ws.onmessage = this.onMessage.bind(this); ws.onmessage = this.onMessage.bind(this);
ws.onerror = this.onError.bind(this); ws.onerror = this.onError.bind(this);
@@ -271,11 +272,8 @@ class WebSocketShard extends EventEmitter {
if (data instanceof ArrayBuffer) data = new Uint8Array(data); if (data instanceof ArrayBuffer) data = new Uint8Array(data);
if (zlib) { if (zlib) {
const l = data.length; const l = data.length;
const flush = l >= 4 && const flush =
data[l - 4] === 0x00 && l >= 4 && data[l - 4] === 0x00 && data[l - 3] === 0x00 && data[l - 2] === 0xff && data[l - 1] === 0xff;
data[l - 3] === 0x00 &&
data[l - 2] === 0xFF &&
data[l - 1] === 0xFF;
this.inflate.push(data, flush && zlib.Z_SYNC_FLUSH); this.inflate.push(data, flush && zlib.Z_SYNC_FLUSH);
if (!flush) return; if (!flush) return;
@@ -529,8 +527,10 @@ class WebSocketShard extends EventEmitter {
* @param {boolean} [ignoreHeartbeatAck] If we should send the heartbeat forcefully. * @param {boolean} [ignoreHeartbeatAck] If we should send the heartbeat forcefully.
* @private * @private
*/ */
sendHeartbeat(tag = 'HeartbeatTimer', sendHeartbeat(
ignoreHeartbeatAck = [Status.WAITING_FOR_GUILDS, Status.IDENTIFYING, Status.RESUMING].includes(this.status)) { tag = 'HeartbeatTimer',
ignoreHeartbeatAck = [Status.WAITING_FOR_GUILDS, Status.IDENTIFYING, Status.RESUMING].includes(this.status),
) {
if (ignoreHeartbeatAck && !this.lastHeartbeatAcked) { if (ignoreHeartbeatAck && !this.lastHeartbeatAcked) {
this.debug(`[${tag}] Didn't process heartbeat ack yet but we are still connected. Sending one now.`); this.debug(`[${tag}] Didn't process heartbeat ack yet but we are still connected. Sending one now.`);
} else if (!this.lastHeartbeatAcked) { } else if (!this.lastHeartbeatAcked) {
@@ -538,7 +538,8 @@ class WebSocketShard extends EventEmitter {
`[${tag}] Didn't receive a heartbeat ack last time, assuming zombie connection. Destroying and reconnecting. `[${tag}] Didn't receive a heartbeat ack last time, assuming zombie connection. Destroying and reconnecting.
Status : ${STATUS_KEYS[this.status]} Status : ${STATUS_KEYS[this.status]}
Sequence : ${this.sequence} Sequence : ${this.sequence}
Connection State: ${this.connection ? CONNECTION_STATE[this.connection.readyState] : 'No Connection??'}`); Connection State: ${this.connection ? CONNECTION_STATE[this.connection.readyState] : 'No Connection??'}`,
);
this.destroy({ closeCode: 4009, reset: true }); this.destroy({ closeCode: 4009, reset: true });
return; return;
@@ -741,10 +742,7 @@ class WebSocketShard extends EventEmitter {
* @private * @private
*/ */
_cleanupConnection() { _cleanupConnection() {
this.connection.onopen = this.connection.onopen = this.connection.onclose = this.connection.onerror = this.connection.onmessage = null;
this.connection.onclose =
this.connection.onerror =
this.connection.onmessage = null;
} }
/** /**

View File

@@ -14,4 +14,3 @@ module.exports = (client, packet) => {
client.emit(Events.CHANNEL_UPDATE, old, updated); client.emit(Events.CHANNEL_UPDATE, old, updated);
} }
}; };

View File

@@ -10,9 +10,9 @@ module.exports = async (client, { d: data }, shard) => {
guild._patch(data); guild._patch(data);
// If the client was ready before and we had unavailable guilds, fetch them // If the client was ready before and we had unavailable guilds, fetch them
if (client.ws.status === Status.READY && client.options.fetchAllMembers) { if (client.ws.status === Status.READY && client.options.fetchAllMembers) {
await guild.members.fetch().catch(err => await guild.members
client.emit(Events.DEBUG, `Failed to fetch all members: ${err}\n${err.stack}`), .fetch()
); .catch(err => client.emit(Events.DEBUG, `Failed to fetch all members: ${err}\n${err.stack}`));
} }
} }
} else { } else {
@@ -26,9 +26,9 @@ module.exports = async (client, { d: data }, shard) => {
* @param {Guild} guild The created guild * @param {Guild} guild The created guild
*/ */
if (client.options.fetchAllMembers) { if (client.options.fetchAllMembers) {
await guild.members.fetch().catch(err => await guild.members
client.emit(Events.DEBUG, `Failed to fetch all members: ${err}\n${err.stack}`), .fetch()
); .catch(err => client.emit(Events.DEBUG, `Failed to fetch all members: ${err}\n${err.stack}`));
} }
client.emit(Events.GUILD_CREATE, guild); client.emit(Events.GUILD_CREATE, guild);
} }

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const { Events } = require('../../../util/Constants');
const Collection = require('../../../util/Collection'); const Collection = require('../../../util/Collection');
const { Events } = require('../../../util/Constants');
module.exports = (client, { d: data }) => { module.exports = (client, { d: data }) => {
const guild = client.guilds.cache.get(data.guild_id); const guild = client.guilds.cache.get(data.guild_id);

View File

@@ -8,11 +8,11 @@ module.exports = (client, { d: data }, shard) => {
guild.memberCount++; guild.memberCount++;
const member = guild.members.add(data); const member = guild.members.add(data);
if (shard.status === Status.READY) { if (shard.status === Status.READY) {
/** /**
* Emitted whenever a user joins a guild. * Emitted whenever a user joins a guild.
* @event Client#guildMemberAdd * @event Client#guildMemberAdd
* @param {GuildMember} member The member that has joined a guild * @param {GuildMember} member The member that has joined a guild
*/ */
client.emit(Events.GUILD_MEMBER_ADD, member); client.emit(Events.GUILD_MEMBER_ADD, member);
} }
} }

View File

@@ -7,12 +7,12 @@ module.exports = (client, { d: data }) => {
const user = client.users.cache.get(data.user_id); const user = client.users.cache.get(data.user_id);
if (channel && user) { if (channel && user) {
/** /**
* Emitted whenever a user starts typing in a channel. * Emitted whenever a user starts typing in a channel.
* @event Client#typingStart * @event Client#typingStart
* @param {Channel} channel The channel the user started typing in * @param {Channel} channel The channel the user started typing in
* @param {User} user The user that started typing * @param {User} user The user that started typing
*/ */
client.emit(Events.TYPING_START, channel, user); client.emit(Events.TYPING_START, channel, user);
} }
}; };

View File

@@ -36,7 +36,7 @@ const Messages = {
USER_NO_DMCHANNEL: 'No DM Channel exists!', USER_NO_DMCHANNEL: 'No DM Channel exists!',
VOICE_INVALID_HEARTBEAT: 'Tried to set voice heartbeat but no valid interval was specified.', VOICE_INVALID_HEARTBEAT: 'Tried to set voice heartbeat but no valid interval was specified.',
VOICE_USER_MISSING: 'Couldn\'t resolve the user to create stream.', VOICE_USER_MISSING: "Couldn't resolve the user to create stream.",
VOICE_JOIN_CHANNEL: (full = false) => VOICE_JOIN_CHANNEL: (full = false) =>
`You do not have permission to join this voice channel${full ? '; it is full.' : '.'}`, `You do not have permission to join this voice channel${full ? '; it is full.' : '.'}`,
VOICE_CONNECTION_TIMEOUT: 'Connection not established within 15 seconds.', VOICE_CONNECTION_TIMEOUT: 'Connection not established within 15 seconds.',
@@ -71,7 +71,7 @@ const Messages = {
SPLIT_MAX_LEN: 'Chunk exceeds the max length and contains no split characters.', SPLIT_MAX_LEN: 'Chunk exceeds the max length and contains no split characters.',
BAN_RESOLVE_ID: (ban = false) => `Couldn't resolve the user ID to ${ban ? 'ban' : 'unban'}.`, BAN_RESOLVE_ID: (ban = false) => `Couldn't resolve the user ID to ${ban ? 'ban' : 'unban'}.`,
FETCH_BAN_RESOLVE_ID: 'Couldn\'t resolve the user ID to fetch the ban.', FETCH_BAN_RESOLVE_ID: "Couldn't resolve the user ID to fetch the ban.",
PRUNE_DAYS_TYPE: 'Days must be a number', PRUNE_DAYS_TYPE: 'Days must be a number',
@@ -79,7 +79,7 @@ const Messages = {
GUILD_VOICE_CHANNEL_RESOLVE: 'Could not resolve channel to a guild voice channel.', GUILD_VOICE_CHANNEL_RESOLVE: 'Could not resolve channel to a guild voice channel.',
GUILD_CHANNEL_ORPHAN: 'Could not find a parent to this guild channel.', GUILD_CHANNEL_ORPHAN: 'Could not find a parent to this guild channel.',
GUILD_OWNED: 'Guild is owned by the client.', GUILD_OWNED: 'Guild is owned by the client.',
GUILD_MEMBERS_TIMEOUT: 'Members didn\'t arrive in time.', GUILD_MEMBERS_TIMEOUT: "Members didn't arrive in time.",
GUILD_UNCACHED_ME: 'The client user as a member of this guild is uncached.', GUILD_UNCACHED_ME: 'The client user as a member of this guild is uncached.',
INVALID_TYPE: (name, expected, an = false) => `Supplied ${name} is not a${an ? 'n' : ''} ${expected}.`, INVALID_TYPE: (name, expected, an = false) => `Supplied ${name} is not a${an ? 'n' : ''} ${expected}.`,
@@ -88,15 +88,15 @@ const Messages = {
EMOJI_TYPE: 'Emoji must be a string or GuildEmoji/ReactionEmoji', EMOJI_TYPE: 'Emoji must be a string or GuildEmoji/ReactionEmoji',
EMOJI_MANAGED: 'Emoji is managed and has no Author.', EMOJI_MANAGED: 'Emoji is managed and has no Author.',
MISSING_MANAGE_EMOJIS_PERMISSION: MISSING_MANAGE_EMOJIS_PERMISSION: guild =>
guild => `Client must have Manage Emoji permission in guild ${guild} to see emoji authors.`, `Client must have Manage Emoji permission in guild ${guild} to see emoji authors.`,
REACTION_RESOLVE_USER: 'Couldn\'t resolve the user ID to remove from the reaction.', REACTION_RESOLVE_USER: "Couldn't resolve the user ID to remove from the reaction.",
VANITY_URL: 'This guild does not have the VANITY_URL feature enabled.', VANITY_URL: 'This guild does not have the VANITY_URL feature enabled.',
DELETE_GROUP_DM_CHANNEL: 'Bots don\'t have access to Group DM Channels and cannot delete them', DELETE_GROUP_DM_CHANNEL: "Bots don't have access to Group DM Channels and cannot delete them",
FETCH_GROUP_DM_CHANNEL: 'Bots don\'t have access to Group DM Channels and cannot fetch them', FETCH_GROUP_DM_CHANNEL: "Bots don't have access to Group DM Channels and cannot fetch them",
}; };
for (const [name, message] of Object.entries(Messages)) register(name, message); for (const [name, message] of Object.entries(Messages)) register(name, message);

View File

@@ -34,9 +34,9 @@ class BaseManager {
this.cacheType = cacheType; this.cacheType = cacheType;
/** /**
* Holds the cache for the data model * Holds the cache for the data model
* @type {Collection} * @type {Collection}
*/ */
this.cache = new cacheType(...cacheOptions); this.cache = new cacheType(...cacheOptions);
if (iterable) for (const i of iterable) this.add(i); if (iterable) for (const i of iterable) this.add(i);
} }

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const Channel = require('../structures/Channel');
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const Channel = require('../structures/Channel');
const { Events } = require('../util/Constants'); const { Events } = require('../util/Constants');
/** /**
@@ -14,10 +14,10 @@ class ChannelManager extends BaseManager {
} }
/** /**
* The cache of Channels * The cache of Channels
* @type {Collection<Snowflake, Channel>} * @type {Collection<Snowflake, Channel>}
* @name ChannelManager#cache * @name ChannelManager#cache
*/ */
add(data, guild, cache = true) { add(data, guild, cache = true) {
const existing = this.cache.get(data.id); const existing = this.cache.get(data.id);

View File

@@ -1,9 +1,9 @@
'use strict'; 'use strict';
const { ChannelTypes } = require('../util/Constants');
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const GuildChannel = require('../structures/GuildChannel'); const GuildChannel = require('../structures/GuildChannel');
const PermissionOverwrites = require('../structures/PermissionOverwrites'); const PermissionOverwrites = require('../structures/PermissionOverwrites');
const { ChannelTypes } = require('../util/Constants');
/** /**
* Manages API methods for GuildChannels and stores their cache. * Manages API methods for GuildChannels and stores their cache.

View File

@@ -1,11 +1,11 @@
'use strict'; 'use strict';
const Collection = require('../util/Collection');
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const { TypeError } = require('../errors');
const GuildEmoji = require('../structures/GuildEmoji'); const GuildEmoji = require('../structures/GuildEmoji');
const ReactionEmoji = require('../structures/ReactionEmoji'); const ReactionEmoji = require('../structures/ReactionEmoji');
const Collection = require('../util/Collection');
const DataResolver = require('../util/DataResolver'); const DataResolver = require('../util/DataResolver');
const { TypeError } = require('../errors');
/** /**
* Manages API methods for GuildEmojis and stores their cache. * Manages API methods for GuildEmojis and stores their cache.
@@ -22,10 +22,10 @@ class GuildEmojiManager extends BaseManager {
} }
/** /**
* The cache of GuildEmojis * The cache of GuildEmojis
* @type {Collection<Snowflake, GuildEmoji>} * @type {Collection<Snowflake, GuildEmoji>}
* @name GuildEmojiManager#cache * @name GuildEmojiManager#cache
*/ */
add(data, cache) { add(data, cache) {
return super.add(data, cache, { extras: [this.guild] }); return super.add(data, cache, { extras: [this.guild] });
@@ -58,14 +58,17 @@ class GuildEmojiManager extends BaseManager {
for (let role of roles instanceof Collection ? roles.values() : roles) { for (let role of roles instanceof Collection ? roles.values() : roles) {
role = this.guild.roles.resolve(role); role = this.guild.roles.resolve(role);
if (!role) { if (!role) {
return Promise.reject(new TypeError('INVALID_TYPE', 'options.roles', return Promise.reject(
'Array or Collection of Roles or Snowflakes', true)); new TypeError('INVALID_TYPE', 'options.roles', 'Array or Collection of Roles or Snowflakes', true),
);
} }
data.roles.push(role.id); data.roles.push(role.id);
} }
} }
return this.client.api.guilds(this.guild.id).emojis.post({ data, reason }) return this.client.api
.guilds(this.guild.id)
.emojis.post({ data, reason })
.then(emoji => this.client.actions.GuildEmojiCreate.handle(this.guild, emoji).emoji); .then(emoji => this.client.actions.GuildEmojiCreate.handle(this.guild, emoji).emoji);
} }

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const Collection = require('../util/Collection');
const { TypeError } = require('../errors'); const { TypeError } = require('../errors');
const Collection = require('../util/Collection');
/** /**
* Manages API methods for roles belonging to emojis and stores their cache. * Manages API methods for roles belonging to emojis and stores their cache.
@@ -56,8 +56,7 @@ class GuildEmojiRoleManager {
roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolve(r)); roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolve(r));
if (roleOrRoles.includes(null)) { if (roleOrRoles.includes(null)) {
return Promise.reject(new TypeError('INVALID_TYPE', 'roles', return Promise.reject(new TypeError('INVALID_TYPE', 'roles', 'Array or Collection of Roles or Snowflakes', true));
'Array or Collection of Roles or Snowflakes', true));
} }
const newRoles = [...new Set(roleOrRoles.concat(...this._roles.values()))]; const newRoles = [...new Set(roleOrRoles.concat(...this._roles.values()))];
@@ -75,8 +74,7 @@ class GuildEmojiRoleManager {
roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolveID(r)); roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolveID(r));
if (roleOrRoles.includes(null)) { if (roleOrRoles.includes(null)) {
return Promise.reject(new TypeError('INVALID_TYPE', 'roles', return Promise.reject(new TypeError('INVALID_TYPE', 'roles', 'Array or Collection of Roles or Snowflakes', true));
'Array or Collection of Roles or Snowflakes', true));
} }
const newRoles = this._roles.keyArray().filter(role => !roleOrRoles.includes(role)); const newRoles = this._roles.keyArray().filter(role => !roleOrRoles.includes(role));

View File

@@ -1,21 +1,21 @@
'use strict'; 'use strict';
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const DataResolver = require('../util/DataResolver'); const Guild = require('../structures/Guild');
const GuildChannel = require('../structures/GuildChannel');
const GuildEmoji = require('../structures/GuildEmoji');
const GuildMember = require('../structures/GuildMember');
const Invite = require('../structures/Invite');
const Role = require('../structures/Role');
const { const {
Events, Events,
VerificationLevels, VerificationLevels,
DefaultMessageNotifications, DefaultMessageNotifications,
ExplicitContentFilterLevels, ExplicitContentFilterLevels,
} = require('../util/Constants'); } = require('../util/Constants');
const Guild = require('../structures/Guild'); const DataResolver = require('../util/DataResolver');
const GuildChannel = require('../structures/GuildChannel');
const GuildMember = require('../structures/GuildMember');
const Permissions = require('../util/Permissions'); const Permissions = require('../util/Permissions');
const { resolveColor } = require('../util/Util'); const { resolveColor } = require('../util/Util');
const GuildEmoji = require('../structures/GuildEmoji');
const Invite = require('../structures/Invite');
const Role = require('../structures/Role');
/** /**
* Manages API methods for Guilds and stores their cache. * Manages API methods for Guilds and stores their cache.
@@ -91,11 +91,15 @@ class GuildManager extends BaseManager {
* @returns {?Guild} * @returns {?Guild}
*/ */
resolve(guild) { resolve(guild) {
if (guild instanceof GuildChannel || if (
guild instanceof GuildChannel ||
guild instanceof GuildMember || guild instanceof GuildMember ||
guild instanceof GuildEmoji || guild instanceof GuildEmoji ||
guild instanceof Role || guild instanceof Role ||
(guild instanceof Invite && guild.guild)) return super.resolve(guild.guild); (guild instanceof Invite && guild.guild)
) {
return super.resolve(guild.guild);
}
return super.resolve(guild); return super.resolve(guild);
} }
@@ -108,11 +112,15 @@ class GuildManager extends BaseManager {
* @returns {?Snowflake} * @returns {?Snowflake}
*/ */
resolveID(guild) { resolveID(guild) {
if (guild instanceof GuildChannel || if (
guild instanceof GuildChannel ||
guild instanceof GuildMember || guild instanceof GuildMember ||
guild instanceof GuildEmoji || guild instanceof GuildEmoji ||
guild instanceof Role || guild instanceof Role ||
(guild instanceof Invite && guild.guild)) return super.resolveID(guild.guild.id); (guild instanceof Invite && guild.guild)
) {
return super.resolveID(guild.guild.id);
}
return super.resolveID(guild); return super.resolveID(guild);
} }
@@ -132,15 +140,18 @@ class GuildManager extends BaseManager {
* @param {VerificationLevel} [options.verificationLevel] The verification level for the guild * @param {VerificationLevel} [options.verificationLevel] The verification level for the guild
* @returns {Promise<Guild>} The guild that was created * @returns {Promise<Guild>} The guild that was created
*/ */
async create(name, { async create(
channels = [], name,
defaultMessageNotifications, {
explicitContentFilter, channels = [],
icon = null, defaultMessageNotifications,
region, explicitContentFilter,
roles = [], icon = null,
verificationLevel, region,
} = {}) { roles = [],
verificationLevel,
} = {},
) {
icon = await DataResolver.resolveImage(icon); icon = await DataResolver.resolveImage(icon);
if (typeof verificationLevel !== 'undefined' && typeof verificationLevel !== 'number') { if (typeof verificationLevel !== 'undefined' && typeof verificationLevel !== 'number') {
verificationLevel = VerificationLevels.indexOf(verificationLevel); verificationLevel = VerificationLevels.indexOf(verificationLevel);
@@ -167,16 +178,19 @@ class GuildManager extends BaseManager {
if (role.permissions) role.permissions = Permissions.resolve(role.permissions); if (role.permissions) role.permissions = Permissions.resolve(role.permissions);
} }
return new Promise((resolve, reject) => return new Promise((resolve, reject) =>
this.client.api.guilds.post({ data: { this.client.api.guilds
name, .post({
region, data: {
icon, name,
verification_level: verificationLevel, region,
default_message_notifications: defaultMessageNotifications, icon,
explicit_content_filter: explicitContentFilter, verification_level: verificationLevel,
channels, default_message_notifications: defaultMessageNotifications,
roles, explicit_content_filter: explicitContentFilter,
} }) channels,
roles,
},
})
.then(data => { .then(data => {
if (this.client.guilds.cache.has(data.id)) return resolve(this.client.guilds.cache.get(data.id)); if (this.client.guilds.cache.has(data.id)) return resolve(this.client.guilds.cache.get(data.id));

View File

@@ -1,10 +1,10 @@
'use strict'; 'use strict';
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const GuildMember = require('../structures/GuildMember');
const { Events, OPCodes } = require('../util/Constants');
const Collection = require('../util/Collection');
const { Error, TypeError } = require('../errors'); const { Error, TypeError } = require('../errors');
const GuildMember = require('../structures/GuildMember');
const Collection = require('../util/Collection');
const { Events, OPCodes } = require('../util/Constants');
/** /**
* Manages API methods for GuildMembers and stores their cache. * Manages API methods for GuildMembers and stores their cache.
@@ -148,10 +148,15 @@ class GuildMemberManager extends BaseManager {
*/ */
prune({ days = 7, dry = false, count = true, reason } = {}) { prune({ days = 7, dry = false, count = true, reason } = {}) {
if (typeof days !== 'number') throw new TypeError('PRUNE_DAYS_TYPE'); if (typeof days !== 'number') throw new TypeError('PRUNE_DAYS_TYPE');
return this.client.api.guilds(this.guild.id).prune[dry ? 'get' : 'post']({ query: { return this.client.api
days, .guilds(this.guild.id)
compute_prune_count: count, .prune[dry ? 'get' : 'post']({
}, reason }) query: {
days,
compute_prune_count: count,
},
reason,
})
.then(data => data.pruned); .then(data => data.pruned);
} }
@@ -174,7 +179,9 @@ class GuildMemberManager extends BaseManager {
if (options.days) options['delete-message-days'] = options.days; if (options.days) options['delete-message-days'] = options.days;
const id = this.client.users.resolveID(user); const id = this.client.users.resolveID(user);
if (!id) return Promise.reject(new Error('BAN_RESOLVE_ID', true)); if (!id) return Promise.reject(new Error('BAN_RESOLVE_ID', true));
return this.client.api.guilds(this.guild.id).bans[id].put({ query: options }) return this.client.api
.guilds(this.guild.id)
.bans[id].put({ query: options })
.then(() => { .then(() => {
if (user instanceof GuildMember) return user; if (user instanceof GuildMember) return user;
const _user = this.client.users.resolve(id); const _user = this.client.users.resolve(id);
@@ -200,21 +207,25 @@ class GuildMemberManager extends BaseManager {
unban(user, reason) { unban(user, reason) {
const id = this.client.users.resolveID(user); const id = this.client.users.resolveID(user);
if (!id) return Promise.reject(new Error('BAN_RESOLVE_ID')); if (!id) return Promise.reject(new Error('BAN_RESOLVE_ID'));
return this.client.api.guilds(this.guild.id).bans[id].delete({ reason }) return this.client.api
.guilds(this.guild.id)
.bans[id].delete({ reason })
.then(() => this.client.users.resolve(user)); .then(() => this.client.users.resolve(user));
} }
_fetchSingle({ user, cache }) { _fetchSingle({ user, cache }) {
const existing = this.cache.get(user); const existing = this.cache.get(user);
if (existing && !existing.partial) return Promise.resolve(existing); if (existing && !existing.partial) return Promise.resolve(existing);
return this.client.api.guilds(this.guild.id).members(user).get() return this.client.api
.guilds(this.guild.id)
.members(user)
.get()
.then(data => this.add(data, cache)); .then(data => this.add(data, cache));
} }
_fetchMany({ limit = 0, withPresences: presences = false, user: user_ids, query } = {}) { _fetchMany({ limit = 0, withPresences: presences = false, user: user_ids, query } = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.guild.memberCount === this.cache.size && (!query && !limit && !presences && !user_ids)) { if (this.guild.memberCount === this.cache.size && !query && !limit && !presences && !user_ids) {
resolve(this.cache); resolve(this.cache);
return; return;
} }
@@ -237,9 +248,11 @@ class GuildMemberManager extends BaseManager {
for (const member of members.values()) { for (const member of members.values()) {
if (option) fetchedMembers.set(member.id, member); if (option) fetchedMembers.set(member.id, member);
} }
if (this.guild.memberCount <= this.cache.size || if (
this.guild.memberCount <= this.cache.size ||
(option && members.size < 1000) || (option && members.size < 1000) ||
(limit && fetchedMembers.size >= limit)) { (limit && fetchedMembers.size >= limit)
) {
this.guild.client.removeListener(Events.GUILD_MEMBERS_CHUNK, handler); this.guild.client.removeListener(Events.GUILD_MEMBERS_CHUNK, handler);
let fetched = option ? fetchedMembers : this.cache; let fetched = option ? fetchedMembers : this.cache;
if (user_ids && !Array.isArray(user_ids) && fetched.size) fetched = fetched.first(); if (user_ids && !Array.isArray(user_ids) && fetched.size) fetched = fetched.first();

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const Collection = require('../util/Collection');
const { TypeError } = require('../errors'); const { TypeError } = require('../errors');
const Collection = require('../util/Collection');
/** /**
* Manages API methods for roles of a GuildMember and stores their cache. * Manages API methods for roles of a GuildMember and stores their cache.
@@ -49,7 +49,7 @@ class GuildMemberRoleManager {
get hoist() { get hoist() {
const hoistedRoles = this._roles.filter(role => role.hoist); const hoistedRoles = this._roles.filter(role => role.hoist);
if (!hoistedRoles.size) return null; if (!hoistedRoles.size) return null;
return hoistedRoles.reduce((prev, role) => !prev || role.comparePositionTo(prev) > 0 ? role : prev); return hoistedRoles.reduce((prev, role) => (!prev || role.comparePositionTo(prev) > 0 ? role : prev));
} }
/** /**
@@ -60,7 +60,7 @@ class GuildMemberRoleManager {
get color() { get color() {
const coloredRoles = this._roles.filter(role => role.color); const coloredRoles = this._roles.filter(role => role.color);
if (!coloredRoles.size) return null; if (!coloredRoles.size) return null;
return coloredRoles.reduce((prev, role) => !prev || role.comparePositionTo(prev) > 0 ? role : prev); return coloredRoles.reduce((prev, role) => (!prev || role.comparePositionTo(prev) > 0 ? role : prev));
} }
/** /**
@@ -69,7 +69,7 @@ class GuildMemberRoleManager {
* @readonly * @readonly
*/ */
get highest() { get highest() {
return this._roles.reduce((prev, role) => role.comparePositionTo(prev) > 0 ? role : prev, this._roles.first()); return this._roles.reduce((prev, role) => (role.comparePositionTo(prev) > 0 ? role : prev), this._roles.first());
} }
/** /**
@@ -82,8 +82,7 @@ class GuildMemberRoleManager {
if (roleOrRoles instanceof Collection || Array.isArray(roleOrRoles)) { if (roleOrRoles instanceof Collection || Array.isArray(roleOrRoles)) {
roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolve(r)); roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolve(r));
if (roleOrRoles.includes(null)) { if (roleOrRoles.includes(null)) {
throw new TypeError('INVALID_TYPE', 'roles', throw new TypeError('INVALID_TYPE', 'roles', 'Array or Collection of Roles or Snowflakes', true);
'Array or Collection of Roles or Snowflakes', true);
} }
const newRoles = [...new Set(roleOrRoles.concat(...this._roles.values()))]; const newRoles = [...new Set(roleOrRoles.concat(...this._roles.values()))];
@@ -91,8 +90,7 @@ class GuildMemberRoleManager {
} else { } else {
roleOrRoles = this.guild.roles.resolve(roleOrRoles); roleOrRoles = this.guild.roles.resolve(roleOrRoles);
if (roleOrRoles === null) { if (roleOrRoles === null) {
throw new TypeError('INVALID_TYPE', 'roles', throw new TypeError('INVALID_TYPE', 'roles', 'Array or Collection of Roles or Snowflakes', true);
'Array or Collection of Roles or Snowflakes', true);
} }
await this.client.api.guilds[this.guild.id].members[this.member.id].roles[roleOrRoles.id].put({ reason }); await this.client.api.guilds[this.guild.id].members[this.member.id].roles[roleOrRoles.id].put({ reason });
@@ -113,8 +111,7 @@ class GuildMemberRoleManager {
if (roleOrRoles instanceof Collection || Array.isArray(roleOrRoles)) { if (roleOrRoles instanceof Collection || Array.isArray(roleOrRoles)) {
roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolve(r)); roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolve(r));
if (roleOrRoles.includes(null)) { if (roleOrRoles.includes(null)) {
throw new TypeError('INVALID_TYPE', 'roles', throw new TypeError('INVALID_TYPE', 'roles', 'Array or Collection of Roles or Snowflakes', true);
'Array or Collection of Roles or Snowflakes', true);
} }
const newRoles = this._roles.filter(role => !roleOrRoles.includes(role)); const newRoles = this._roles.filter(role => !roleOrRoles.includes(role));
@@ -122,8 +119,7 @@ class GuildMemberRoleManager {
} else { } else {
roleOrRoles = this.guild.roles.resolve(roleOrRoles); roleOrRoles = this.guild.roles.resolve(roleOrRoles);
if (roleOrRoles === null) { if (roleOrRoles === null) {
throw new TypeError('INVALID_TYPE', 'roles', throw new TypeError('INVALID_TYPE', 'roles', 'Array or Collection of Roles or Snowflakes', true);
'Array or Collection of Roles or Snowflakes', true);
} }
await this.client.api.guilds[this.guild.id].members[this.member.id].roles[roleOrRoles.id].delete({ reason }); await this.client.api.guilds[this.guild.id].members[this.member.id].roles[roleOrRoles.id].delete({ reason });

View File

@@ -2,82 +2,82 @@
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const Message = require('../structures/Message'); const Message = require('../structures/Message');
const LimitedCollection = require('../util/LimitedCollection');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
const LimitedCollection = require('../util/LimitedCollection');
/** /**
* Manages API methods for Messages and holds their cache. * Manages API methods for Messages and holds their cache.
* @extends {BaseManager} * @extends {BaseManager}
*/ */
class MessageManager extends BaseManager { class MessageManager extends BaseManager {
constructor(channel, iterable) { constructor(channel, iterable) {
super(channel.client, iterable, Message, LimitedCollection, channel.client.options.messageCacheMaxSize); super(channel.client, iterable, Message, LimitedCollection, channel.client.options.messageCacheMaxSize);
/** /**
* The channel that the messages belong to * The channel that the messages belong to
* @type {TextBasedChannel} * @type {TextBasedChannel}
*/ */
this.channel = channel; this.channel = channel;
} }
/** /**
* The cache of Messages * The cache of Messages
* @type {LimitedCollection<Snowflake, Message>} * @type {LimitedCollection<Snowflake, Message>}
* @name MessageManager#cache * @name MessageManager#cache
*/ */
add(data, cache) { add(data, cache) {
return super.add(data, cache, { extras: [this.channel] }); return super.add(data, cache, { extras: [this.channel] });
} }
/** /**
* The parameters to pass in when requesting previous messages from a channel. `around`, `before` and * The parameters to pass in when requesting previous messages from a channel. `around`, `before` and
* `after` are mutually exclusive. All the parameters are optional. * `after` are mutually exclusive. All the parameters are optional.
* @typedef {Object} ChannelLogsQueryOptions * @typedef {Object} ChannelLogsQueryOptions
* @property {number} [limit=50] Number of messages to acquire * @property {number} [limit=50] Number of messages to acquire
* @property {Snowflake} [before] ID of a message to get the messages that were posted before it * @property {Snowflake} [before] ID of a message to get the messages that were posted before it
* @property {Snowflake} [after] ID of a message to get the messages that were posted after it * @property {Snowflake} [after] ID of a message to get the messages that were posted after it
* @property {Snowflake} [around] ID of a message to get the messages that were posted around it * @property {Snowflake} [around] ID of a message to get the messages that were posted around it
*/ */
/** /**
* Gets a message, or messages, from this channel. * Gets a message, or messages, from this channel.
* <info>The returned Collection does not contain reaction users of the messages if they were not cached. * <info>The returned Collection does not contain reaction users of the messages if they were not cached.
* Those need to be fetched separately in such a case.</info> * Those need to be fetched separately in such a case.</info>
* @param {Snowflake|ChannelLogsQueryOptions} [message] The ID of the message to fetch, or query parameters. * @param {Snowflake|ChannelLogsQueryOptions} [message] The ID of the message to fetch, or query parameters.
* @param {boolean} [cache=true] Whether to cache the message(s) * @param {boolean} [cache=true] Whether to cache the message(s)
* @returns {Promise<Message>|Promise<Collection<Snowflake, Message>>} * @returns {Promise<Message>|Promise<Collection<Snowflake, Message>>}
* @example * @example
* // Get message * // Get message
* channel.messages.fetch('99539446449315840') * channel.messages.fetch('99539446449315840')
* .then(message => console.log(message.content)) * .then(message => console.log(message.content))
* .catch(console.error); * .catch(console.error);
* @example * @example
* // Get messages * // Get messages
* channel.messages.fetch({ limit: 10 }) * channel.messages.fetch({ limit: 10 })
* .then(messages => console.log(`Received ${messages.size} messages`)) * .then(messages => console.log(`Received ${messages.size} messages`))
* .catch(console.error); * .catch(console.error);
* @example * @example
* // Get messages and filter by user ID * // Get messages and filter by user ID
* channel.messages.fetch() * channel.messages.fetch()
* .then(messages => console.log(`${messages.filter(m => m.author.id === '84484653687267328').size} messages`)) * .then(messages => console.log(`${messages.filter(m => m.author.id === '84484653687267328').size} messages`))
* .catch(console.error); * .catch(console.error);
*/ */
fetch(message, cache = true) { fetch(message, cache = true) {
return typeof message === 'string' ? this._fetchId(message, cache) : this._fetchMany(message, cache); return typeof message === 'string' ? this._fetchId(message, cache) : this._fetchMany(message, cache);
} }
/** /**
* Fetches the pinned messages of this channel and returns a collection of them. * Fetches the pinned messages of this channel and returns a collection of them.
* <info>The returned Collection does not contain any reaction data of the messages. * <info>The returned Collection does not contain any reaction data of the messages.
* Those need to be fetched separately.</info> * Those need to be fetched separately.</info>
* @param {boolean} [cache=true] Whether to cache the message(s) * @param {boolean} [cache=true] Whether to cache the message(s)
* @returns {Promise<Collection<Snowflake, Message>>} * @returns {Promise<Collection<Snowflake, Message>>}
* @example * @example
* // Get pinned messages * // Get pinned messages
* channel.fetchPinned() * channel.fetchPinned()
* .then(messages => console.log(`Received ${messages.size} messages`)) * .then(messages => console.log(`Received ${messages.size} messages`))
* .catch(console.error); * .catch(console.error);
*/ */
fetchPinned(cache = true) { fetchPinned(cache = true) {
return this.client.api.channels[this.channel.id].pins.get().then(data => { return this.client.api.channels[this.channel.id].pins.get().then(data => {
const messages = new Collection(); const messages = new Collection();
@@ -94,23 +94,22 @@ class MessageManager extends BaseManager {
*/ */
/** /**
* Resolves a MessageResolvable to a Message object. * Resolves a MessageResolvable to a Message object.
* @method resolve * @method resolve
* @memberof MessageManager * @memberof MessageManager
* @instance * @instance
* @param {MessageResolvable} message The message resolvable to resolve * @param {MessageResolvable} message The message resolvable to resolve
* @returns {?Message} * @returns {?Message}
*/ */
/** /**
* Resolves a MessageResolvable to a Message ID string. * Resolves a MessageResolvable to a Message ID string.
* @method resolveID * @method resolveID
* @memberof MessageManager * @memberof MessageManager
* @instance * @instance
* @param {MessageResolvable} message The message resolvable to resolve * @param {MessageResolvable} message The message resolvable to resolve
* @returns {?Snowflake} * @returns {?Snowflake}
*/ */
/** /**
* Deletes a message, even if it's not cached. * Deletes a message, even if it's not cached.
@@ -119,7 +118,12 @@ class MessageManager extends BaseManager {
*/ */
async delete(message, reason) { async delete(message, reason) {
message = this.resolveID(message); message = this.resolveID(message);
if (message) await this.client.api.channels(this.channel.id).messages(message).delete({ reason }); if (message) {
await this.client.api
.channels(this.channel.id)
.messages(message)
.delete({ reason });
}
} }
async _fetchId(messageID, cache) { async _fetchId(messageID, cache) {

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const { Presence } = require('../structures/Presence');
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const { Presence } = require('../structures/Presence');
/** /**
* Manages API methods for Presences and holds their cache. * Manages API methods for Presences and holds their cache.
@@ -13,10 +13,10 @@ class PresenceManager extends BaseManager {
} }
/** /**
* The cache of Presences * The cache of Presences
* @type {Collection<Snowflake, Presence>} * @type {Collection<Snowflake, Presence>}
* @name PresenceManager#cache * @name PresenceManager#cache
*/ */
add(data, cache) { add(data, cache) {
const existing = this.cache.get(data.user.id); const existing = this.cache.get(data.user.id);
@@ -32,10 +32,10 @@ class PresenceManager extends BaseManager {
*/ */
/** /**
* Resolves a PresenceResolvable to a Presence object. * Resolves a PresenceResolvable to a Presence object.
* @param {PresenceResolvable} presence The presence resolvable to resolve * @param {PresenceResolvable} presence The presence resolvable to resolve
* @returns {?Presence} * @returns {?Presence}
*/ */
resolve(presence) { resolve(presence) {
const presenceResolvable = super.resolve(presence); const presenceResolvable = super.resolve(presence);
if (presenceResolvable) return presenceResolvable; if (presenceResolvable) return presenceResolvable;
@@ -44,10 +44,10 @@ class PresenceManager extends BaseManager {
} }
/** /**
* Resolves a PresenceResolvable to a Presence ID string. * Resolves a PresenceResolvable to a Presence ID string.
* @param {PresenceResolvable} presence The presence resolvable to resolve * @param {PresenceResolvable} presence The presence resolvable to resolve
* @returns {?Snowflake} * @returns {?Snowflake}
*/ */
resolveID(presence) { resolveID(presence) {
const presenceResolvable = super.resolveID(presence); const presenceResolvable = super.resolveID(presence);
if (presenceResolvable) return presenceResolvable; if (presenceResolvable) return presenceResolvable;

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const MessageReaction = require('../structures/MessageReaction');
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const MessageReaction = require('../structures/MessageReaction');
/** /**
* Manages API methods for reactions and holds their cache. * Manages API methods for reactions and holds their cache.
@@ -36,29 +36,32 @@ class ReactionManager extends BaseManager {
*/ */
/** /**
* Resolves a MessageReactionResolvable to a MessageReaction object. * Resolves a MessageReactionResolvable to a MessageReaction object.
* @method resolve * @method resolve
* @memberof ReactionManager * @memberof ReactionManager
* @instance * @instance
* @param {MessageReactionResolvable} reaction The MessageReaction to resolve * @param {MessageReactionResolvable} reaction The MessageReaction to resolve
* @returns {?MessageReaction} * @returns {?MessageReaction}
*/ */
/** /**
* Resolves a MessageReactionResolvable to a MessageReaction ID string. * Resolves a MessageReactionResolvable to a MessageReaction ID string.
* @method resolveID * @method resolveID
* @memberof ReactionManager * @memberof ReactionManager
* @instance * @instance
* @param {MessageReactionResolvable} reaction The MessageReaction to resolve * @param {MessageReactionResolvable} reaction The MessageReaction to resolve
* @returns {?Snowflake} * @returns {?Snowflake}
*/ */
/** /**
* Removes all reactions from a message. * Removes all reactions from a message.
* @returns {Promise<Message>} * @returns {Promise<Message>}
*/ */
removeAll() { removeAll() {
return this.client.api.channels(this.message.channel.id).messages(this.message.id).reactions.delete() return this.client.api
.channels(this.message.channel.id)
.messages(this.message.id)
.reactions.delete()
.then(() => this.message); .then(() => this.message);
} }
@@ -72,7 +75,10 @@ class ReactionManager extends BaseManager {
const id = reactionEmoji.id || reactionEmoji.name; const id = reactionEmoji.id || reactionEmoji.name;
const existing = this.cache.get(id); const existing = this.cache.get(id);
if (!this._partial(reactionEmoji)) return existing; if (!this._partial(reactionEmoji)) return existing;
const data = await this.client.api.channels(this.message.channel.id).messages(this.message.id).get(); const data = await this.client.api
.channels(this.message.channel.id)
.messages(this.message.id)
.get();
if (this.message.partial) this.message._patch(data); if (this.message.partial) this.message._patch(data);
if (!data.reactions || !data.reactions.some(r => (r.emoji.id || r.emoji.name) === id)) { if (!data.reactions || !data.reactions.some(r => (r.emoji.id || r.emoji.name) === id)) {
reactionEmoji.reaction._patch({ count: 0 }); reactionEmoji.reaction._patch({ count: 0 });

View File

@@ -1,8 +1,8 @@
'use strict'; 'use strict';
const Collection = require('../util/Collection');
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const { Error } = require('../errors'); const { Error } = require('../errors');
const Collection = require('../util/Collection');
/** /**
* Manages API methods for users who reacted to a reaction and stores their cache. * Manages API methods for users who reacted to a reaction and stores their cache.
@@ -34,9 +34,9 @@ class ReactionUserManager extends BaseManager {
*/ */
async fetch({ limit = 100, after, before } = {}) { async fetch({ limit = 100, after, before } = {}) {
const message = this.reaction.message; const message = this.reaction.message;
const data = await this.client.api.channels[message.channel.id].messages[message.id] const data = await this.client.api.channels[message.channel.id].messages[message.id].reactions[
.reactions[this.reaction.emoji.identifier] this.reaction.emoji.identifier
.get({ query: { limit, before, after } }); ].get({ query: { limit, before, after } });
const users = new Collection(); const users = new Collection();
for (const rawUser of data) { for (const rawUser of data) {
const user = this.client.users.add(rawUser); const user = this.client.users.add(rawUser);
@@ -55,8 +55,9 @@ class ReactionUserManager extends BaseManager {
const message = this.reaction.message; const message = this.reaction.message;
const userID = message.client.users.resolveID(user); const userID = message.client.users.resolveID(user);
if (!userID) return Promise.reject(new Error('REACTION_RESOLVE_USER')); if (!userID) return Promise.reject(new Error('REACTION_RESOLVE_USER'));
return message.client.api.channels[message.channel.id].messages[message.id] return message.client.api.channels[message.channel.id].messages[message.id].reactions[
.reactions[this.reaction.emoji.identifier][userID === message.client.user.id ? '@me' : userID] this.reaction.emoji.identifier
][userID === message.client.user.id ? '@me' : userID]
.delete() .delete()
.then(() => this.reaction); .then(() => this.reaction);
} }

View File

@@ -2,8 +2,8 @@
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const Role = require('../structures/Role'); const Role = require('../structures/Role');
const { resolveColor } = require('../util/Util');
const Permissions = require('../util/Permissions'); const Permissions = require('../util/Permissions');
const { resolveColor } = require('../util/Util');
/** /**
* Manages API methods for roles and stores their cache. * Manages API methods for roles and stores their cache.
@@ -110,14 +110,17 @@ class RoleManager extends BaseManager {
if (data.color) data.color = resolveColor(data.color); if (data.color) data.color = resolveColor(data.color);
if (data.permissions) data.permissions = Permissions.resolve(data.permissions); if (data.permissions) data.permissions = Permissions.resolve(data.permissions);
return this.guild.client.api.guilds(this.guild.id).roles.post({ data, reason }).then(r => { return this.guild.client.api
const { role } = this.client.actions.GuildRoleCreate.handle({ .guilds(this.guild.id)
guild_id: this.guild.id, .roles.post({ data, reason })
role: r, .then(r => {
const { role } = this.client.actions.GuildRoleCreate.handle({
guild_id: this.guild.id,
role: r,
});
if (data.position) return role.setPosition(data.position, reason);
return role;
}); });
if (data.position) return role.setPosition(data.position, reason);
return role;
});
} }
/** /**
@@ -135,7 +138,7 @@ class RoleManager extends BaseManager {
* @readonly * @readonly
*/ */
get highest() { get highest() {
return this.cache.reduce((prev, role) => role.comparePositionTo(prev) > 0 ? role : prev, this.cache.first()); return this.cache.reduce((prev, role) => (role.comparePositionTo(prev) > 0 ? role : prev), this.cache.first());
} }
} }

View File

@@ -1,9 +1,9 @@
'use strict'; 'use strict';
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const User = require('../structures/User');
const GuildMember = require('../structures/GuildMember'); const GuildMember = require('../structures/GuildMember');
const Message = require('../structures/Message'); const Message = require('../structures/Message');
const User = require('../structures/User');
/** /**
* Manages API methods for users and stores their cache. * Manages API methods for users and stores their cache.

View File

@@ -1,10 +1,10 @@
'use strict'; 'use strict';
const FormData = require('form-data');
const https = require('https'); const https = require('https');
const { browser, UserAgent } = require('../util/Constants');
const fetch = require('node-fetch');
const AbortController = require('abort-controller'); const AbortController = require('abort-controller');
const FormData = require('form-data');
const fetch = require('node-fetch');
const { browser, UserAgent } = require('../util/Constants');
if (https.Agent) var agent = new https.Agent({ keepAlive: true }); if (https.Agent) var agent = new https.Agent({ keepAlive: true });
@@ -26,8 +26,10 @@ class APIRequest {
} }
make() { make() {
const API = this.options.versioned === false ? this.client.options.http.api : const API =
`${this.client.options.http.api}/v${this.client.options.http.version}`; this.options.versioned === false
? this.client.options.http.api
: `${this.client.options.http.api}/v${this.client.options.http.version}`;
const url = API + this.path; const url = API + this.path;
let headers = {}; let headers = {};
@@ -42,7 +44,8 @@ class APIRequest {
for (const file of this.options.files) if (file && file.file) body.append(file.name, file.file, file.name); for (const file of this.options.files) if (file && file.file) body.append(file.name, file.file, file.name);
if (typeof this.options.data !== 'undefined') body.append('payload_json', JSON.stringify(this.options.data)); if (typeof this.options.data !== 'undefined') body.append('payload_json', JSON.stringify(this.options.data));
if (!browser) headers = Object.assign(headers, body.getHeaders()); if (!browser) headers = Object.assign(headers, body.getHeaders());
} else if (this.options.data != null) { // eslint-disable-line eqeqeq // eslint-disable-next-line eqeqeq
} else if (this.options.data != null) {
body = JSON.stringify(this.options.data); body = JSON.stringify(this.options.data);
headers['Content-Type'] = 'application/json'; headers['Content-Type'] = 'application/json';
} }

View File

@@ -3,8 +3,12 @@
const noop = () => {}; // eslint-disable-line no-empty-function const noop = () => {}; // eslint-disable-line no-empty-function
const methods = ['get', 'post', 'delete', 'patch', 'put']; const methods = ['get', 'post', 'delete', 'patch', 'put'];
const reflectors = [ const reflectors = [
'toString', 'valueOf', 'inspect', 'constructor', 'toString',
Symbol.toPrimitive, Symbol.for('nodejs.util.inspect.custom'), 'valueOf',
'inspect',
'constructor',
Symbol.toPrimitive,
Symbol.for('nodejs.util.inspect.custom'),
]; ];
function buildRoute(manager) { function buildRoute(manager) {
@@ -22,10 +26,18 @@ function buildRoute(manager) {
// All other parts of the route should be considered as part of the bucket identifier // All other parts of the route should be considered as part of the bucket identifier
else routeBucket.push(route[i]); else routeBucket.push(route[i]);
} }
return options => manager.request(name, route.join('/'), Object.assign({ return options =>
versioned: manager.versioned, manager.request(
route: routeBucket.join('/'), name,
}, options)); route.join('/'),
Object.assign(
{
versioned: manager.versioned,
route: routeBucket.join('/'),
},
options,
),
);
} }
route.push(name); route.push(name);
return new Proxy(noop, handler); return new Proxy(noop, handler);

View File

@@ -48,7 +48,7 @@ class DiscordAPIError extends Error {
for (const [k, v] of Object.entries(obj)) { for (const [k, v] of Object.entries(obj)) {
if (k === 'message') continue; if (k === 'message') continue;
const newKey = key ? isNaN(k) ? `${key}.${k}` : `${key}[${k}]` : k; const newKey = key ? (isNaN(k) ? `${key}.${k}` : `${key}[${k}]`) : k;
if (v._errors) { if (v._errors) {
messages.push(`${newKey}: ${v._errors.map(e => e.message).join(' ')}`); messages.push(`${newKey}: ${v._errors.map(e => e.message).join(' ')}`);

View File

@@ -1,11 +1,11 @@
'use strict'; 'use strict';
const RequestHandler = require('./RequestHandler');
const APIRequest = require('./APIRequest'); const APIRequest = require('./APIRequest');
const routeBuilder = require('./APIRouter'); const routeBuilder = require('./APIRouter');
const RequestHandler = require('./RequestHandler');
const { Error } = require('../errors'); const { Error } = require('../errors');
const { Endpoints } = require('../util/Constants');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
const { Endpoints } = require('../util/Constants');
class RESTManager { class RESTManager {
constructor(client, tokenPrefix = 'Bot') { constructor(client, tokenPrefix = 'Bot') {
@@ -37,12 +37,14 @@ class RESTManager {
push(handler, apiRequest) { push(handler, apiRequest) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
handler.push({ handler
request: apiRequest, .push({
resolve, request: apiRequest,
reject, resolve,
retries: 0, reject,
}).catch(reject); retries: 0,
})
.catch(reject);
}); });
} }

View File

@@ -2,8 +2,11 @@
const DiscordAPIError = require('./DiscordAPIError'); const DiscordAPIError = require('./DiscordAPIError');
const HTTPError = require('./HTTPError'); const HTTPError = require('./HTTPError');
const {
Events: { RATE_LIMIT },
browser,
} = require('../util/Constants');
const Util = require('../util/Util'); const Util = require('../util/Util');
const { Events: { RATE_LIMIT }, browser } = require('../util/Constants');
function parseResponse(res) { function parseResponse(res) {
if (res.headers.get('content-type').startsWith('application/json')) return res.json(); if (res.headers.get('content-type').startsWith('application/json')) return res.json();
@@ -52,7 +55,6 @@ class RequestHandler {
return this.queue.length === 0 && !this.limited && this.busy !== true; return this.queue.length === 0 && !this.limited && this.busy !== true;
} }
/* eslint-disable-next-line complexity */
async execute(item) { async execute(item) {
// Insert item back to the beginning if currently busy // Insert item back to the beginning if currently busy
if (this.busy) { if (this.busy) {
@@ -102,9 +104,7 @@ class RequestHandler {
} catch (error) { } catch (error) {
// NodeFetch error expected for all "operational" errors, such as 500 status code // NodeFetch error expected for all "operational" errors, such as 500 status code
this.busy = false; this.busy = false;
return reject( return reject(new HTTPError(error.message, error.constructor.name, error.status, request.method, request.path));
new HTTPError(error.message, error.constructor.name, error.status, request.method, request.path),
);
} }
if (res && res.headers) { if (res && res.headers) {
@@ -171,9 +171,7 @@ class RequestHandler {
} }
return null; return null;
} catch (err) { } catch (err) {
return reject( return reject(new HTTPError(err.message, err.constructor.name, err.status, request.method, request.path));
new HTTPError(err.message, err.constructor.name, err.status, request.method, request.path),
);
} }
} }
} }

View File

@@ -2,8 +2,8 @@
const EventEmitter = require('events'); const EventEmitter = require('events');
const path = require('path'); const path = require('path');
const Util = require('../util/Util');
const { Error } = require('../errors'); const { Error } = require('../errors');
const Util = require('../util/Util');
let childProcess = null; let childProcess = null;
let Worker = null; let Worker = null;
@@ -111,9 +111,11 @@ class Shard extends EventEmitter {
if (this.worker) throw new Error('SHARDING_WORKER_EXISTS', this.id); if (this.worker) throw new Error('SHARDING_WORKER_EXISTS', this.id);
if (this.manager.mode === 'process') { if (this.manager.mode === 'process') {
this.process = childProcess.fork(path.resolve(this.manager.file), this.args, { this.process = childProcess
env: this.env, execArgv: this.execArgv, .fork(path.resolve(this.manager.file), this.args, {
}) env: this.env,
execArgv: this.execArgv,
})
.on('message', this._handleMessage.bind(this)) .on('message', this._handleMessage.bind(this))
.on('exit', this._exitListener); .on('exit', this._exitListener);
} else if (this.manager.mode === 'worker') { } else if (this.manager.mode === 'worker') {
@@ -203,7 +205,8 @@ class Shard extends EventEmitter {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.process) { if (this.process) {
this.process.send(message, err => { this.process.send(message, err => {
if (err) reject(err); else resolve(this); if (err) reject(err);
else resolve(this);
}); });
} else { } else {
this.worker.postMessage(message); this.worker.postMessage(message);
@@ -261,7 +264,8 @@ class Shard extends EventEmitter {
if (!message || message._eval !== script) return; if (!message || message._eval !== script) return;
child.removeListener('message', listener); child.removeListener('message', listener);
this._evals.delete(script); this._evals.delete(script);
if (!message._error) resolve(message._result); else reject(Util.makeError(message._error)); if (!message._error) resolve(message._result);
else reject(Util.makeError(message._error));
}; };
child.on('message', listener); child.on('message', listener);

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const Util = require('../util/Util');
const { Events } = require('../util/Constants'); const { Events } = require('../util/Constants');
const Util = require('../util/Util');
/** /**
* Helper class for sharded clients spawned as a child process/worker, such as from a {@link ShardingManager}. * Helper class for sharded clients spawned as a child process/worker, such as from a {@link ShardingManager}.
@@ -33,15 +33,27 @@ class ShardClientUtil {
if (mode === 'process') { if (mode === 'process') {
process.on('message', this._handleMessage.bind(this)); process.on('message', this._handleMessage.bind(this));
client.on('ready', () => { process.send({ _ready: true }); }); client.on('ready', () => {
client.on('disconnect', () => { process.send({ _disconnect: true }); }); process.send({ _ready: true });
client.on('reconnecting', () => { process.send({ _reconnecting: true }); }); });
client.on('disconnect', () => {
process.send({ _disconnect: true });
});
client.on('reconnecting', () => {
process.send({ _reconnecting: true });
});
} else if (mode === 'worker') { } else if (mode === 'worker') {
this.parentPort = require('worker_threads').parentPort; this.parentPort = require('worker_threads').parentPort;
this.parentPort.on('message', this._handleMessage.bind(this)); this.parentPort.on('message', this._handleMessage.bind(this));
client.on('ready', () => { this.parentPort.postMessage({ _ready: true }); }); client.on('ready', () => {
client.on('disconnect', () => { this.parentPort.postMessage({ _disconnect: true }); }); this.parentPort.postMessage({ _ready: true });
client.on('reconnecting', () => { this.parentPort.postMessage({ _reconnecting: true }); }); });
client.on('disconnect', () => {
this.parentPort.postMessage({ _disconnect: true });
});
client.on('reconnecting', () => {
this.parentPort.postMessage({ _reconnecting: true });
});
} }
} }
@@ -72,7 +84,8 @@ class ShardClientUtil {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.mode === 'process') { if (this.mode === 'process') {
process.send(message, err => { process.send(message, err => {
if (err) reject(err); else resolve(); if (err) reject(err);
else resolve();
}); });
} else if (this.mode === 'worker') { } else if (this.mode === 'worker') {
this.parentPort.postMessage(message); this.parentPort.postMessage(message);
@@ -98,7 +111,8 @@ class ShardClientUtil {
const listener = message => { const listener = message => {
if (!message || message._sFetchProp !== prop) return; if (!message || message._sFetchProp !== prop) return;
parent.removeListener('message', listener); parent.removeListener('message', listener);
if (!message._error) resolve(message._result); else reject(Util.makeError(message._error)); if (!message._error) resolve(message._result);
else reject(Util.makeError(message._error));
}; };
parent.on('message', listener); parent.on('message', listener);
@@ -127,7 +141,8 @@ class ShardClientUtil {
const listener = message => { const listener = message => {
if (!message || message._sEval !== script) return; if (!message || message._sEval !== script) return;
parent.removeListener('message', listener); parent.removeListener('message', listener);
if (!message._error) resolve(message._result); else reject(Util.makeError(message._error)); if (!message._error) resolve(message._result);
else reject(Util.makeError(message._error));
}; };
parent.on('message', listener); parent.on('message', listener);
@@ -201,8 +216,10 @@ class ShardClientUtil {
if (!this._singleton) { if (!this._singleton) {
this._singleton = new this(client, mode); this._singleton = new this(client, mode);
} else { } else {
client.emit(Events.WARN, client.emit(
'Multiple clients created in child process/worker; only the first will handle sharding helpers.'); Events.WARN,
'Multiple clients created in child process/worker; only the first will handle sharding helpers.',
);
} }
return this._singleton; return this._singleton;
} }

View File

@@ -1,12 +1,12 @@
'use strict'; 'use strict';
const path = require('path');
const fs = require('fs');
const EventEmitter = require('events'); const EventEmitter = require('events');
const fs = require('fs');
const path = require('path');
const Shard = require('./Shard'); const Shard = require('./Shard');
const { Error, TypeError, RangeError } = require('../errors');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
const Util = require('../util/Util'); const Util = require('../util/Util');
const { Error, TypeError, RangeError } = require('../errors');
/** /**
* This is a utility class that makes multi-process sharding of a bot an easy and painless experience. * This is a utility class that makes multi-process sharding of a bot an easy and painless experience.
@@ -41,14 +41,17 @@ class ShardingManager extends EventEmitter {
*/ */
constructor(file, options = {}) { constructor(file, options = {}) {
super(); super();
options = Util.mergeDefault({ options = Util.mergeDefault(
totalShards: 'auto', {
mode: 'process', totalShards: 'auto',
respawn: true, mode: 'process',
shardArgs: [], respawn: true,
execArgv: [], shardArgs: [],
token: process.env.DISCORD_TOKEN, execArgv: [],
}, options); token: process.env.DISCORD_TOKEN,
},
options,
);
/** /**
* Path to the shard script file * Path to the shard script file
@@ -71,8 +74,11 @@ class ShardingManager extends EventEmitter {
} }
this.shardList = [...new Set(this.shardList)]; this.shardList = [...new Set(this.shardList)];
if (this.shardList.length < 1) throw new RangeError('CLIENT_INVALID_OPTION', 'shardList', 'at least 1 ID.'); if (this.shardList.length < 1) throw new RangeError('CLIENT_INVALID_OPTION', 'shardList', 'at least 1 ID.');
if (this.shardList.some(shardID => typeof shardID !== 'number' || isNaN(shardID) || if (
!Number.isInteger(shardID) || shardID < 0)) { this.shardList.some(
shardID => typeof shardID !== 'number' || isNaN(shardID) || !Number.isInteger(shardID) || shardID < 0,
)
) {
throw new TypeError('CLIENT_INVALID_OPTION', 'shardList', 'an array of positive integers.'); throw new TypeError('CLIENT_INVALID_OPTION', 'shardList', 'an array of positive integers.');
} }
} }
@@ -186,8 +192,11 @@ class ShardingManager extends EventEmitter {
} }
if (this.shardList.some(shardID => shardID >= amount)) { if (this.shardList.some(shardID => shardID >= amount)) {
throw new RangeError('CLIENT_INVALID_OPTION', 'Amount of shards', throw new RangeError(
'bigger than the highest shardID in the shardList option.'); 'CLIENT_INVALID_OPTION',
'Amount of shards',
'bigger than the highest shardID in the shardList option.',
);
} }
// Spawn the shards // Spawn the shards

View File

@@ -1,12 +1,12 @@
'use strict'; 'use strict';
const DataResolver = require('../util/DataResolver');
const MessageEmbed = require('./MessageEmbed');
const MessageAttachment = require('./MessageAttachment'); const MessageAttachment = require('./MessageAttachment');
const { browser } = require('../util/Constants'); const MessageEmbed = require('./MessageEmbed');
const Util = require('../util/Util');
const { RangeError } = require('../errors'); const { RangeError } = require('../errors');
const { browser } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const MessageFlags = require('../util/MessageFlags'); const MessageFlags = require('../util/MessageFlags');
const Util = require('../util/Util');
/** /**
* Represents a message to be sent to the API. * Represents a message to be sent to the API.
@@ -78,7 +78,7 @@ class APIMessage {
* Makes the content of this message. * Makes the content of this message.
* @returns {?(string|string[])} * @returns {?(string|string[])}
*/ */
makeContent() { // eslint-disable-line complexity makeContent() {
const GuildMember = require('./GuildMember'); const GuildMember = require('./GuildMember');
let content; let content;
@@ -88,13 +88,14 @@ class APIMessage {
content = Util.resolveString(this.options.content); content = Util.resolveString(this.options.content);
} }
const disableMentions = typeof this.options.disableMentions === 'undefined' ? const disableMentions =
this.target.client.options.disableMentions : typeof this.options.disableMentions === 'undefined'
this.options.disableMentions; ? this.target.client.options.disableMentions
: this.options.disableMentions;
if (disableMentions === 'all') { if (disableMentions === 'all') {
content = Util.removeMentions(content || ''); content = Util.removeMentions(content || '');
} else if (disableMentions === 'everyone') { } else if (disableMentions === 'everyone') {
content = (content || '').replace(/@([^<>@ ]*)/gsmu, (match, target) => { content = (content || '').replace(/@([^<>@ ]*)/gmsu, (match, target) => {
if (target.match(/^[&!]?\d+$/)) { if (target.match(/^[&!]?\d+$/)) {
return `@${target}`; return `@${target}`;
} else { } else {
@@ -270,7 +271,8 @@ class APIMessage {
return 'file.jpg'; return 'file.jpg';
}; };
const ownAttachment = typeof fileLike === 'string' || const ownAttachment =
typeof fileLike === 'string' ||
fileLike instanceof (browser ? ArrayBuffer : Buffer) || fileLike instanceof (browser ? ArrayBuffer : Buffer) ||
typeof fileLike.pipe === 'function'; typeof fileLike.pipe === 'function';
if (ownAttachment) { if (ownAttachment) {

View File

@@ -20,7 +20,9 @@ class Base {
return Object.assign(Object.create(this), this); return Object.assign(Object.create(this), this);
} }
_patch(data) { return data; } _patch(data) {
return data;
}
_update(data) { _update(data) {
const clone = this._clone(); const clone = this._clone();

View File

@@ -1,8 +1,8 @@
'use strict'; 'use strict';
const Snowflake = require('../util/Snowflake');
const Base = require('./Base'); const Base = require('./Base');
const { ChannelTypes } = require('../util/Constants'); const { ChannelTypes } = require('../util/Constants');
const Snowflake = require('../util/Snowflake');
/** /**
* Represents any channel on Discord. * Represents any channel on Discord.
@@ -82,7 +82,10 @@ class Channel extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
delete() { delete() {
return this.client.api.channels(this.id).delete().then(() => this); return this.client.api
.channels(this.id)
.delete()
.then(() => this);
} }
/** /**

View File

@@ -1,9 +1,9 @@
'use strict'; 'use strict';
const Snowflake = require('../util/Snowflake');
const { ClientApplicationAssetTypes, Endpoints } = require('../util/Constants');
const Base = require('./Base'); const Base = require('./Base');
const Team = require('./Team'); const Team = require('./Team');
const { ClientApplicationAssetTypes, Endpoints } = require('../util/Constants');
const Snowflake = require('../util/Snowflake');
const AssetTypes = Object.keys(ClientApplicationAssetTypes); const AssetTypes = Object.keys(ClientApplicationAssetTypes);
@@ -70,11 +70,7 @@ class ClientApplication extends Base {
* The owner of this OAuth application * The owner of this OAuth application
* @type {?User|Team} * @type {?User|Team}
*/ */
this.owner = data.team ? this.owner = data.team ? new Team(this.client, data.team) : data.owner ? this.client.users.add(data.owner) : null;
new Team(this.client, data.team) :
data.owner ?
this.client.users.add(data.owner) :
null;
} }
/** /**
@@ -112,9 +108,7 @@ class ClientApplication extends Base {
*/ */
coverImage({ format, size } = {}) { coverImage({ format, size } = {}) {
if (!this.cover) return null; if (!this.cover) return null;
return Endpoints return Endpoints.CDN(this.client.options.http.cdn).AppIcon(this.id, this.cover, { format, size });
.CDN(this.client.options.http.cdn)
.AppIcon(this.id, this.cover, { format, size });
} }
/** /**
@@ -130,12 +124,16 @@ class ClientApplication extends Base {
* @returns {Promise<Array<ClientAsset>>} * @returns {Promise<Array<ClientAsset>>}
*/ */
fetchAssets() { fetchAssets() {
return this.client.api.oauth2.applications(this.id).assets.get() return this.client.api.oauth2
.then(assets => assets.map(a => ({ .applications(this.id)
id: a.id, .assets.get()
name: a.name, .then(assets =>
type: AssetTypes[a.type - 1], assets.map(a => ({
}))); id: a.id,
name: a.name,
type: AssetTypes[a.type - 1],
})),
);
} }
/** /**

View File

@@ -1,9 +1,9 @@
'use strict'; 'use strict';
const { Presence } = require('./Presence'); const { Presence } = require('./Presence');
const { TypeError } = require('../errors');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
const { ActivityTypes, OPCodes } = require('../util/Constants'); const { ActivityTypes, OPCodes } = require('../util/Constants');
const { TypeError } = require('../errors');
class ClientPresence extends Presence { class ClientPresence extends Presence {
/** /**
@@ -29,7 +29,7 @@ class ClientPresence extends Presence {
return this; return this;
} }
async _parse({ status, since, afk, activity }) { // eslint-disable-line complexity async _parse({ status, since, afk, activity }) {
const applicationID = activity && (activity.application ? activity.application.id || activity.application : null); const applicationID = activity && (activity.application ? activity.application.id || activity.application : null);
let assets = new Collection(); let assets = new Collection();
if (activity) { if (activity) {
@@ -47,24 +47,28 @@ class ClientPresence extends Presence {
afk: afk != null ? afk : false, // eslint-disable-line eqeqeq afk: afk != null ? afk : false, // eslint-disable-line eqeqeq
since: since != null ? since : null, // eslint-disable-line eqeqeq since: since != null ? since : null, // eslint-disable-line eqeqeq
status: status || this.status, status: status || this.status,
game: activity ? { game: activity
type: activity.type, ? {
name: activity.name, type: activity.type,
url: activity.url, name: activity.name,
details: activity.details || undefined, url: activity.url,
state: activity.state || undefined, details: activity.details || undefined,
assets: activity.assets ? { state: activity.state || undefined,
large_text: activity.assets.largeText || undefined, assets: activity.assets
small_text: activity.assets.smallText || undefined, ? {
large_image: assets.get(activity.assets.largeImage) || activity.assets.largeImage, large_text: activity.assets.largeText || undefined,
small_image: assets.get(activity.assets.smallImage) || activity.assets.smallImage, small_text: activity.assets.smallText || undefined,
} : undefined, large_image: assets.get(activity.assets.largeImage) || activity.assets.largeImage,
timestamps: activity.timestamps || undefined, small_image: assets.get(activity.assets.smallImage) || activity.assets.smallImage,
party: activity.party || undefined, }
application_id: applicationID || undefined, : undefined,
secrets: activity.secrets || undefined, timestamps: activity.timestamps || undefined,
instance: activity.instance || undefined, party: activity.party || undefined,
} : null, application_id: applicationID || undefined,
secrets: activity.secrets || undefined,
instance: activity.instance || undefined,
}
: null,
}; };
if ((status || afk || since) && !activity) { if ((status || afk || since) && !activity) {
@@ -72,8 +76,8 @@ class ClientPresence extends Presence {
} }
if (packet.game) { if (packet.game) {
packet.game.type = typeof packet.game.type === 'number' ? packet.game.type =
packet.game.type : ActivityTypes.indexOf(packet.game.type); typeof packet.game.type === 'number' ? packet.game.type : ActivityTypes.indexOf(packet.game.type);
} }
return packet; return packet;

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const Structures = require('../util/Structures');
const DataResolver = require('../util/DataResolver'); const DataResolver = require('../util/DataResolver');
const Structures = require('../util/Structures');
/** /**
* Represents the logged in client's Discord user. * Represents the logged in client's Discord user.
@@ -47,7 +47,9 @@ class ClientUser extends Structures.get('User') {
} }
edit(data) { edit(data) {
return this.client.api.users('@me').patch({ data }) return this.client.api
.users('@me')
.patch({ data })
.then(newData => { .then(newData => {
this.client.token = newData.token; this.client.token = newData.token;
const { updated } = this.client.actions.UserUpdate.handle(newData); const { updated } = this.client.actions.UserUpdate.handle(newData);

View File

@@ -1,8 +1,7 @@
'use strict'; 'use strict';
const Snowflake = require('../util/Snowflake');
const Base = require('./Base'); const Base = require('./Base');
const Snowflake = require('../util/Snowflake');
/** /**
* Represents an emoji, see {@link GuildEmoji} and {@link ReactionEmoji}. * Represents an emoji, see {@link GuildEmoji} and {@link ReactionEmoji}.

View File

@@ -1,10 +1,18 @@
'use strict'; 'use strict';
const Invite = require('./Invite'); const Base = require('./Base');
const Integration = require('./Integration');
const GuildAuditLogs = require('./GuildAuditLogs'); const GuildAuditLogs = require('./GuildAuditLogs');
const Webhook = require('./Webhook'); const Integration = require('./Integration');
const Invite = require('./Invite');
const VoiceRegion = require('./VoiceRegion'); const VoiceRegion = require('./VoiceRegion');
const Webhook = require('./Webhook');
const GuildChannelManager = require('../src/managers/GuildChannelManager');
const GuildEmojiManager = require('../src/managers/GuildEmojiManager');
const GuildMemberManager = require('../src/managers/GuildMemberManager');
const PresenceManager = require('../src/managers/PresenceManager');
const RoleManager = require('../src/managers/RoleManager');
const VoiceStateManager = require('../src/managers/VoiceStateManager');
const Collection = require('../util/Collection');
const { const {
ChannelTypes, ChannelTypes,
DefaultMessageNotifications, DefaultMessageNotifications,
@@ -12,19 +20,10 @@ const {
VerificationLevels, VerificationLevels,
ExplicitContentFilterLevels, ExplicitContentFilterLevels,
} = require('../util/Constants'); } = require('../util/Constants');
const Collection = require('../util/Collection');
const Util = require('../util/Util');
const DataResolver = require('../util/DataResolver'); const DataResolver = require('../util/DataResolver');
const Snowflake = require('../util/Snowflake'); const Snowflake = require('../util/Snowflake');
const SystemChannelFlags = require('../util/SystemChannelFlags'); const SystemChannelFlags = require('../util/SystemChannelFlags');
const GuildMemberManager = require('../managers/GuildMemberManager'); const Util = require('../util/Util');
const RoleManager = require('../managers/RoleManager');
const GuildEmojiManager = require('../managers/GuildEmojiManager');
const GuildChannelManager = require('../managers/GuildChannelManager');
const PresenceManager = require('../managers/PresenceManager');
const VoiceStateManager = require('../managers/VoiceStateManager');
const Base = require('./Base');
const { Error, TypeError } = require('../errors');
/** /**
* Represents a guild (or a server) on Discord. * Represents a guild (or a server) on Discord.
@@ -110,7 +109,6 @@ class Guild extends Base {
return this.client.ws.shards.get(this.shardID); return this.client.ws.shards.get(this.shardID);
} }
/* eslint-disable complexity */
/** /**
* Sets up the guild. * Sets up the guild.
* @param {*} data The raw data of the guild * @param {*} data The raw data of the guild
@@ -279,8 +277,8 @@ class Guild extends Base {
* The value set for the guild's default message notifications * The value set for the guild's default message notifications
* @type {DefaultMessageNotifications|number} * @type {DefaultMessageNotifications|number}
*/ */
this.defaultMessageNotifications = DefaultMessageNotifications[data.default_message_notifications] || this.defaultMessageNotifications =
data.default_message_notifications; DefaultMessageNotifications[data.default_message_notifications] || data.default_message_notifications;
/** /**
* The value set for the guild's system channel flags * The value set for the guild's system channel flags
@@ -483,9 +481,12 @@ class Guild extends Base {
* @readonly * @readonly
*/ */
get owner() { get owner() {
return this.members.cache.get(this.ownerID) || (this.client.options.partials.includes(PartialTypes.GUILD_MEMBER) ? return (
this.members.add({ user: { id: this.ownerID } }, true) : this.members.cache.get(this.ownerID) ||
null); (this.client.options.partials.includes(PartialTypes.GUILD_MEMBER)
? this.members.add({ user: { id: this.ownerID } }, true)
: null)
);
} }
/** /**
@@ -550,10 +551,12 @@ class Guild extends Base {
* @readonly * @readonly
*/ */
get me() { get me() {
return this.members.cache.get(this.client.user.id) || return (
(this.client.options.partials.includes(PartialTypes.GUILD_MEMBER) ? this.members.cache.get(this.client.user.id) ||
this.members.add({ user: { id: this.client.user.id } }, true) : (this.client.options.partials.includes(PartialTypes.GUILD_MEMBER)
null); ? this.members.add({ user: { id: this.client.user.id } }, true)
: null)
);
} }
/** /**
@@ -582,10 +585,13 @@ class Guild extends Base {
* @returns {Promise<Guild>} * @returns {Promise<Guild>}
*/ */
fetch() { fetch() {
return this.client.api.guilds(this.id).get().then(data => { return this.client.api
this._patch(data); .guilds(this.id)
return this; .get()
}); .then(data => {
this._patch(data);
return this;
});
} }
/** /**
@@ -596,14 +602,17 @@ class Guild extends Base {
*/ */
/** /**
* Fetches information on a banned user from this guild. * Fetches information on a banned user from this guild.
* @param {UserResolvable} user The User to fetch the ban info of * @param {UserResolvable} user The User to fetch the ban info of
* @returns {Promise<BanInfo>} * @returns {Promise<BanInfo>}
*/ */
fetchBan(user) { fetchBan(user) {
const id = this.client.users.resolveID(user); const id = this.client.users.resolveID(user);
if (!id) throw new Error('FETCH_BAN_RESOLVE_ID'); if (!id) throw new Error('FETCH_BAN_RESOLVE_ID');
return this.client.api.guilds(this.id).bans(id).get() return this.client.api
.guilds(this.id)
.bans(id)
.get()
.then(ban => ({ .then(ban => ({
reason: ban.reason, reason: ban.reason,
user: this.client.users.add(ban.user), user: this.client.users.add(ban.user),
@@ -615,15 +624,18 @@ class Guild extends Base {
* @returns {Promise<Collection<Snowflake, BanInfo>>} * @returns {Promise<Collection<Snowflake, BanInfo>>}
*/ */
fetchBans() { fetchBans() {
return this.client.api.guilds(this.id).bans.get().then(bans => return this.client.api
bans.reduce((collection, ban) => { .guilds(this.id)
collection.set(ban.user.id, { .bans.get()
reason: ban.reason, .then(bans =>
user: this.client.users.add(ban.user), bans.reduce((collection, ban) => {
}); collection.set(ban.user.id, {
return collection; reason: ban.reason,
}, new Collection()), user: this.client.users.add(ban.user),
); });
return collection;
}, new Collection()),
);
} }
/** /**
@@ -637,11 +649,15 @@ class Guild extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
fetchIntegrations() { fetchIntegrations() {
return this.client.api.guilds(this.id).integrations.get().then(data => return this.client.api
data.reduce((collection, integration) => .guilds(this.id)
collection.set(integration.id, new Integration(this.client, integration, this)), .integrations.get()
new Collection()), .then(data =>
); data.reduce(
(collection, integration) => collection.set(integration.id, new Integration(this.client, integration, this)),
new Collection(),
),
);
} }
/** /**
@@ -658,7 +674,9 @@ class Guild extends Base {
* @returns {Promise<Guild>} * @returns {Promise<Guild>}
*/ */
createIntegration(data, reason) { createIntegration(data, reason) {
return this.client.api.guilds(this.id).integrations.post({ data, reason }) return this.client.api
.guilds(this.id)
.integrations.post({ data, reason })
.then(() => this); .then(() => this);
} }
@@ -678,7 +696,9 @@ class Guild extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
fetchInvites() { fetchInvites() {
return this.client.api.guilds(this.id).invites.get() return this.client.api
.guilds(this.id)
.invites.get()
.then(inviteItems => { .then(inviteItems => {
const invites = new Collection(); const invites = new Collection();
for (const inviteItem of inviteItems) { for (const inviteItem of inviteItems) {
@@ -705,7 +725,9 @@ class Guild extends Base {
if (!this.features.includes('VANITY_URL')) { if (!this.features.includes('VANITY_URL')) {
return Promise.reject(new Error('VANITY_URL')); return Promise.reject(new Error('VANITY_URL'));
} }
return this.client.api.guilds(this.id, 'vanity-url').get() return this.client.api
.guilds(this.id, 'vanity-url')
.get()
.then(res => res.code); .then(res => res.code);
} }
@@ -719,11 +741,14 @@ class Guild extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
fetchWebhooks() { fetchWebhooks() {
return this.client.api.guilds(this.id).webhooks.get().then(data => { return this.client.api
const hooks = new Collection(); .guilds(this.id)
for (const hook of data) hooks.set(hook.id, new Webhook(this.client, hook)); .webhooks.get()
return hooks; .then(data => {
}); const hooks = new Collection();
for (const hook of data) hooks.set(hook.id, new Webhook(this.client, hook));
return hooks;
});
} }
/** /**
@@ -731,11 +756,14 @@ class Guild extends Base {
* @returns {Promise<Collection<string, VoiceRegion>>} * @returns {Promise<Collection<string, VoiceRegion>>}
*/ */
fetchVoiceRegions() { fetchVoiceRegions() {
return this.client.api.guilds(this.id).regions.get().then(res => { return this.client.api
const regions = new Collection(); .guilds(this.id)
for (const region of res) regions.set(region.id, new VoiceRegion(region)); .regions.get()
return regions; .then(res => {
}); const regions = new Collection();
for (const region of res) regions.set(region.id, new VoiceRegion(region));
return regions;
});
} }
/** /**
@@ -755,10 +783,13 @@ class Guild extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
fetchEmbed() { fetchEmbed() {
return this.client.api.guilds(this.id).embed.get().then(data => ({ return this.client.api
enabled: data.enabled, .guilds(this.id)
channel: data.channel_id ? this.channels.cache.get(data.channel_id) : null, .embed.get()
})); .then(data => ({
enabled: data.enabled,
channel: data.channel_id ? this.channels.cache.get(data.channel_id) : null,
}));
} }
/** /**
@@ -779,12 +810,16 @@ class Guild extends Base {
if (options.before && options.before instanceof GuildAuditLogs.Entry) options.before = options.before.id; if (options.before && options.before instanceof GuildAuditLogs.Entry) options.before = options.before.id;
if (typeof options.type === 'string') options.type = GuildAuditLogs.Actions[options.type]; if (typeof options.type === 'string') options.type = GuildAuditLogs.Actions[options.type];
return this.client.api.guilds(this.id)['audit-logs'].get({ query: { return this.client.api
before: options.before, .guilds(this.id)
limit: options.limit, ['audit-logs'].get({
user_id: this.client.users.resolveID(options.user), query: {
action_type: options.type, before: options.before,
} }) limit: options.limit,
user_id: this.client.users.resolveID(options.user),
action_type: options.type,
},
})
.then(data => GuildAuditLogs.build(this, data)); .then(data => GuildAuditLogs.build(this, data));
} }
@@ -811,14 +846,18 @@ class Guild extends Base {
for (let role of options.roles instanceof Collection ? options.roles.values() : options.roles) { for (let role of options.roles instanceof Collection ? options.roles.values() : options.roles) {
role = this.roles.resolve(role); role = this.roles.resolve(role);
if (!role) { if (!role) {
return Promise.reject(new TypeError('INVALID_TYPE', 'options.roles', return Promise.reject(
'Array or Collection of Roles or Snowflakes', true)); new TypeError('INVALID_TYPE', 'options.roles', 'Array or Collection of Roles or Snowflakes', true),
);
} }
roles.push(role.id); roles.push(role.id);
} }
options.roles = roles; options.roles = roles;
} }
return this.client.api.guilds(this.id).members(user).put({ data: options }) return this.client.api
.guilds(this.id)
.members(user)
.put({ data: options })
.then(data => this.members.add(data)); .then(data => this.members.add(data));
} }
@@ -859,9 +898,10 @@ class Guild extends Base {
if (data.name) _data.name = data.name; if (data.name) _data.name = data.name;
if (data.region) _data.region = data.region; if (data.region) _data.region = data.region;
if (typeof data.verificationLevel !== 'undefined') { if (typeof data.verificationLevel !== 'undefined') {
_data.verification_level = typeof data.verificationLevel === 'number' ? _data.verification_level =
Number(data.verificationLevel) : typeof data.verificationLevel === 'number'
ExplicitContentFilterLevels.indexOf(data.verificationLevel); ? Number(data.verificationLevel)
: ExplicitContentFilterLevels.indexOf(data.verificationLevel);
} }
if (typeof data.afkChannel !== 'undefined') { if (typeof data.afkChannel !== 'undefined') {
_data.afk_channel_id = this.client.channels.resolveID(data.afkChannel); _data.afk_channel_id = this.client.channels.resolveID(data.afkChannel);
@@ -875,19 +915,23 @@ class Guild extends Base {
if (data.splash) _data.splash = data.splash; if (data.splash) _data.splash = data.splash;
if (data.banner) _data.banner = data.banner; if (data.banner) _data.banner = data.banner;
if (typeof data.explicitContentFilter !== 'undefined') { if (typeof data.explicitContentFilter !== 'undefined') {
_data.explicit_content_filter = typeof data.explicitContentFilter === 'number' ? _data.explicit_content_filter =
data.explicitContentFilter : typeof data.explicitContentFilter === 'number'
ExplicitContentFilterLevels.indexOf(data.explicitContentFilter); ? data.explicitContentFilter
: ExplicitContentFilterLevels.indexOf(data.explicitContentFilter);
} }
if (typeof data.defaultMessageNotifications !== 'undefined') { if (typeof data.defaultMessageNotifications !== 'undefined') {
_data.default_message_notifications = typeof data.defaultMessageNotifications === 'string' ? _data.default_message_notifications =
DefaultMessageNotifications.indexOf(data.defaultMessageNotifications) : typeof data.defaultMessageNotifications === 'string'
data.defaultMessageNotifications; ? DefaultMessageNotifications.indexOf(data.defaultMessageNotifications)
: data.defaultMessageNotifications;
} }
if (typeof data.systemChannelFlags !== 'undefined') { if (typeof data.systemChannelFlags !== 'undefined') {
_data.system_channel_flags = SystemChannelFlags.resolve(data.systemChannelFlags); _data.system_channel_flags = SystemChannelFlags.resolve(data.systemChannelFlags);
} }
return this.client.api.guilds(this.id).patch({ data: _data, reason }) return this.client.api
.guilds(this.id)
.patch({ data: _data, reason })
.then(newData => this.client.actions.GuildUpdate.handle(newData).updated); .then(newData => this.client.actions.GuildUpdate.handle(newData).updated);
} }
@@ -1094,12 +1138,16 @@ class Guild extends Base {
position: r.position, position: r.position,
})); }));
return this.client.api.guilds(this.id).channels.patch({ data: updatedChannels }).then(() => return this.client.api
this.client.actions.GuildChannelsPositionUpdate.handle({ .guilds(this.id)
guild_id: this.id, .channels.patch({ data: updatedChannels })
channels: updatedChannels, .then(
}).guild, () =>
); this.client.actions.GuildChannelsPositionUpdate.handle({
guild_id: this.id,
channels: updatedChannels,
}).guild,
);
} }
/** /**
@@ -1126,14 +1174,18 @@ class Guild extends Base {
})); }));
// Call the API to update role positions // Call the API to update role positions
return this.client.api.guilds(this.id).roles.patch({ return this.client.api
data: rolePositions, .guilds(this.id)
}).then(() => .roles.patch({
this.client.actions.GuildRolePositionUpdate.handle({ data: rolePositions,
guild_id: this.id, })
roles: rolePositions, .then(
}).guild, () =>
); this.client.actions.GuildRolePositionUpdate.handle({
guild_id: this.id,
roles: rolePositions,
}).guild,
);
} }
/** /**
@@ -1143,13 +1195,16 @@ class Guild extends Base {
* @returns {Promise<Guild>} * @returns {Promise<Guild>}
*/ */
setEmbed(embed, reason) { setEmbed(embed, reason) {
return this.client.api.guilds(this.id).embed.patch({ return this.client.api
data: { .guilds(this.id)
enabled: embed.enabled, .embed.patch({
channel_id: this.channels.resolveID(embed.channel), data: {
}, enabled: embed.enabled,
reason, channel_id: this.channels.resolveID(embed.channel),
}).then(() => this); },
reason,
})
.then(() => this);
} }
/** /**
@@ -1163,7 +1218,10 @@ class Guild extends Base {
*/ */
leave() { leave() {
if (this.ownerID === this.client.user.id) return Promise.reject(new Error('GUILD_OWNED')); if (this.ownerID === this.client.user.id) return Promise.reject(new Error('GUILD_OWNED'));
return this.client.api.users('@me').guilds(this.id).delete() return this.client.api
.users('@me')
.guilds(this.id)
.delete()
.then(() => this.client.actions.GuildDelete.handle({ id: this.id }).guild); .then(() => this.client.actions.GuildDelete.handle({ id: this.id }).guild);
} }
@@ -1177,7 +1235,9 @@ class Guild extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
delete() { delete() {
return this.client.api.guilds(this.id).delete() return this.client.api
.guilds(this.id)
.delete()
.then(() => this.client.actions.GuildDelete.handle({ id: this.id }).guild); .then(() => this.client.actions.GuildDelete.handle({ id: this.id }).guild);
} }
@@ -1203,10 +1263,9 @@ class Guild extends Base {
this.ownerID === guild.ownerID && this.ownerID === guild.ownerID &&
this.verificationLevel === guild.verificationLevel && this.verificationLevel === guild.verificationLevel &&
this.embedEnabled === guild.embedEnabled && this.embedEnabled === guild.embedEnabled &&
(this.features === guild.features || ( (this.features === guild.features ||
this.features.length === guild.features.length && (this.features.length === guild.features.length &&
this.features.every((feat, i) => feat === guild.features[i])) this.features.every((feat, i) => feat === guild.features[i])));
);
if (equal) { if (equal) {
if (this.embedChannel) { if (this.embedChannel) {
@@ -1261,9 +1320,9 @@ class Guild extends Base {
*/ */
_sortedChannels(channel) { _sortedChannels(channel) {
const category = channel.type === ChannelTypes.CATEGORY; const category = channel.type === ChannelTypes.CATEGORY;
return Util.discordSort(this.channels.cache.filter(c => return Util.discordSort(
c.type === channel.type && (category || c.parent === channel.parent), this.channels.cache.filter(c => c.type === channel.type && (category || c.parent === channel.parent)),
)); );
} }
} }

View File

@@ -1,11 +1,11 @@
'use strict'; 'use strict';
const Collection = require('../util/Collection');
const Integration = require('./Integration'); const Integration = require('./Integration');
const Snowflake = require('../util/Snowflake');
const Webhook = require('./Webhook'); const Webhook = require('./Webhook');
const Util = require('../util/Util'); const Collection = require('../util/Collection');
const { PartialTypes } = require('../util/Constants'); const { PartialTypes } = require('../util/Constants');
const Snowflake = require('../util/Snowflake');
const Util = require('../util/Util');
/** /**
* The target type of an entry, e.g. `GUILD`. Here are the available types: * The target type of an entry, e.g. `GUILD`. Here are the available types:
@@ -125,7 +125,6 @@ const Actions = {
INTEGRATION_DELETE: 82, INTEGRATION_DELETE: 82,
}; };
/** /**
* Audit logs entries are held in this class. * Audit logs entries are held in this class.
*/ */
@@ -223,49 +222,61 @@ class GuildAuditLogs {
* @returns {AuditLogActionType} * @returns {AuditLogActionType}
*/ */
static actionType(action) { static actionType(action) {
if ([ if (
Actions.CHANNEL_CREATE, [
Actions.CHANNEL_OVERWRITE_CREATE, Actions.CHANNEL_CREATE,
Actions.MEMBER_BAN_REMOVE, Actions.CHANNEL_OVERWRITE_CREATE,
Actions.BOT_ADD, Actions.MEMBER_BAN_REMOVE,
Actions.ROLE_CREATE, Actions.BOT_ADD,
Actions.INVITE_CREATE, Actions.ROLE_CREATE,
Actions.WEBHOOK_CREATE, Actions.INVITE_CREATE,
Actions.EMOJI_CREATE, Actions.WEBHOOK_CREATE,
Actions.MESSAGE_PIN, Actions.EMOJI_CREATE,
Actions.INTEGRATION_CREATE, Actions.MESSAGE_PIN,
].includes(action)) return 'CREATE'; Actions.INTEGRATION_CREATE,
].includes(action)
) {
return 'CREATE';
}
if ([ if (
Actions.CHANNEL_DELETE, [
Actions.CHANNEL_OVERWRITE_DELETE, Actions.CHANNEL_DELETE,
Actions.MEMBER_KICK, Actions.CHANNEL_OVERWRITE_DELETE,
Actions.MEMBER_PRUNE, Actions.MEMBER_KICK,
Actions.MEMBER_BAN_ADD, Actions.MEMBER_PRUNE,
Actions.MEMBER_DISCONNECT, Actions.MEMBER_BAN_ADD,
Actions.ROLE_DELETE, Actions.MEMBER_DISCONNECT,
Actions.INVITE_DELETE, Actions.ROLE_DELETE,
Actions.WEBHOOK_DELETE, Actions.INVITE_DELETE,
Actions.EMOJI_DELETE, Actions.WEBHOOK_DELETE,
Actions.MESSAGE_DELETE, Actions.EMOJI_DELETE,
Actions.MESSAGE_BULK_DELETE, Actions.MESSAGE_DELETE,
Actions.MESSAGE_UNPIN, Actions.MESSAGE_BULK_DELETE,
Actions.INTEGRATION_DELETE, Actions.MESSAGE_UNPIN,
].includes(action)) return 'DELETE'; Actions.INTEGRATION_DELETE,
].includes(action)
) {
return 'DELETE';
}
if ([ if (
Actions.GUILD_UPDATE, [
Actions.CHANNEL_UPDATE, Actions.GUILD_UPDATE,
Actions.CHANNEL_OVERWRITE_UPDATE, Actions.CHANNEL_UPDATE,
Actions.MEMBER_UPDATE, Actions.CHANNEL_OVERWRITE_UPDATE,
Actions.MEMBER_ROLE_UPDATE, Actions.MEMBER_UPDATE,
Actions.MEMBER_MOVE, Actions.MEMBER_ROLE_UPDATE,
Actions.ROLE_UPDATE, Actions.MEMBER_MOVE,
Actions.INVITE_UPDATE, Actions.ROLE_UPDATE,
Actions.WEBHOOK_UPDATE, Actions.INVITE_UPDATE,
Actions.EMOJI_UPDATE, Actions.WEBHOOK_UPDATE,
Actions.INTEGRATION_UPDATE, Actions.EMOJI_UPDATE,
].includes(action)) return 'UPDATE'; Actions.INTEGRATION_UPDATE,
].includes(action)
) {
return 'UPDATE';
}
return 'ALL'; return 'ALL';
} }
@@ -279,7 +290,7 @@ class GuildAuditLogs {
* Audit logs entry. * Audit logs entry.
*/ */
class GuildAuditLogsEntry { class GuildAuditLogsEntry {
constructor(logs, guild, data) { // eslint-disable-line complexity constructor(logs, guild, data) {
const targetType = GuildAuditLogs.targetType(data.action_type); const targetType = GuildAuditLogs.targetType(data.action_type);
/** /**
* The target type of this entry * The target type of this entry
@@ -309,9 +320,9 @@ class GuildAuditLogsEntry {
* The user that executed this entry * The user that executed this entry
* @type {User} * @type {User}
*/ */
this.executor = guild.client.options.partials.includes(PartialTypes.USER) ? this.executor = guild.client.options.partials.includes(PartialTypes.USER)
guild.client.users.add({ id: data.user_id }) : ? guild.client.users.add({ id: data.user_id })
guild.client.users.cache.get(data.user_id); : guild.client.users.cache.get(data.user_id);
/** /**
* An entry in the audit log representing a specific change. * An entry in the audit log representing a specific change.
@@ -374,13 +385,15 @@ class GuildAuditLogsEntry {
case Actions.CHANNEL_OVERWRITE_DELETE: case Actions.CHANNEL_OVERWRITE_DELETE:
switch (data.options.type) { switch (data.options.type) {
case 'member': case 'member':
this.extra = guild.members.cache.get(data.options.id) || this.extra = guild.members.cache.get(data.options.id) || { id: data.options.id, type: 'member' };
{ id: data.options.id, type: 'member' };
break; break;
case 'role': case 'role':
this.extra = guild.roles.cache.get(data.options.id) || this.extra = guild.roles.cache.get(data.options.id) || {
{ id: data.options.id, name: data.options.role_name, type: 'role' }; id: data.options.id,
name: data.options.role_name,
type: 'role',
};
break; break;
default: default:
@@ -405,21 +418,27 @@ class GuildAuditLogsEntry {
this.target.id = data.target_id; this.target.id = data.target_id;
// MEMBER_DISCONNECT and similar types do not provide a target_id. // MEMBER_DISCONNECT and similar types do not provide a target_id.
} else if (targetType === Targets.USER && data.target_id) { } else if (targetType === Targets.USER && data.target_id) {
this.target = guild.client.options.partials.includes(PartialTypes.USER) ? this.target = guild.client.options.partials.includes(PartialTypes.USER)
guild.client.users.add({ id: data.target_id }) : ? guild.client.users.add({ id: data.target_id })
guild.client.users.cache.get(data.target_id); : guild.client.users.cache.get(data.target_id);
} else if (targetType === Targets.GUILD) { } else if (targetType === Targets.GUILD) {
this.target = guild.client.guilds.cache.get(data.target_id); this.target = guild.client.guilds.cache.get(data.target_id);
} else if (targetType === Targets.WEBHOOK) { } else if (targetType === Targets.WEBHOOK) {
this.target = logs.webhooks.get(data.target_id) || this.target =
new Webhook(guild.client, logs.webhooks.get(data.target_id) ||
this.changes.reduce((o, c) => { new Webhook(
o[c.key] = c.new || c.old; guild.client,
return o; this.changes.reduce(
}, { (o, c) => {
id: data.target_id, o[c.key] = c.new || c.old;
guild_id: guild.id, return o;
})); },
{
id: data.target_id,
guild_id: guild.id,
},
),
);
} else if (targetType === Targets.INVITE) { } else if (targetType === Targets.INVITE) {
this.target = guild.members.fetch(guild.client.user.id).then(me => { this.target = guild.members.fetch(guild.client.user.id).then(me => {
if (me.permissions.has('MANAGE_GUILD')) { if (me.permissions.has('MANAGE_GUILD')) {
@@ -437,15 +456,24 @@ class GuildAuditLogsEntry {
}); });
} else if (targetType === Targets.MESSAGE) { } else if (targetType === Targets.MESSAGE) {
// Discord sends a channel id for the MESSAGE_BULK_DELETE action type. // Discord sends a channel id for the MESSAGE_BULK_DELETE action type.
this.target = data.action_type === Actions.MESSAGE_BULK_DELETE ? this.target =
guild.channels.cache.get(data.target_id) || { id: data.target_id } : data.action_type === Actions.MESSAGE_BULK_DELETE
guild.client.users.cache.get(data.target_id); ? guild.channels.cache.get(data.target_id) || { id: data.target_id }
: guild.client.users.cache.get(data.target_id);
} else if (targetType === Targets.INTEGRATION) { } else if (targetType === Targets.INTEGRATION) {
this.target = logs.integrations.get(data.target_id) || this.target =
new Integration(guild.client, this.changes.reduce((o, c) => { logs.integrations.get(data.target_id) ||
o[c.key] = c.new || c.old; new Integration(
return o; guild.client,
}, { id: data.target_id }), guild); this.changes.reduce(
(o, c) => {
o[c.key] = c.new || c.old;
return o;
},
{ id: data.target_id },
),
guild,
);
} else if (data.target_id) { } else if (data.target_id) {
this.target = guild[`${targetType.toLowerCase()}s`].cache.get(data.target_id) || { id: data.target_id }; this.target = guild[`${targetType.toLowerCase()}s`].cache.get(data.target_id) || { id: data.target_id };
} }

View File

@@ -1,13 +1,13 @@
'use strict'; 'use strict';
const Channel = require('./Channel'); const Channel = require('./Channel');
const Role = require('./Role');
const Invite = require('./Invite'); const Invite = require('./Invite');
const PermissionOverwrites = require('./PermissionOverwrites'); const PermissionOverwrites = require('./PermissionOverwrites');
const Util = require('../util/Util'); const Role = require('./Role');
const Permissions = require('../util/Permissions');
const Collection = require('../util/Collection');
const { Error, TypeError } = require('../errors'); const { Error, TypeError } = require('../errors');
const Collection = require('../util/Collection');
const Permissions = require('../util/Permissions');
const Util = require('../util/Util');
/** /**
* Represents a guild channel from any of the following: * Represents a guild channel from any of the following:
@@ -85,9 +85,11 @@ class GuildChannel extends Channel {
if (this.permissionOverwrites.size !== this.parent.permissionOverwrites.size) return false; if (this.permissionOverwrites.size !== this.parent.permissionOverwrites.size) return false;
return this.permissionOverwrites.every((value, key) => { return this.permissionOverwrites.every((value, key) => {
const testVal = this.parent.permissionOverwrites.get(key); const testVal = this.parent.permissionOverwrites.get(key);
return testVal !== undefined && return (
testVal !== undefined &&
testVal.deny.bitfield === value.deny.bitfield && testVal.deny.bitfield === value.deny.bitfield &&
testVal.allow.bitfield === value.allow.bitfield; testVal.allow.bitfield === value.allow.bitfield
);
}); });
} }
@@ -202,12 +204,9 @@ class GuildChannel extends Channel {
*/ */
overwritePermissions(overwrites, reason) { overwritePermissions(overwrites, reason) {
if (!Array.isArray(overwrites) && !(overwrites instanceof Collection)) { if (!Array.isArray(overwrites) && !(overwrites instanceof Collection)) {
return Promise.reject(new TypeError( return Promise.reject(
'INVALID_TYPE', new TypeError('INVALID_TYPE', 'overwrites', 'Array or Collection of Permission Overwrites', true),
'overwrites', );
'Array or Collection of Permission Overwrites',
true,
));
} }
return this.edit({ permissionOverwrites: overwrites, reason }).then(() => this); return this.edit({ permissionOverwrites: overwrites, reason }).then(() => this);
} }
@@ -256,8 +255,12 @@ class GuildChannel extends Channel {
const type = userOrRole instanceof Role ? 'role' : 'member'; const type = userOrRole instanceof Role ? 'role' : 'member';
const { allow, deny } = PermissionOverwrites.resolveOverwriteOptions(options); const { allow, deny } = PermissionOverwrites.resolveOverwriteOptions(options);
return this.client.api.channels(this.id).permissions[userOrRole.id] return this.client.api
.put({ data: { id: userOrRole.id, type, allow: allow.bitfield, deny: deny.bitfield }, reason }) .channels(this.id)
.permissions[userOrRole.id].put({
data: { id: userOrRole.id, type, allow: allow.bitfield, deny: deny.bitfield },
reason,
})
.then(() => this); .then(() => this);
} }
@@ -316,18 +319,23 @@ class GuildChannel extends Channel {
*/ */
async edit(data, reason) { async edit(data, reason) {
if (typeof data.position !== 'undefined') { if (typeof data.position !== 'undefined') {
await Util.setPosition(this, data.position, false, await Util.setPosition(
this.guild._sortedChannels(this), this.client.api.guilds(this.guild.id).channels, reason) this,
.then(updatedChannels => { data.position,
this.client.actions.GuildChannelsPositionUpdate.handle({ false,
guild_id: this.guild.id, this.guild._sortedChannels(this),
channels: updatedChannels, this.client.api.guilds(this.guild.id).channels,
}); reason,
).then(updatedChannels => {
this.client.actions.GuildChannelsPositionUpdate.handle({
guild_id: this.guild.id,
channels: updatedChannels,
}); });
});
} }
const permission_overwrites = data.permissionOverwrites && const permission_overwrites =
data.permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild)); data.permissionOverwrites && data.permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild));
const newData = await this.client.api.channels(this.id).patch({ const newData = await this.client.api.channels(this.id).patch({
data: { data: {
@@ -378,11 +386,14 @@ class GuildChannel extends Channel {
* .catch(console.error); * .catch(console.error);
*/ */
setParent(channel, { lockPermissions = true, reason } = {}) { setParent(channel, { lockPermissions = true, reason } = {}) {
return this.edit({ return this.edit(
// eslint-disable-next-line no-prototype-builtins {
parentID: channel !== null ? channel.hasOwnProperty('id') ? channel.id : channel : null, // eslint-disable-next-line no-prototype-builtins
lockPermissions, parentID: channel !== null ? (channel.hasOwnProperty('id') ? channel.id : channel) : null,
}, reason); lockPermissions,
},
reason,
);
} }
/** /**
@@ -414,15 +425,20 @@ class GuildChannel extends Channel {
* .catch(console.error); * .catch(console.error);
*/ */
setPosition(position, { relative, reason } = {}) { setPosition(position, { relative, reason } = {}) {
return Util.setPosition(this, position, relative, return Util.setPosition(
this.guild._sortedChannels(this), this.client.api.guilds(this.guild.id).channels, reason) this,
.then(updatedChannels => { position,
this.client.actions.GuildChannelsPositionUpdate.handle({ relative,
guild_id: this.guild.id, this.guild._sortedChannels(this),
channels: updatedChannels, this.client.api.guilds(this.guild.id).channels,
}); reason,
return this; ).then(updatedChannels => {
this.client.actions.GuildChannelsPositionUpdate.handle({
guild_id: this.guild.id,
channels: updatedChannels,
}); });
return this;
});
} }
/** /**
@@ -442,9 +458,17 @@ class GuildChannel extends Channel {
* .catch(console.error); * .catch(console.error);
*/ */
createInvite({ temporary = false, maxAge = 86400, maxUses = 0, unique, reason } = {}) { createInvite({ temporary = false, maxAge = 86400, maxUses = 0, unique, reason } = {}) {
return this.client.api.channels(this.id).invites.post({ data: { return this.client.api
temporary, max_age: maxAge, max_uses: maxUses, unique, .channels(this.id)
}, reason }) .invites.post({
data: {
temporary,
max_age: maxAge,
max_uses: maxUses,
unique,
},
reason,
})
.then(invite => new Invite(this.client, invite)); .then(invite => new Invite(this.client, invite));
} }
@@ -481,18 +505,21 @@ class GuildChannel extends Channel {
* @returns {Promise<GuildChannel>} * @returns {Promise<GuildChannel>}
*/ */
clone(options = {}) { clone(options = {}) {
Util.mergeDefault({ Util.mergeDefault(
name: this.name, {
permissionOverwrites: this.permissionOverwrites, name: this.name,
topic: this.topic, permissionOverwrites: this.permissionOverwrites,
type: this.type, topic: this.topic,
nsfw: this.nsfw, type: this.type,
parent: this.parent, nsfw: this.nsfw,
bitrate: this.bitrate, parent: this.parent,
userLimit: this.userLimit, bitrate: this.bitrate,
rateLimitPerUser: this.rateLimitPerUser, userLimit: this.userLimit,
reason: null, rateLimitPerUser: this.rateLimitPerUser,
}, options); reason: null,
},
options,
);
return this.guild.channels.create(options.name, options); return this.guild.channels.create(options.name, options);
} }
/* eslint-enable max-len */ /* eslint-enable max-len */
@@ -504,7 +531,8 @@ class GuildChannel extends Channel {
* @returns {boolean} * @returns {boolean}
*/ */
equals(channel) { equals(channel) {
let equal = channel && let equal =
channel &&
this.id === channel.id && this.id === channel.id &&
this.type === channel.type && this.type === channel.type &&
this.topic === channel.topic && this.topic === channel.topic &&
@@ -565,7 +593,10 @@ class GuildChannel extends Channel {
* .catch(console.error); * .catch(console.error);
*/ */
delete(reason) { delete(reason) {
return this.client.api.channels(this.id).delete({ reason }).then(() => this); return this.client.api
.channels(this.id)
.delete({ reason })
.then(() => this);
} }
} }

View File

@@ -1,9 +1,9 @@
'use strict'; 'use strict';
const Emoji = require('./Emoji');
const { Error } = require('../errors');
const GuildEmojiRoleManager = require('../managers/GuildEmojiRoleManager'); const GuildEmojiRoleManager = require('../managers/GuildEmojiRoleManager');
const Permissions = require('../util/Permissions'); const Permissions = require('../util/Permissions');
const { Error } = require('../errors');
const Emoji = require('./Emoji');
/** /**
* Represents a custom emoji. * Represents a custom emoji.
@@ -74,8 +74,7 @@ class GuildEmoji extends Emoji {
*/ */
get deletable() { get deletable() {
if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME'); if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME');
return !this.managed && return !this.managed && this.guild.me.hasPermission(Permissions.FLAGS.MANAGE_EMOJIS);
this.guild.me.hasPermission(Permissions.FLAGS.MANAGE_EMOJIS);
} }
/** /**
@@ -100,7 +99,10 @@ class GuildEmoji extends Emoji {
return Promise.reject(new Error('MISSING_MANAGE_EMOJIS_PERMISSION', this.guild)); return Promise.reject(new Error('MISSING_MANAGE_EMOJIS_PERMISSION', this.guild));
} }
} }
return this.client.api.guilds(this.guild.id).emojis(this.id).get() return this.client.api
.guilds(this.guild.id)
.emojis(this.id)
.get()
.then(emoji => this.client.users.add(emoji.user)); .then(emoji => this.client.users.add(emoji.user));
} }
@@ -124,11 +126,16 @@ class GuildEmoji extends Emoji {
*/ */
edit(data, reason) { edit(data, reason) {
const roles = data.roles ? data.roles.map(r => r.id || r) : undefined; const roles = data.roles ? data.roles.map(r => r.id || r) : undefined;
return this.client.api.guilds(this.guild.id).emojis(this.id) return this.client.api
.patch({ data: { .guilds(this.guild.id)
name: data.name, .emojis(this.id)
roles, .patch({
}, reason }) data: {
name: data.name,
roles,
},
reason,
})
.then(newData => { .then(newData => {
const clone = this._clone(); const clone = this._clone();
clone._patch(newData); clone._patch(newData);
@@ -152,7 +159,10 @@ class GuildEmoji extends Emoji {
* @returns {Promise<GuildEmoji>} * @returns {Promise<GuildEmoji>}
*/ */
delete(reason) { delete(reason) {
return this.client.api.guilds(this.guild.id).emojis(this.id).delete({ reason }) return this.client.api
.guilds(this.guild.id)
.emojis(this.id)
.delete({ reason })
.then(() => this); .then(() => this);
} }

View File

@@ -1,13 +1,13 @@
'use strict'; 'use strict';
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Role = require('./Role');
const Permissions = require('../util/Permissions');
const GuildMemberRoleManager = require('../managers/GuildMemberRoleManager');
const Base = require('./Base'); const Base = require('./Base');
const VoiceState = require('./VoiceState');
const { Presence } = require('./Presence'); const { Presence } = require('./Presence');
const Role = require('./Role');
const VoiceState = require('./VoiceState');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const { Error } = require('../errors'); const { Error } = require('../errors');
const GuildMemberRoleManager = require('../managers/GuildMemberRoleManager');
const Permissions = require('../util/Permissions');
/** /**
* Represents a member of a guild on Discord. * Represents a member of a guild on Discord.
@@ -152,12 +152,15 @@ class GuildMember extends Base {
* @readonly * @readonly
*/ */
get presence() { get presence() {
return this.guild.presences.cache.get(this.id) || new Presence(this.client, { return (
user: { this.guild.presences.cache.get(this.id) ||
id: this.id, new Presence(this.client, {
}, user: {
guild: this.guild, id: this.id,
}); },
guild: this.guild,
})
);
} }
/** /**
@@ -293,7 +296,7 @@ class GuildMember extends Base {
data.channel_id = null; data.channel_id = null;
data.channel = undefined; data.channel = undefined;
} }
if (data.roles) data.roles = data.roles.map(role => role instanceof Role ? role.id : role); if (data.roles) data.roles = data.roles.map(role => (role instanceof Role ? role.id : role));
let endpoint = this.client.api.guilds(this.guild.id); let endpoint = this.client.api.guilds(this.guild.id);
if (this.user.id === this.client.user.id) { if (this.user.id === this.client.user.id) {
const keys = Object.keys(data); const keys = Object.keys(data);
@@ -342,7 +345,10 @@ class GuildMember extends Base {
* @returns {Promise<GuildMember>} * @returns {Promise<GuildMember>}
*/ */
kick(reason) { kick(reason) {
return this.client.api.guilds(this.guild.id).members(this.user.id).delete({ reason }) return this.client.api
.guilds(this.guild.id)
.members(this.user.id)
.delete({ reason })
.then(() => this); .then(() => this);
} }

View File

@@ -98,7 +98,10 @@ class Integration extends Base {
*/ */
sync() { sync() {
this.syncing = true; this.syncing = true;
return this.client.api.guilds(this.guild.id).integrations(this.id).post() return this.client.api
.guilds(this.guild.id)
.integrations(this.id)
.post()
.then(() => { .then(() => {
this.syncing = false; this.syncing = false;
this.syncedAt = Date.now(); this.syncedAt = Date.now();
@@ -129,7 +132,10 @@ class Integration extends Base {
data.expireGracePeriod = null; data.expireGracePeriod = null;
} }
// The option enable_emoticons is only available for Twitch at this moment // The option enable_emoticons is only available for Twitch at this moment
return this.client.api.guilds(this.guild.id).integrations(this.id).patch({ data, reason }) return this.client.api
.guilds(this.guild.id)
.integrations(this.id)
.patch({ data, reason })
.then(() => { .then(() => {
this._patch(data); this._patch(data);
return this; return this;
@@ -142,7 +148,10 @@ class Integration extends Base {
* @param {string} [reason] Reason for deleting this integration * @param {string} [reason] Reason for deleting this integration
*/ */
delete(reason) { delete(reason) {
return this.client.api.guilds(this.guild.id).integrations(this.id).delete({ reason }) return this.client.api
.guilds(this.guild.id)
.integrations(this.id)
.delete({ reason })
.then(() => this); .then(() => this);
} }

View File

@@ -1,8 +1,8 @@
'use strict'; 'use strict';
const Base = require('./Base');
const { Endpoints } = require('../util/Constants'); const { Endpoints } = require('../util/Constants');
const Permissions = require('../util/Permissions'); const Permissions = require('../util/Permissions');
const Base = require('./Base');
/** /**
* Represents an invitation to a guild channel. * Represents an invitation to a guild channel.
@@ -119,8 +119,10 @@ class Invite extends Base {
const guild = this.guild; const guild = this.guild;
if (!guild || !this.client.guilds.cache.has(guild.id)) return false; if (!guild || !this.client.guilds.cache.has(guild.id)) return false;
if (!guild.me) throw new Error('GUILD_UNCACHED_ME'); if (!guild.me) throw new Error('GUILD_UNCACHED_ME');
return this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS, false) || return (
guild.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD); this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS, false) ||
guild.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD)
);
} }
/** /**
@@ -129,7 +131,7 @@ class Invite extends Base {
* @readonly * @readonly
*/ */
get expiresTimestamp() { get expiresTimestamp() {
return this.createdTimestamp && this.maxAge ? this.createdTimestamp + (this.maxAge * 1000) : null; return this.createdTimestamp && this.maxAge ? this.createdTimestamp + this.maxAge * 1000 : null;
} }
/** /**

View File

@@ -1,19 +1,19 @@
'use strict'; 'use strict';
const Mentions = require('./MessageMentions'); const APIMessage = require('./APIMessage');
const Base = require('./Base');
const ClientApplication = require('./ClientApplication');
const MessageAttachment = require('./MessageAttachment'); const MessageAttachment = require('./MessageAttachment');
const Embed = require('./MessageEmbed'); const Embed = require('./MessageEmbed');
const Mentions = require('./MessageMentions');
const ReactionCollector = require('./ReactionCollector'); const ReactionCollector = require('./ReactionCollector');
const ClientApplication = require('./ClientApplication');
const Util = require('../util/Util');
const Collection = require('../util/Collection');
const ReactionManager = require('../managers/ReactionManager');
const { MessageTypes } = require('../util/Constants');
const Permissions = require('../util/Permissions');
const Base = require('./Base');
const { Error, TypeError } = require('../errors'); const { Error, TypeError } = require('../errors');
const APIMessage = require('./APIMessage'); const ReactionManager = require('../managers/ReactionManager');
const Collection = require('../util/Collection');
const { MessageTypes } = require('../util/Constants');
const MessageFlags = require('../util/MessageFlags'); const MessageFlags = require('../util/MessageFlags');
const Permissions = require('../util/Permissions');
const Util = require('../util/Util');
/** /**
* Represents a message on Discord. * Represents a message on Discord.
@@ -43,7 +43,7 @@ class Message extends Base {
if (data) this._patch(data); if (data) this._patch(data);
} }
_patch(data) { // eslint-disable-line complexity _patch(data) {
/** /**
* The ID of the message * The ID of the message
* @type {Snowflake} * @type {Snowflake}
@@ -107,9 +107,7 @@ class Message extends Base {
this.attachments = new Collection(); this.attachments = new Collection();
if (data.attachments) { if (data.attachments) {
for (const attachment of data.attachments) { for (const attachment of data.attachments) {
this.attachments.set(attachment.id, new MessageAttachment( this.attachments.set(attachment.id, new MessageAttachment(attachment.url, attachment.filename, attachment));
attachment.url, attachment.filename, attachment,
));
} }
} }
@@ -158,10 +156,12 @@ class Message extends Base {
* Group activity * Group activity
* @type {?MessageActivity} * @type {?MessageActivity}
*/ */
this.activity = data.activity ? { this.activity = data.activity
partyID: data.activity.party_id, ? {
type: data.activity.type, partyID: data.activity.party_id,
} : null; type: data.activity.type,
}
: null;
/** /**
* The previous versions of the message, sorted with the most recent first * The previous versions of the message, sorted with the most recent first
@@ -194,11 +194,13 @@ class Message extends Base {
* Message reference data * Message reference data
* @type {?MessageReference} * @type {?MessageReference}
*/ */
this.reference = data.message_reference ? { this.reference = data.message_reference
channelID: data.message_reference.channel_id, ? {
guildID: data.message_reference.guild_id, channelID: data.message_reference.channel_id,
messageID: data.message_reference.message_id, guildID: data.message_reference.guild_id,
} : null; messageID: data.message_reference.message_id,
}
: null;
} }
/** /**
@@ -229,9 +231,7 @@ class Message extends Base {
if ('attachments' in data) { if ('attachments' in data) {
this.attachments = new Collection(); this.attachments = new Collection();
for (const attachment of data.attachments) { for (const attachment of data.attachments) {
this.attachments.set(attachment.id, new MessageAttachment( this.attachments.set(attachment.id, new MessageAttachment(attachment.url, attachment.filename, attachment));
attachment.url, attachment.filename, attachment,
));
} }
} else { } else {
this.attachments = new Collection(this.attachments); this.attachments = new Collection(this.attachments);
@@ -377,9 +377,11 @@ class Message extends Base {
* @readonly * @readonly
*/ */
get deletable() { get deletable() {
return !this.deleted && (this.author.id === this.client.user.id || (this.guild && return (
this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_MESSAGES, false) !this.deleted &&
)); (this.author.id === this.client.user.id ||
(this.guild && this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_MESSAGES, false)))
);
} }
/** /**
@@ -388,8 +390,10 @@ class Message extends Base {
* @readonly * @readonly
*/ */
get pinnable() { get pinnable() {
return this.type === 'DEFAULT' && (!this.guild || return (
this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_MESSAGES, false)); this.type === 'DEFAULT' &&
(!this.guild || this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_MESSAGES, false))
);
} }
/** /**
@@ -412,16 +416,13 @@ class Message extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
edit(content, options) { edit(content, options) {
const { data } = content instanceof APIMessage ? const { data } =
content.resolveData() : content instanceof APIMessage ? content.resolveData() : APIMessage.create(this, content, options).resolveData();
APIMessage.create(this, content, options).resolveData(); return this.client.api.channels[this.channel.id].messages[this.id].patch({ data }).then(d => {
return this.client.api.channels[this.channel.id].messages[this.id] const clone = this._clone();
.patch({ data }) clone._patch(d);
.then(d => { return clone;
const clone = this._clone(); });
clone._patch(d);
return clone;
});
} }
/** /**
@@ -429,7 +430,10 @@ class Message extends Base {
* @returns {Promise<Message>} * @returns {Promise<Message>}
*/ */
pin() { pin() {
return this.client.api.channels(this.channel.id).pins(this.id).put() return this.client.api
.channels(this.channel.id)
.pins(this.id)
.put()
.then(() => this); .then(() => this);
} }
@@ -438,7 +442,10 @@ class Message extends Base {
* @returns {Promise<Message>} * @returns {Promise<Message>}
*/ */
unpin() { unpin() {
return this.client.api.channels(this.channel.id).pins(this.id).delete() return this.client.api
.channels(this.channel.id)
.pins(this.id)
.delete()
.then(() => this); .then(() => this);
} }
@@ -461,14 +468,20 @@ class Message extends Base {
emoji = this.client.emojis.resolveIdentifier(emoji); emoji = this.client.emojis.resolveIdentifier(emoji);
if (!emoji) throw new TypeError('EMOJI_TYPE'); if (!emoji) throw new TypeError('EMOJI_TYPE');
return this.client.api.channels(this.channel.id).messages(this.id).reactions(emoji, '@me') return this.client.api
.channels(this.channel.id)
.messages(this.id)
.reactions(emoji, '@me')
.put() .put()
.then(() => this.client.actions.MessageReactionAdd.handle({ .then(
user: this.client.user, () =>
channel: this.channel, this.client.actions.MessageReactionAdd.handle({
message: this, user: this.client.user,
emoji: Util.parseEmoji(emoji), channel: this.channel,
}).reaction); message: this,
emoji: Util.parseEmoji(emoji),
}).reaction,
);
} }
/** /**
@@ -509,9 +522,10 @@ class Message extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
reply(content, options) { reply(content, options) {
return this.channel.send(content instanceof APIMessage ? return this.channel.send(
content : content instanceof APIMessage
APIMessage.transformOptions(content, options, { reply: this.member || this.author }), ? content
: APIMessage.transformOptions(content, options, { reply: this.member || this.author }),
); );
} }
@@ -562,16 +576,18 @@ class Message extends Base {
const embedUpdate = !message.author && !message.attachments; const embedUpdate = !message.author && !message.attachments;
if (embedUpdate) return this.id === message.id && this.embeds.length === message.embeds.length; if (embedUpdate) return this.id === message.id && this.embeds.length === message.embeds.length;
let equal = this.id === message.id && let equal =
this.author.id === message.author.id && this.id === message.id &&
this.content === message.content && this.author.id === message.author.id &&
this.tts === message.tts && this.content === message.content &&
this.nonce === message.nonce && this.tts === message.tts &&
this.embeds.length === message.embeds.length && this.nonce === message.nonce &&
this.attachments.length === message.attachments.length; this.embeds.length === message.embeds.length &&
this.attachments.length === message.attachments.length;
if (equal && rawData) { if (equal && rawData) {
equal = this.mentions.everyone === message.mentions.everyone && equal =
this.mentions.everyone === message.mentions.everyone &&
this.createdTimestamp === new Date(rawData.timestamp).getTime() && this.createdTimestamp === new Date(rawData.timestamp).getTime() &&
this.editedTimestamp === new Date(rawData.edited_timestamp).getTime(); this.editedTimestamp === new Date(rawData.edited_timestamp).getTime();
} }

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const Util = require('../util/Util');
const { RangeError } = require('../errors'); const { RangeError } = require('../errors');
const Util = require('../util/Util');
/** /**
* Represents an embed in a message (image/video preview, rich embed, etc.) * Represents an embed in a message (image/video preview, rich embed, etc.)
@@ -11,7 +11,7 @@ class MessageEmbed {
this.setup(data); this.setup(data);
} }
setup(data) { // eslint-disable-line complexity setup(data) {
/** /**
* The type of this embed, either: * The type of this embed, either:
* * `rich` - a rich embed * * `rich` - a rich embed
@@ -79,12 +79,14 @@ class MessageEmbed {
* The thumbnail of this embed (if there is one) * The thumbnail of this embed (if there is one)
* @type {?MessageEmbedThumbnail} * @type {?MessageEmbedThumbnail}
*/ */
this.thumbnail = data.thumbnail ? { this.thumbnail = data.thumbnail
url: data.thumbnail.url, ? {
proxyURL: data.thumbnail.proxyURL || data.thumbnail.proxy_url, url: data.thumbnail.url,
height: data.thumbnail.height, proxyURL: data.thumbnail.proxyURL || data.thumbnail.proxy_url,
width: data.thumbnail.width, height: data.thumbnail.height,
} : null; width: data.thumbnail.width,
}
: null;
/** /**
* @typedef {Object} MessageEmbedImage * @typedef {Object} MessageEmbedImage
@@ -98,12 +100,14 @@ class MessageEmbed {
* The image of this embed, if there is one * The image of this embed, if there is one
* @type {?MessageEmbedImage} * @type {?MessageEmbedImage}
*/ */
this.image = data.image ? { this.image = data.image
url: data.image.url, ? {
proxyURL: data.image.proxyURL || data.image.proxy_url, url: data.image.url,
height: data.image.height, proxyURL: data.image.proxyURL || data.image.proxy_url,
width: data.image.width, height: data.image.height,
} : null; width: data.image.width,
}
: null;
/** /**
* @typedef {Object} MessageEmbedVideo * @typedef {Object} MessageEmbedVideo
@@ -118,12 +122,14 @@ class MessageEmbed {
* @type {?MessageEmbedVideo} * @type {?MessageEmbedVideo}
* @readonly * @readonly
*/ */
this.video = data.video ? { this.video = data.video
url: data.video.url, ? {
proxyURL: data.video.proxyURL || data.video.proxy_url, url: data.video.url,
height: data.video.height, proxyURL: data.video.proxyURL || data.video.proxy_url,
width: data.video.width, height: data.video.height,
} : null; width: data.video.width,
}
: null;
/** /**
* @typedef {Object} MessageEmbedAuthor * @typedef {Object} MessageEmbedAuthor
@@ -137,12 +143,14 @@ class MessageEmbed {
* The author of this embed (if there is one) * The author of this embed (if there is one)
* @type {?MessageEmbedAuthor} * @type {?MessageEmbedAuthor}
*/ */
this.author = data.author ? { this.author = data.author
name: data.author.name, ? {
url: data.author.url, name: data.author.name,
iconURL: data.author.iconURL || data.author.icon_url, url: data.author.url,
proxyIconURL: data.author.proxyIconURL || data.author.proxy_icon_url, iconURL: data.author.iconURL || data.author.icon_url,
} : null; proxyIconURL: data.author.proxyIconURL || data.author.proxy_icon_url,
}
: null;
/** /**
* @typedef {Object} MessageEmbedProvider * @typedef {Object} MessageEmbedProvider
@@ -154,10 +162,12 @@ class MessageEmbed {
* The provider of this embed (if there is one) * The provider of this embed (if there is one)
* @type {?MessageEmbedProvider} * @type {?MessageEmbedProvider}
*/ */
this.provider = data.provider ? { this.provider = data.provider
name: data.provider.name, ? {
url: data.provider.name, name: data.provider.name,
} : null; url: data.provider.name,
}
: null;
/** /**
* @typedef {Object} MessageEmbedFooter * @typedef {Object} MessageEmbedFooter
@@ -170,11 +180,13 @@ class MessageEmbed {
* The footer of this embed * The footer of this embed
* @type {?MessageEmbedFooter} * @type {?MessageEmbedFooter}
*/ */
this.footer = data.footer ? { this.footer = data.footer
text: data.footer.text, ? {
iconURL: data.footer.iconURL || data.footer.icon_url, text: data.footer.text,
proxyIconURL: data.footer.proxyIconURL || data.footer.proxy_icon_url, iconURL: data.footer.iconURL || data.footer.icon_url,
} : null; proxyIconURL: data.footer.proxyIconURL || data.footer.proxy_icon_url,
}
: null;
/** /**
* The files of this embed * The files of this embed
@@ -210,9 +222,11 @@ class MessageEmbed {
return ( return (
(this.title ? this.title.length : 0) + (this.title ? this.title.length : 0) +
(this.description ? this.description.length : 0) + (this.description ? this.description.length : 0) +
(this.fields.length >= 1 ? this.fields.reduce((prev, curr) => (this.fields.length >= 1
prev + curr.name.length + curr.value.length, 0) : 0) + ? this.fields.reduce((prev, curr) => prev + curr.name.length + curr.value.length, 0)
(this.footer ? this.footer.text.length : 0)); : 0) +
(this.footer ? this.footer.text.length : 0)
);
} }
/** /**
@@ -371,15 +385,19 @@ class MessageEmbed {
fields: this.fields, fields: this.fields,
thumbnail: this.thumbnail, thumbnail: this.thumbnail,
image: this.image, image: this.image,
author: this.author ? { author: this.author
name: this.author.name, ? {
url: this.author.url, name: this.author.name,
icon_url: this.author.iconURL, url: this.author.url,
} : null, icon_url: this.author.iconURL,
footer: this.footer ? { }
text: this.footer.text, : null,
icon_url: this.footer.iconURL, footer: this.footer
} : null, ? {
text: this.footer.text,
icon_url: this.footer.iconURL,
}
: null,
}; };
} }
@@ -399,11 +417,11 @@ class MessageEmbed {
} }
/** /**
* @typedef {Object} EmbedFieldData * @typedef {Object} EmbedFieldData
* @property {StringResolvable} name The name of this field * @property {StringResolvable} name The name of this field
* @property {StringResolvable} value The value of this field * @property {StringResolvable} value The value of this field
* @property {boolean} [inline=false] If this field will be displayed inline * @property {boolean} [inline] If this field will be displayed inline
*/ */
/** /**
* Normalizes field input and resolves strings. * Normalizes field input and resolves strings.
@@ -411,13 +429,15 @@ class MessageEmbed {
* @returns {EmbedField[]} * @returns {EmbedField[]}
*/ */
static normalizeFields(...fields) { static normalizeFields(...fields) {
return fields.flat(2).map(field => return fields
this.normalizeField( .flat(2)
field && field.name, .map(field =>
field && field.value, this.normalizeField(
field && typeof field.inline === 'boolean' ? field.inline : false, field && field.name,
), field && field.value,
); field && typeof field.inline === 'boolean' ? field.inline : false,
),
);
} }
} }

View File

@@ -1,9 +1,9 @@
'use strict'; 'use strict';
const Collection = require('../util/Collection');
const Util = require('../util/Util');
const GuildMember = require('./GuildMember'); const GuildMember = require('./GuildMember');
const Collection = require('../util/Collection');
const { ChannelTypes } = require('../util/Constants'); const { ChannelTypes } = require('../util/Constants');
const Util = require('../util/Util');
/** /**
* Keeps track of mentions in a {@link Message}. * Keeps track of mentions in a {@link Message}.

View File

@@ -1,9 +1,9 @@
'use strict'; 'use strict';
const GuildEmoji = require('./GuildEmoji'); const GuildEmoji = require('./GuildEmoji');
const Util = require('../util/Util');
const ReactionEmoji = require('./ReactionEmoji'); const ReactionEmoji = require('./ReactionEmoji');
const ReactionUserManager = require('../managers/ReactionUserManager'); const ReactionUserManager = require('../managers/ReactionUserManager');
const Util = require('../util/Util');
/** /**
* Represents a reaction to a message. * Represents a reaction to a message.
@@ -59,7 +59,10 @@ class MessageReaction {
* @returns {Promise<MessageReaction>} * @returns {Promise<MessageReaction>}
*/ */
async remove() { async remove() {
await this.client.api.channels(this.message.channel.id).messages(this.message.id).reactions(this._emoji.identifier) await this.client.api
.channels(this.message.channel.id)
.messages(this.message.id)
.reactions(this._emoji.identifier)
.delete(); .delete();
return this; return this;
} }

View File

@@ -1,9 +1,9 @@
'use strict'; 'use strict';
const Role = require('./Role'); const Role = require('./Role');
const { TypeError } = require('../errors');
const Permissions = require('../util/Permissions'); const Permissions = require('../util/Permissions');
const Util = require('../util/Util'); const Util = require('../util/Util');
const { TypeError } = require('../errors');
/** /**
* Represents a permission overwrite for a role or member in a guild channel. * Represents a permission overwrite for a role or member in a guild channel.
@@ -70,8 +70,12 @@ class PermissionOverwrites {
update(options, reason) { update(options, reason) {
const { allow, deny } = this.constructor.resolveOverwriteOptions(options, this); const { allow, deny } = this.constructor.resolveOverwriteOptions(options, this);
return this.channel.client.api.channels(this.channel.id).permissions[this.id] return this.channel.client.api
.put({ data: { id: this.id, type: this.type, allow: allow.bitfield, deny: deny.bitfield }, reason }) .channels(this.channel.id)
.permissions[this.id].put({
data: { id: this.id, type: this.type, allow: allow.bitfield, deny: deny.bitfield },
reason,
})
.then(() => this); .then(() => this);
} }
@@ -81,9 +85,7 @@ class PermissionOverwrites {
* @returns {Promise<PermissionOverwrites>} * @returns {Promise<PermissionOverwrites>}
*/ */
delete(reason) { delete(reason) {
return this.channel.client.api.channels[this.channel.id].permissions[this.id] return this.channel.client.api.channels[this.channel.id].permissions[this.id].delete({ reason }).then(() => this);
.delete({ reason })
.then(() => this);
} }
toJSON() { toJSON() {
@@ -168,10 +170,7 @@ class PermissionOverwrites {
static resolve(overwrite, guild) { static resolve(overwrite, guild) {
if (overwrite instanceof this) return overwrite.toJSON(); if (overwrite instanceof this) return overwrite.toJSON();
if (typeof overwrite.id === 'string' && ['role', 'member'].includes(overwrite.type)) { if (typeof overwrite.id === 'string' && ['role', 'member'].includes(overwrite.type)) {
return { ...overwrite, return { ...overwrite, allow: Permissions.resolve(overwrite.allow), deny: Permissions.resolve(overwrite.deny) };
allow: Permissions.resolve(overwrite.allow),
deny: Permissions.resolve(overwrite.deny),
};
} }
const userOrRole = guild.roles.resolve(overwrite.id) || guild.client.users.resolve(overwrite.id); const userOrRole = guild.roles.resolve(overwrite.id) || guild.client.users.resolve(overwrite.id);

View File

@@ -1,9 +1,9 @@
'use strict'; 'use strict';
const Util = require('../util/Util'); const Emoji = require('./Emoji');
const ActivityFlags = require('../util/ActivityFlags'); const ActivityFlags = require('../util/ActivityFlags');
const { ActivityTypes } = require('../util/Constants'); const { ActivityTypes } = require('../util/Constants');
const Emoji = require('./Emoji'); const Util = require('../util/Util');
/** /**
* Activity sent in a message. * Activity sent in a message.
@@ -121,14 +121,15 @@ class Presence {
* @returns {boolean} * @returns {boolean}
*/ */
equals(presence) { equals(presence) {
return this === presence || ( return (
presence && this === presence ||
this.status === presence.status && (presence &&
this.activities.length === presence.activities.length && this.status === presence.status &&
this.activities.every((activity, index) => activity.equals(presence.activities[index])) && this.activities.length === presence.activities.length &&
this.clientStatus.web === presence.clientStatus.web && this.activities.every((activity, index) => activity.equals(presence.activities[index])) &&
this.clientStatus.mobile === presence.clientStatus.mobile && this.clientStatus.web === presence.clientStatus.web &&
this.clientStatus.desktop === presence.clientStatus.desktop this.clientStatus.mobile === presence.clientStatus.mobile &&
this.clientStatus.desktop === presence.clientStatus.desktop)
); );
} }
@@ -186,10 +187,12 @@ class Activity {
* @prop {?Date} start When the activity started * @prop {?Date} start When the activity started
* @prop {?Date} end When the activity will end * @prop {?Date} end When the activity will end
*/ */
this.timestamps = data.timestamps ? { this.timestamps = data.timestamps
start: data.timestamps.start ? new Date(Number(data.timestamps.start)) : null, ? {
end: data.timestamps.end ? new Date(Number(data.timestamps.end)) : null, start: data.timestamps.start ? new Date(Number(data.timestamps.start)) : null,
} : null; end: data.timestamps.end ? new Date(Number(data.timestamps.end)) : null,
}
: null;
/** /**
* Party of the activity * Party of the activity
@@ -232,11 +235,9 @@ class Activity {
* @returns {boolean} * @returns {boolean}
*/ */
equals(activity) { equals(activity) {
return this === activity || ( return (
activity && this === activity ||
this.name === activity.name && (activity && this.name === activity.name && this.type === activity.type && this.url === activity.url)
this.type === activity.type &&
this.url === activity.url
); );
} }
@@ -303,8 +304,10 @@ class RichPresenceAssets {
*/ */
smallImageURL({ format, size } = {}) { smallImageURL({ format, size } = {}) {
if (!this.smallImage) return null; if (!this.smallImage) return null;
return this.activity.presence.client.rest.cdn return this.activity.presence.client.rest.cdn.AppAsset(this.activity.applicationID, this.smallImage, {
.AppAsset(this.activity.applicationID, this.smallImage, { format, size }); format,
size,
});
} }
/** /**
@@ -321,8 +324,10 @@ class RichPresenceAssets {
} else if (/^twitch:/.test(this.largeImage)) { } else if (/^twitch:/.test(this.largeImage)) {
return `https://static-cdn.jtvnw.net/previews-ttv/live_user_${this.largeImage.slice(7)}.png`; return `https://static-cdn.jtvnw.net/previews-ttv/live_user_${this.largeImage.slice(7)}.png`;
} }
return this.activity.presence.client.rest.cdn return this.activity.presence.client.rest.cdn.AppAsset(this.activity.applicationID, this.largeImage, {
.AppAsset(this.activity.applicationID, this.largeImage, { format, size }); format,
size,
});
} }
} }

View File

@@ -118,8 +118,7 @@ class ReactionCollector extends Collector {
* @param {MessageReaction} reaction The reaction that was removed * @param {MessageReaction} reaction The reaction that was removed
* @param {User} user The user that removed the reaction * @param {User} user The user that removed the reaction
*/ */
if (this.collected.has(ReactionCollector.key(reaction)) && if (this.collected.has(ReactionCollector.key(reaction)) && this.users.has(user.id)) {
this.users.has(user.id)) {
this.emit('remove', reaction, user); this.emit('remove', reaction, user);
} }
return reaction.count ? null : ReactionCollector.key(reaction); return reaction.count ? null : ReactionCollector.key(reaction);

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const Util = require('../util/Util');
const Emoji = require('./Emoji'); const Emoji = require('./Emoji');
const Util = require('../util/Util');
/** /**
* Represents a limited emoji set used for both custom and unicode emojis. Custom emojis * Represents a limited emoji set used for both custom and unicode emojis. Custom emojis

View File

@@ -1,10 +1,10 @@
'use strict'; 'use strict';
const Snowflake = require('../util/Snowflake');
const Permissions = require('../util/Permissions');
const Util = require('../util/Util');
const Base = require('./Base'); const Base = require('./Base');
const { Error, TypeError } = require('../errors'); const { Error, TypeError } = require('../errors');
const Permissions = require('../util/Permissions');
const Snowflake = require('../util/Snowflake');
const Util = require('../util/Util');
/** /**
* Represents a role on Discord. * Represents a role on Discord.
@@ -180,25 +180,31 @@ class Role extends Base {
if (typeof data.permissions !== 'undefined') data.permissions = Permissions.resolve(data.permissions); if (typeof data.permissions !== 'undefined') data.permissions = Permissions.resolve(data.permissions);
else data.permissions = this.permissions.bitfield; else data.permissions = this.permissions.bitfield;
if (typeof data.position !== 'undefined') { if (typeof data.position !== 'undefined') {
await Util.setPosition(this, data.position, false, this.guild._sortedRoles(), await Util.setPosition(
this.client.api.guilds(this.guild.id).roles, reason) this,
.then(updatedRoles => { data.position,
this.client.actions.GuildRolesPositionUpdate.handle({ false,
guild_id: this.guild.id, this.guild._sortedRoles(),
roles: updatedRoles, this.client.api.guilds(this.guild.id).roles,
}); reason,
).then(updatedRoles => {
this.client.actions.GuildRolesPositionUpdate.handle({
guild_id: this.guild.id,
roles: updatedRoles,
}); });
});
} }
return this.client.api.guilds[this.guild.id].roles[this.id].patch({ return this.client.api.guilds[this.guild.id].roles[this.id]
data: { .patch({
name: data.name || this.name, data: {
color: data.color !== null ? Util.resolveColor(data.color || this.color) : null, name: data.name || this.name,
hoist: typeof data.hoist !== 'undefined' ? data.hoist : this.hoist, color: data.color !== null ? Util.resolveColor(data.color || this.color) : null,
permissions: data.permissions, hoist: typeof data.hoist !== 'undefined' ? data.hoist : this.hoist,
mentionable: typeof data.mentionable !== 'undefined' ? data.mentionable : this.mentionable, permissions: data.permissions,
}, mentionable: typeof data.mentionable !== 'undefined' ? data.mentionable : this.mentionable,
reason, },
}) reason,
})
.then(role => { .then(role => {
const clone = this._clone(); const clone = this._clone();
clone._patch(role); clone._patch(role);
@@ -312,15 +318,20 @@ class Role extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
setPosition(position, { relative, reason } = {}) { setPosition(position, { relative, reason } = {}) {
return Util.setPosition(this, position, relative, return Util.setPosition(
this.guild._sortedRoles(), this.client.api.guilds(this.guild.id).roles, reason) this,
.then(updatedRoles => { position,
this.client.actions.GuildRolesPositionUpdate.handle({ relative,
guild_id: this.guild.id, this.guild._sortedRoles(),
roles: updatedRoles, this.client.api.guilds(this.guild.id).roles,
}); reason,
return this; ).then(updatedRoles => {
this.client.actions.GuildRolesPositionUpdate.handle({
guild_id: this.guild.id,
roles: updatedRoles,
}); });
return this;
});
} }
/** /**
@@ -334,11 +345,10 @@ class Role extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
delete(reason) { delete(reason) {
return this.client.api.guilds[this.guild.id].roles[this.id].delete({ reason }) return this.client.api.guilds[this.guild.id].roles[this.id].delete({ reason }).then(() => {
.then(() => { this.client.actions.GuildRoleDelete.handle({ guild_id: this.guild.id, role_id: this.id });
this.client.actions.GuildRoleDelete.handle({ guild_id: this.guild.id, role_id: this.id }); return this;
return this; });
});
} }
/** /**
@@ -349,14 +359,16 @@ class Role extends Base {
* @returns {boolean} * @returns {boolean}
*/ */
equals(role) { equals(role) {
return role && return (
role &&
this.id === role.id && this.id === role.id &&
this.name === role.name && this.name === role.name &&
this.color === role.color && this.color === role.color &&
this.hoist === role.hoist && this.hoist === role.hoist &&
this.position === role.position && this.position === role.position &&
this.permissions.bitfield === role.permissions.bitfield && this.permissions.bitfield === role.permissions.bitfield &&
this.managed === role.managed; this.managed === role.managed
);
} }
/** /**

Some files were not shown because too many files have changed in this diff Show More