├── .eslintignore
├── .eslintrc.json
├── .gitignore
├── .npmignore
├── LICENSE
├── README.md
├── docs
├── Lavalink.html
├── Lavalink.js.html
├── Player.html
├── Player.js.html
├── PlayerManager.html
├── PlayerManager.js.html
├── index.html
├── scripts
│ ├── linenumber.js
│ └── prettify
│ │ ├── Apache-License-2.0.txt
│ │ ├── lang-css.js
│ │ └── prettify.js
└── styles
│ ├── jsdoc.css
│ └── prettify.css
├── package.json
└── src
├── Lavalink.js
├── Player.js
├── PlayerManager.js
└── index.js
/.eslintignore:
--------------------------------------------------------------------------------
1 | node_modules/*
2 | test/*
3 | build/*
4 | public/*
5 | private/*
6 | views/*
7 | *.md
8 | *.json
9 | commands/Admin/Eval.js
--------------------------------------------------------------------------------
/.eslintrc.json:
--------------------------------------------------------------------------------
1 |
2 | {
3 | "parser": "babel-eslint",
4 | "extends": "eslint:recommended",
5 | "parserOptions": {
6 | "ecmaVersion": 6
7 | },
8 | "env": {
9 | "es6": true,
10 | "node": true
11 | },
12 | "globals": {
13 | "Loader": true,
14 | "requireReload": true
15 | },
16 | "rules": {
17 | // "no-extra-parens": ["warn", "all", {
18 | // "nestedBinaryExpressions": false
19 | // }],
20 | // "valid-jsdoc": ["error", {
21 | // "requireReturn": false,
22 | // "requireReturnDescription": false,
23 | // "prefer": {
24 | // // "return": "returns"
25 | // }
26 | // }],
27 |
28 | "accessor-pairs": "warn",
29 | "array-callback-return": "error",
30 | // "complexity": "warn",
31 | // "consistent-return": "error",
32 | // "curly": ["error", "multi-line", "consistent"],
33 | "dot-location": ["error", "property"],
34 | "dot-notation": "error",
35 | "eqeqeq": "error",
36 | "no-empty-function": "error",
37 | "no-floating-decimal": "error",
38 | "no-implied-eval": "error",
39 | "no-invalid-this": "error",
40 | "no-lone-blocks": "error",
41 | "no-new-func": "error",
42 | "no-new-wrappers": "error",
43 | "no-new": "error",
44 | "no-octal-escape": "error",
45 | "no-return-assign": "error",
46 | "no-self-compare": "error",
47 | "no-sequences": "error",
48 | "no-throw-literal": "error",
49 | "no-unmodified-loop-condition": "error",
50 | "no-unused-expressions": "error",
51 | "no-useless-call": "error",
52 | "no-useless-escape": "error",
53 | "no-void": "error",
54 | "no-warning-comments": "warn",
55 | "wrap-iife": "error",
56 | "yoda": "error",
57 |
58 | "no-label-var": "error",
59 | // "no-shadow": "error",
60 | "no-undef-init": "error",
61 |
62 | "callback-return": "error",
63 | "handle-callback-err": "error",
64 | "no-mixed-requires": "error",
65 | "no-new-require": "error",
66 | "no-path-concat": "error",
67 |
68 | "array-bracket-spacing": "error",
69 | "block-spacing": "error",
70 | "brace-style": ["error", "1tbs", { "allowSingleLine": true }],
71 | "comma-dangle": ["error", "always-multiline"],
72 | "comma-spacing": "error",
73 | "comma-style": "error",
74 | "computed-property-spacing": "error",
75 | "consistent-this": ["error", "$this"],
76 | "eol-last": "error",
77 | "func-names": "error",
78 | "func-style": ["error", "declaration", { "allowArrowFunctions": true }],
79 | "keyword-spacing": "error",
80 | "max-depth": "error",
81 | // "max-len": ["error", 120, 2],
82 | "max-nested-callbacks": ["error", { "max": 4 }],
83 | "max-statements-per-line": ["error", { "max": 2 }],
84 | "new-cap": "error",
85 | "newline-per-chained-call": ["error", { "ignoreChainWithDepth": 3 }],
86 | "no-array-constructor": "error",
87 | // "no-inline-comments": "error",
88 | "no-lonely-if": "error",
89 | "no-mixed-operators": "error",
90 | "no-multiple-empty-lines": ["error", { "max": 2, "maxEOF": 1, "maxBOF": 0 }],
91 | "no-new-object": "error",
92 | "no-spaced-func": "error",
93 | "no-trailing-spaces": "error",
94 | "no-unneeded-ternary": "error",
95 | "no-whitespace-before-property": "error",
96 | "object-curly-spacing": ["error", "always"],
97 | "operator-assignment": "error",
98 | "operator-linebreak": ["error", "after"],
99 | "padded-blocks": ["error", "never"],
100 | "quote-props": ["error", "as-needed"],
101 | "quotes": ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
102 | "semi-spacing": "error",
103 | "semi": "error",
104 | "space-before-blocks": "error",
105 | "space-before-function-paren": ["error", "never"],
106 | "space-in-parens": "error",
107 | "space-infix-ops": "error",
108 | "space-unary-ops": "error",
109 | "spaced-comment": "error",
110 | "unicode-bom": "error",
111 |
112 | "arrow-body-style": "error",
113 | "arrow-spacing": "error",
114 | "no-duplicate-imports": "error",
115 | "no-useless-computed-key": "error",
116 | "no-useless-constructor": "error",
117 | "prefer-arrow-callback": "error",
118 | "prefer-rest-params": "error",
119 | "prefer-spread": "error",
120 | // "prefer-template": "error",
121 | "rest-spread-spacing": "error",
122 | "template-curly-spacing": "error",
123 | "yield-star-spacing": "error"
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | package-lock.json
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 |
6 | # Dependencies
7 | node_modules/
8 |
9 | # Coverage
10 | coverage
11 |
12 | test/
13 | docs/
14 | __tests__/
15 |
16 | # VS Code
17 | .vscode
18 | !.vscode/tasks.js
19 |
20 | # JetBrains IDEs
21 | .idea/
22 |
23 | # Optional npm cache directory
24 | .npm
25 |
26 | # Optional eslint cache
27 | .eslintcache
28 |
29 | # Misc
30 | .DS_Store
31 | .env
32 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Brian Tanner
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # eris-lavalink
2 |
3 | A [Lavalink](https://github.com/Frederikam/Lavalink) client for [eris](https://github.com/abalabahaha/eris)
4 |
5 | ## Links
6 | - **[Documentation](https://briantanner.github.io/eris-lavalink/)**
7 | - **[Lavalink](https://github.com/Frederikam/Lavalink)**
8 | - **[eris](https://github.com/abalabahaha/eris)**
9 | - **[lavalink.js](https://github.com/briantanner/lavalink.js)** (discord.js port)
10 |
11 | ## Install
12 | ```
13 | # Lavalink v2
14 | npm install eris-lavalink
15 |
16 | # Lavalink v1
17 | npm install eris-lavalink@^0.1.3
18 | ```
19 |
20 | ## Implementation
21 |
22 | Start by creating the `PlayerManager` and passing a list of nodes and optional list of regions
23 | ```js
24 | const { PlayerManager } = require('eris-lavalink');
25 |
26 | let nodes = [
27 | { host: 'localhost', port: 8080, region: 'eu', password: 'youshallnotpass' }
28 | ];
29 |
30 | let regions = {
31 | eu: ['eu', 'amsterdam', 'frankfurt', 'russia', 'hongkong', 'singapore', 'sydney'],
32 | us: ['us', 'brazil'],
33 | };
34 |
35 | if (!(client.voiceConnections instanceof PlayerManager)) {
36 | client.voiceConnections = new PlayerManager(client, nodes, {
37 | numShards: shardCount, // number of shards
38 | userId: userid, // the user id of the bot
39 | regions: regions,
40 | defaultRegion: 'eu',
41 | });
42 | }
43 | ```
44 |
45 | To resolve a track, use the Lavalink rest api
46 | ```js
47 | const superagent = require('superagent');
48 |
49 | async function resolveTracks(node, search) {
50 | try {
51 | var result = await superagent.get(`http://${node.host}:2333/loadtracks?identifier=${search}`)
52 | .set('Authorization', node.password)
53 | .set('Accept', 'application/json');
54 | } catch (err) {
55 | throw err;
56 | }
57 |
58 | if (!result) {
59 | throw 'Unable play that video.';
60 | }
61 |
62 | return result.body; // array of tracks resolved from lavalink
63 | }
64 |
65 | resolveTracks(node, 'ytsearch:the 30 second video').then(tracks => {
66 | if (!tracks) {
67 | // no tracks to play
68 | }
69 | // do something with the tracks
70 | })
71 | ```
72 |
73 | To join and leave voice channels, use the Lavalink client rather than using eris.
74 | ```js
75 | // to get or join a channel
76 | function getPlayer(channel) {
77 | if (!channel || !channel.guild) {
78 | return Promise.reject('Not a guild channel.');
79 | }
80 |
81 | let player = client.voiceConnections.get(channel.guild.id);
82 | if (player) {
83 | return Promise.resolve(player);
84 | }
85 |
86 | let options = {};
87 | if (channel.guild.region) {
88 | options.region = channel.guild.region;
89 | }
90 |
91 | return client.joinVoiceChannel(channel.id, options);
92 | }
93 |
94 | // play example
95 | getPlayer(channel).then(player => {
96 | player.play(track); // track is the base64 track we get from Lavalink
97 |
98 | player.on('disconnect', (err) => {
99 | if (err) {
100 | // log error
101 | }
102 | // do something
103 | });
104 |
105 | player.on('error', err => {
106 | // log error and handle it
107 | });
108 |
109 | player.on('stuck', msg => {
110 | // track stuck event
111 | })
112 |
113 | player.once('end', data => {
114 | // REPLACED reason is emitted when playing without stopping, I ignore these to prevent skip loops
115 | if (data.reason && data.reason === 'REPLACED') {
116 | return;
117 | }
118 |
119 | // start playing the next song
120 | });
121 | });
122 |
123 | // stop example
124 | getPlayer(channel).then(player => {
125 | player.stop();
126 | if (leave) {
127 | // disconnect and leave the channel
128 | client.leaveVoiceChannel(channel.id);
129 | }
130 | })
131 | ```
132 |
133 | **A note on pauses**
134 |
135 | When you pause a player, the player will be kept in a paused state until you explicitly call resume or the player is disconnected. Calls to `play` and `stop` won't clear the pause state. `player.paused` can be used to check if the player is in paused state.
136 |
--------------------------------------------------------------------------------
/docs/Lavalink.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Lavalink - Documentation
6 |
7 |
8 |
9 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | Classes
26 |
27 |
28 |
29 |
30 |
Lavalink
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
Constructor
58 |
59 |
60 |
new Lavalink(options)
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 | Source:
71 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
Properties:
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 | Name
118 |
119 |
120 | Type
121 |
122 |
123 |
124 |
125 |
126 | Description
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 | host
136 |
137 |
138 |
139 |
140 |
141 | string
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 | The hostname for the node
152 |
153 |
154 |
155 |
156 |
157 |
158 | port
159 |
160 |
161 |
162 |
163 |
164 | number
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 | The port number for the node
175 |
176 |
177 |
178 |
179 |
180 |
181 | address
182 |
183 |
184 |
185 |
186 |
187 | string
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 | The full ws address for the node
198 |
199 |
200 |
201 |
202 |
203 |
204 | region
205 |
206 |
207 |
208 |
209 |
210 | string
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 | The region for this node
221 |
222 |
223 |
224 |
225 |
226 |
227 | userId
228 |
229 |
230 |
231 |
232 |
233 | string
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 | The client user id
244 |
245 |
246 |
247 |
248 |
249 |
250 | numShards
251 |
252 |
253 |
254 |
255 |
256 | number
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 | The total number of shards the bot is running
267 |
268 |
269 |
270 |
271 |
272 |
273 | password
274 |
275 |
276 |
277 |
278 |
279 | string
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 | The password used to connect
290 |
291 |
292 |
293 |
294 |
295 |
296 | connected
297 |
298 |
299 |
300 |
301 |
302 | boolean
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 | If it's connected to the node
313 |
314 |
315 |
316 |
317 |
318 |
319 | draining
320 |
321 |
322 |
323 |
324 |
325 | boolean
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 | True if this node will no longer take new connections
336 |
337 |
338 |
339 |
340 |
341 |
342 | stats
343 |
344 |
345 |
346 |
347 |
348 | object
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 | The Lavalink node stats
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 | Lavalink constructor
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
Parameters:
385 |
386 |
387 |
388 |
389 |
390 |
391 | Name
392 |
393 |
394 | Type
395 |
396 |
397 |
398 |
399 |
400 | Description
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 | options
410 |
411 |
412 |
413 |
414 |
415 | Object
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 | Lavalink node options
426 | Properties
427 |
428 |
429 |
430 |
431 |
432 |
433 | Name
434 |
435 |
436 | Type
437 |
438 |
439 | Attributes
440 |
441 |
442 |
443 | Default
444 |
445 |
446 | Description
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 | host
456 |
457 |
458 |
459 |
460 |
461 | string
462 |
463 |
464 |
465 |
466 |
467 |
468 |
469 |
470 |
471 |
472 |
473 |
474 |
475 |
476 |
477 |
478 |
479 |
480 |
481 |
482 |
483 | The hostname to connect to
484 |
485 |
486 |
487 |
488 |
489 |
490 | port
491 |
492 |
493 |
494 |
495 |
496 | string
497 |
498 |
499 |
500 |
501 |
502 |
503 |
504 |
505 |
506 |
507 |
508 |
509 |
510 |
511 |
512 |
513 |
514 |
515 |
516 |
517 |
518 | The port to connect with
519 |
520 |
521 |
522 |
523 |
524 |
525 | region
526 |
527 |
528 |
529 |
530 |
531 | string
532 |
533 |
534 |
535 |
536 |
537 |
538 |
539 |
540 |
541 |
542 |
543 |
544 |
545 |
546 |
547 |
548 |
549 |
550 |
551 |
552 |
553 | The region of the node
554 |
555 |
556 |
557 |
558 |
559 |
560 | numShards
561 |
562 |
563 |
564 |
565 |
566 | number
567 |
568 |
569 |
570 |
571 |
572 |
573 |
574 |
575 |
576 |
577 |
578 |
579 |
580 |
581 |
582 |
583 |
584 |
585 |
586 |
587 |
588 | The number of shards the bot is running
589 |
590 |
591 |
592 |
593 |
594 |
595 | userId
596 |
597 |
598 |
599 |
600 |
601 | string
602 |
603 |
604 |
605 |
606 |
607 |
608 |
609 |
610 |
611 |
612 |
613 |
614 |
615 |
616 |
617 |
618 |
619 |
620 |
621 |
622 |
623 | The user id of the bot
624 |
625 |
626 |
627 |
628 |
629 |
630 | password
631 |
632 |
633 |
634 |
635 |
636 | string
637 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 |
646 |
647 |
648 |
649 |
650 |
651 |
652 |
653 |
654 |
655 |
656 |
657 |
658 | The password for the Lavalink node
659 |
660 |
661 |
662 |
663 |
664 |
665 | timeout
666 |
667 |
668 |
669 |
670 |
671 | number
672 |
673 |
674 |
675 |
676 |
677 |
678 |
679 |
680 | <optional>
681 |
682 |
683 |
684 |
685 |
686 |
687 |
688 |
689 |
690 |
691 |
692 | 5000
693 |
694 |
695 |
696 |
697 | Optional timeout in ms used for the reconnect backoff
698 |
699 |
700 |
701 |
702 |
703 |
704 |
705 |
706 |
707 |
708 |
709 |
710 |
711 |
712 |
713 |
714 |
715 |
716 |
717 |
718 |
719 |
720 |
721 |
722 |
723 |
724 |
725 |
726 |
727 |
728 |
729 |
730 | Extends
731 |
732 |
733 |
734 |
735 |
738 |
739 |
740 |
741 |
742 |
743 |
744 |
745 |
746 |
747 |
748 |
749 |
750 |
751 |
752 |
753 | Methods
754 |
755 |
756 |
757 |
758 |
759 |
760 | destroy()
761 |
762 |
763 |
764 |
765 |
766 |
767 |
768 |
769 |
770 | Source:
771 |
774 |
775 |
776 |
777 |
778 |
779 |
780 |
781 |
782 |
783 |
784 |
785 |
786 |
787 |
788 |
789 |
790 |
791 |
792 |
793 |
794 |
795 |
796 |
797 |
798 |
799 |
800 |
801 |
802 |
803 |
804 |
805 |
806 |
807 |
808 |
809 |
810 |
811 |
812 | Destroy the websocket connection
813 |
814 |
815 |
816 |
817 |
818 |
819 |
820 |
821 |
822 |
823 |
824 |
825 |
826 |
827 |
828 |
829 |
830 |
831 |
832 |
833 |
834 |
835 |
836 |
837 |
838 |
839 |
840 |
841 |
842 |
843 |
844 | send(op, data)
845 |
846 |
847 |
848 |
849 |
850 |
851 |
852 |
853 |
854 | Source:
855 |
858 |
859 |
860 |
861 |
862 |
863 |
864 |
865 |
866 |
867 |
868 |
869 |
870 |
871 |
872 |
873 |
874 |
875 |
876 |
877 |
878 |
879 |
880 |
881 |
882 |
883 |
884 |
885 |
886 |
887 |
888 |
889 |
890 |
891 |
892 |
893 |
894 |
895 |
896 | Send data to Lavalink
897 |
898 |
899 |
900 |
901 |
902 |
903 |
904 |
905 |
906 |
907 |
908 |
909 | Parameters:
910 |
911 |
912 |
913 |
914 |
915 |
916 | Name
917 |
918 |
919 | Type
920 |
921 |
922 |
923 |
924 |
925 | Description
926 |
927 |
928 |
929 |
930 |
931 |
932 |
933 |
934 | op
935 |
936 |
937 |
938 |
939 |
940 | string
941 |
942 |
943 |
944 |
945 |
946 |
947 |
948 |
949 |
950 | Op name
951 |
952 |
953 |
954 |
955 |
956 |
957 | data
958 |
959 |
960 |
961 |
962 |
963 | *
964 |
965 |
966 |
967 |
968 |
969 |
970 |
971 |
972 |
973 | Data to send
974 |
975 |
976 |
977 |
978 |
979 |
980 |
981 |
982 |
983 |
984 |
985 |
986 |
987 |
988 |
989 |
990 |
991 |
992 |
993 |
994 |
995 |
996 |
997 |
998 |
999 |
1000 |
1001 |
1002 |
1003 |
1004 |
1005 |
1006 |
1007 |
1008 |
1009 |
1010 |
1011 |
1012 |
1013 | Documentation generated by JSDoc 3.5.5 on Mon Jan 01 2018 08:16:30 GMT-0500 (EST) using the docdash theme.
1014 |
1015 |
1016 |
1017 |
1018 |
1019 |
--------------------------------------------------------------------------------
/docs/Lavalink.js.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Lavalink.js - Documentation
6 |
7 |
8 |
9 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | Classes
26 |
27 |
28 |
29 |
30 |
Lavalink.js
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 | 'use strict';
41 |
42 | const WebSocket = require('ws');
43 |
44 | var EventEmitter;
45 |
46 | try {
47 | EventEmitter = require('eventemitter3');
48 | } catch (err) {
49 | EventEmitter = require('events').EventEmitter;
50 | }
51 |
52 | /**
53 | * Represents a Lavalink node
54 | * @extends EventEmitter
55 | * @prop {string} host The hostname for the node
56 | * @prop {number} port The port number for the node
57 | * @prop {string} address The full ws address for the node
58 | * @prop {string} region The region for this node
59 | * @prop {string} userId The client user id
60 | * @prop {number} numShards The total number of shards the bot is running
61 | * @prop {string} password The password used to connect
62 | * @prop {boolean} connected If it's connected to the node
63 | * @prop {boolean} draining True if this node will no longer take new connections
64 | * @prop {object} stats The Lavalink node stats
65 | */
66 | class Lavalink extends EventEmitter {
67 | /**
68 | * Lavalink constructor
69 | * @param {Object} options Lavalink node options
70 | * @param {string} options.host The hostname to connect to
71 | * @param {string} options.port The port to connect with
72 | * @param {string} options.region The region of the node
73 | * @param {number} options.numShards The number of shards the bot is running
74 | * @param {string} options.userId The user id of the bot
75 | * @param {string} options.password The password for the Lavalink node
76 | * @param {number} [options.timeout=5000] Optional timeout in ms used for the reconnect backoff
77 | */
78 | constructor(options) {
79 | super();
80 |
81 | this.host = options.host;
82 | this.port = options.port || 80;
83 | this.address = `ws://${this.host}:${this.port}`;
84 | this.region = options.region || null;
85 | this.userId = options.userId;
86 | this.numShards = options.numShards;
87 | this.password = options.password || 'youshallnotpass';
88 | this.connected = false;
89 | this.draining = false;
90 | this.retries = 0;
91 | this.reconnectTimeout = options.timeout || 5000;
92 | this.reconnectInterval = null;
93 | this.stats = { players: 0, playingPlayers: 0 };
94 | this.disconnectHandler = this.disconnected.bind(this);
95 |
96 | this.connect();
97 | }
98 |
99 | /**
100 | * Connect to the websocket server
101 | * @private
102 | */
103 | connect() {
104 | this.ws = new WebSocket(this.address, {
105 | headers: {
106 | 'Authorization': this.password,
107 | 'Num-Shards': this.numShards,
108 | 'User-Id': this.userId,
109 | },
110 | });
111 |
112 | this.ws.on('open', this.ready.bind(this));
113 | this.ws.on('message', this.onMessage.bind(this));
114 | this.ws.on('close', this.disconnectHandler);
115 | this.ws.on('error', (err) => {
116 | this.emit('error', err);
117 | });
118 | }
119 |
120 | /**
121 | * Reconnect to the websocket
122 | * @private
123 | */
124 | reconnect() {
125 | let interval = this.retryInterval();
126 | this.reconnectInterval = setTimeout(this.reconnect.bind(this), interval);
127 | this.retries++;
128 | this.connect();
129 | }
130 |
131 | /**
132 | * Destroy the websocket connection
133 | */
134 | destroy() {
135 | if (this.ws) {
136 | this.ws.removeListener('close', this.disconnectHandler);
137 | this.ws.close();
138 | }
139 | }
140 |
141 | /**
142 | * Called when the websocket is open
143 | * @private
144 | */
145 | ready() {
146 | if (this.reconnectInterval) {
147 | clearTimeout(this.reconnectInterval);
148 | this.reconnectInterval = null;
149 | }
150 |
151 | this.connected = true;
152 | this.retries = 0;
153 | this.emit('ready');
154 | }
155 |
156 | /**
157 | * Called when the websocket disconnects
158 | * @private
159 | */
160 | disconnected() {
161 | this.connected = false;
162 | if (!this.reconnectInterval) {
163 | this.emit('disconnect');
164 | }
165 |
166 | delete this.ws;
167 |
168 | if (!this.reconnectInterval) {
169 | this.reconnectInterval = setTimeout(this.reconnect.bind(this), this.reconnectTimeout);
170 | }
171 | }
172 |
173 | /**
174 | * Get the retry interval
175 | * @private
176 | */
177 | retryInterval() {
178 | let retries = Math.min(this.retries-1, 5);
179 | return Math.pow(retries + 5, 2) * 1000;
180 | }
181 |
182 | /**
183 | * Send data to Lavalink
184 | * @param {string} op Op name
185 | * @param {*} data Data to send
186 | */
187 | send(data) {
188 | const ws = this.ws;
189 | if (!ws) return;
190 |
191 | try {
192 | var payload = JSON.stringify(data);
193 | } catch (err) {
194 | return this.emit('error', 'Unable to stringify payload.');
195 | }
196 |
197 | ws.send(payload);
198 | }
199 |
200 | /**
201 | * Handle message from the server
202 | * @param {string} message Raw websocket message
203 | * @private
204 | */
205 | onMessage(message) {
206 | try {
207 | var data = JSON.parse(message);
208 | } catch (e) {
209 | return this.emit('error', 'Unable to parse ws message.');
210 | }
211 |
212 | if (data.op && data.op === 'stats') {
213 | this.stats = data;
214 | }
215 |
216 | this.emit('message', data);
217 | }
218 | }
219 |
220 | module.exports = Lavalink;
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 | Documentation generated by JSDoc 3.5.5 on Mon Jan 01 2018 08:16:30 GMT-0500 (EST) using the docdash theme.
234 |
235 |
236 |
237 |
238 |
239 |
240 |
--------------------------------------------------------------------------------
/docs/Player.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Player - Documentation
6 |
7 |
8 |
9 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | Classes
26 |
27 |
28 |
29 |
30 |
Player
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
Constructor
58 |
59 |
60 |
new Player(id, data)
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 | Source:
71 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
Properties:
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 | Name
118 |
119 |
120 | Type
121 |
122 |
123 |
124 |
125 |
126 | Description
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 | id
136 |
137 |
138 |
139 |
140 |
141 | string
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 | Guild id for the player
152 |
153 |
154 |
155 |
156 |
157 |
158 | manager
159 |
160 |
161 |
162 |
163 |
164 | PlayerManager
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 | Reference to the player manager
175 |
176 |
177 |
178 |
179 |
180 |
181 | node
182 |
183 |
184 |
185 |
186 |
187 | Lavalink
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 | Lavalink node the player is connected to
198 |
199 |
200 |
201 |
202 |
203 |
204 | shard
205 |
206 |
207 |
208 |
209 |
210 | object
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 | The eris shard the player is associated with
221 |
222 |
223 |
224 |
225 |
226 |
227 | hostname
228 |
229 |
230 |
231 |
232 |
233 | string
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 | Hostname of the lavalink node
244 |
245 |
246 |
247 |
248 |
249 |
250 | guildId
251 |
252 |
253 |
254 |
255 |
256 | string
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 | Guild ID
267 |
268 |
269 |
270 |
271 |
272 |
273 | channelId
274 |
275 |
276 |
277 |
278 |
279 | string
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 | Channel ID
290 |
291 |
292 |
293 |
294 |
295 |
296 | ready
297 |
298 |
299 |
300 |
301 |
302 | boolean
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 | If the connection is ready
313 |
314 |
315 |
316 |
317 |
318 |
319 | playing
320 |
321 |
322 |
323 |
324 |
325 | boolean
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 | If the player is playing
336 |
337 |
338 |
339 |
340 |
341 |
342 | state
343 |
344 |
345 |
346 |
347 |
348 | object
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 | The lavalink player state
359 |
360 |
361 |
362 |
363 |
364 |
365 | track
366 |
367 |
368 |
369 |
370 |
371 | string
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 | The lavalink track to play
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 | Player constructor
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
Parameters:
408 |
409 |
410 |
411 |
412 |
413 |
414 | Name
415 |
416 |
417 | Type
418 |
419 |
420 |
421 |
422 |
423 | Description
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 | id
433 |
434 |
435 |
436 |
437 |
438 | string
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 | Guild ID
449 |
450 |
451 |
452 |
453 |
454 |
455 | data
456 |
457 |
458 |
459 |
460 |
461 | Object
462 |
463 |
464 |
465 |
466 |
467 |
468 |
469 |
470 |
471 | Player data
472 | Properties
473 |
474 |
475 |
476 |
477 |
478 |
479 | Name
480 |
481 |
482 | Type
483 |
484 |
485 | Attributes
486 |
487 |
488 |
489 |
490 | Description
491 |
492 |
493 |
494 |
495 |
496 |
497 |
498 |
499 | channelId
500 |
501 |
502 |
503 |
504 |
505 | string
506 |
507 |
508 |
509 |
510 |
511 |
512 |
513 |
514 |
515 |
516 |
517 |
518 |
519 |
520 |
521 |
522 |
523 | The channel id of the player
524 |
525 |
526 |
527 |
528 |
529 |
530 | guildId
531 |
532 |
533 |
534 |
535 |
536 | string
537 |
538 |
539 |
540 |
541 |
542 |
543 |
544 |
545 |
546 |
547 |
548 |
549 |
550 |
551 |
552 |
553 |
554 | The guild id of the player
555 |
556 |
557 |
558 |
559 |
560 |
561 | hostname
562 |
563 |
564 |
565 |
566 |
567 | string
568 |
569 |
570 |
571 |
572 |
573 |
574 |
575 |
576 |
577 |
578 |
579 |
580 |
581 |
582 |
583 |
584 |
585 | The hostname of the lavalink node
586 |
587 |
588 |
589 |
590 |
591 |
592 | manager
593 |
594 |
595 |
596 |
597 |
598 | PlayerManager
599 |
600 |
601 |
602 |
603 |
604 |
605 |
606 |
607 |
608 |
609 |
610 |
611 |
612 |
613 |
614 |
615 |
616 | The PlayerManager associated with this player
617 |
618 |
619 |
620 |
621 |
622 |
623 | node
624 |
625 |
626 |
627 |
628 |
629 | Lavalink
630 |
631 |
632 |
633 |
634 |
635 |
636 |
637 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 |
646 |
647 | The Lavalink node associated with this player
648 |
649 |
650 |
651 |
652 |
653 |
654 | shard
655 |
656 |
657 |
658 |
659 |
660 | Shard
661 |
662 |
663 |
664 |
665 |
666 |
667 |
668 |
669 |
670 |
671 |
672 |
673 |
674 |
675 |
676 |
677 |
678 | The eris shard associated with this player
679 |
680 |
681 |
682 |
683 |
684 |
685 | options
686 |
687 |
688 |
689 |
690 |
691 | Object
692 |
693 |
694 |
695 |
696 |
697 |
698 |
699 |
700 | <optional>
701 |
702 |
703 |
704 |
705 |
706 |
707 |
708 |
709 |
710 |
711 | Additional passed from the user to the player
712 |
713 |
714 |
715 |
716 |
717 |
718 |
719 |
720 |
721 |
722 |
723 |
724 |
725 |
726 |
727 |
728 |
729 |
730 |
731 |
732 |
733 |
734 |
735 |
736 |
737 |
738 |
739 |
740 |
741 |
742 |
743 |
744 | Extends
745 |
746 |
747 |
748 |
749 |
752 |
753 |
754 |
755 |
756 |
757 |
758 |
759 |
760 |
761 |
762 |
763 |
764 |
765 |
766 |
767 | Methods
768 |
769 |
770 |
771 |
772 |
773 |
774 | connect(data) → {void}
775 |
776 |
777 |
778 |
779 |
780 |
781 |
782 |
783 |
784 | Source:
785 |
788 |
789 |
790 |
791 |
792 |
793 |
794 |
795 |
796 |
797 |
798 |
799 |
800 |
801 |
802 |
803 |
804 |
805 |
806 |
807 |
808 |
809 |
810 |
811 |
812 |
813 |
814 |
815 |
816 |
817 |
818 |
819 |
820 |
821 |
822 |
823 |
824 |
825 |
826 | Connect to the Lavalink node
827 |
828 |
829 |
830 |
831 |
832 |
833 |
834 |
835 |
836 |
837 |
838 |
839 | Parameters:
840 |
841 |
842 |
843 |
844 |
845 |
846 | Name
847 |
848 |
849 | Type
850 |
851 |
852 |
853 |
854 |
855 | Description
856 |
857 |
858 |
859 |
860 |
861 |
862 |
863 |
864 | data
865 |
866 |
867 |
868 |
869 |
870 | Object
871 |
872 |
873 |
874 |
875 |
876 |
877 |
878 |
879 |
880 | The data used to connect
881 | Properties
882 |
883 |
884 |
885 |
886 |
887 |
888 | Name
889 |
890 |
891 | Type
892 |
893 |
894 |
895 |
896 |
897 | Description
898 |
899 |
900 |
901 |
902 |
903 |
904 |
905 |
906 | guildId
907 |
908 |
909 |
910 |
911 |
912 | string
913 |
914 |
915 |
916 |
917 |
918 |
919 |
920 |
921 |
922 | The guild ID to connect
923 |
924 |
925 |
926 |
927 |
928 |
929 | sessionId
930 |
931 |
932 |
933 |
934 |
935 | string
936 |
937 |
938 |
939 |
940 |
941 |
942 |
943 |
944 |
945 | The voice connection session ID
946 |
947 |
948 |
949 |
950 |
951 |
952 | event
953 |
954 |
955 |
956 |
957 |
958 | object
959 |
960 |
961 |
962 |
963 |
964 |
965 |
966 |
967 |
968 | The event data from the voice server update
969 |
970 |
971 |
972 |
973 |
974 |
975 |
976 |
977 |
978 |
979 |
980 |
981 |
982 |
983 |
984 |
985 |
986 |
987 |
988 |
989 |
990 |
991 |
992 |
993 |
994 |
995 | Returns:
996 |
997 |
998 |
999 |
1000 |
1001 |
1002 | Type
1003 |
1004 |
1005 |
1006 | void
1007 |
1008 |
1009 |
1010 |
1011 |
1012 |
1013 |
1014 |
1015 |
1016 |
1017 |
1018 |
1019 |
1020 | (async) disconnect(msgopt ) → {void}
1021 |
1022 |
1023 |
1024 |
1025 |
1026 |
1027 |
1028 |
1029 |
1030 | Source:
1031 |
1034 |
1035 |
1036 |
1037 |
1038 |
1039 |
1040 |
1041 |
1042 |
1043 |
1044 |
1045 |
1046 |
1047 |
1048 |
1049 |
1050 |
1051 |
1052 |
1053 |
1054 |
1055 |
1056 |
1057 |
1058 |
1059 |
1060 |
1061 |
1062 |
1063 |
1064 |
1065 |
1066 |
1067 |
1068 |
1069 |
1070 |
1071 |
1072 | Disconnect from Lavalink
1073 |
1074 |
1075 |
1076 |
1077 |
1078 |
1079 |
1080 |
1081 |
1082 |
1083 |
1084 |
1085 | Parameters:
1086 |
1087 |
1088 |
1089 |
1090 |
1091 |
1092 | Name
1093 |
1094 |
1095 | Type
1096 |
1097 |
1098 | Attributes
1099 |
1100 |
1101 |
1102 |
1103 | Description
1104 |
1105 |
1106 |
1107 |
1108 |
1109 |
1110 |
1111 |
1112 | msg
1113 |
1114 |
1115 |
1116 |
1117 |
1118 | *
1119 |
1120 |
1121 |
1122 |
1123 |
1124 |
1125 |
1126 |
1127 | <optional>
1128 |
1129 |
1130 |
1131 |
1132 |
1133 |
1134 |
1135 |
1136 |
1137 |
1138 | An optional disconnect message
1139 |
1140 |
1141 |
1142 |
1143 |
1144 |
1145 |
1146 |
1147 |
1148 |
1149 |
1150 |
1151 |
1152 |
1153 |
1154 |
1155 |
1156 |
1157 |
1158 | Returns:
1159 |
1160 |
1161 |
1162 |
1163 |
1164 |
1165 | Type
1166 |
1167 |
1168 |
1169 | void
1170 |
1171 |
1172 |
1173 |
1174 |
1175 |
1176 |
1177 |
1178 |
1179 |
1180 |
1181 |
1182 |
1183 | play(track, optionsopt ) → {void}
1184 |
1185 |
1186 |
1187 |
1188 |
1189 |
1190 |
1191 |
1192 |
1193 | Source:
1194 |
1197 |
1198 |
1199 |
1200 |
1201 |
1202 |
1203 |
1204 |
1205 |
1206 |
1207 |
1208 |
1209 |
1210 |
1211 |
1212 |
1213 |
1214 |
1215 |
1216 |
1217 |
1218 |
1219 |
1220 |
1221 |
1222 |
1223 |
1224 |
1225 |
1226 |
1227 |
1228 |
1229 |
1230 |
1231 |
1232 |
1233 |
1234 |
1235 | Play a Lavalink track
1236 |
1237 |
1238 |
1239 |
1240 |
1241 |
1242 |
1243 |
1244 |
1245 |
1246 |
1247 |
1248 | Parameters:
1249 |
1250 |
1251 |
1252 |
1253 |
1254 |
1255 | Name
1256 |
1257 |
1258 | Type
1259 |
1260 |
1261 | Attributes
1262 |
1263 |
1264 |
1265 |
1266 | Description
1267 |
1268 |
1269 |
1270 |
1271 |
1272 |
1273 |
1274 |
1275 | track
1276 |
1277 |
1278 |
1279 |
1280 |
1281 | string
1282 |
1283 |
1284 |
1285 |
1286 |
1287 |
1288 |
1289 |
1290 |
1291 |
1292 |
1293 |
1294 |
1295 |
1296 |
1297 |
1298 |
1299 | The track to play
1300 |
1301 |
1302 |
1303 |
1304 |
1305 |
1306 | options
1307 |
1308 |
1309 |
1310 |
1311 |
1312 | Object
1313 |
1314 |
1315 |
1316 |
1317 |
1318 |
1319 |
1320 |
1321 | <optional>
1322 |
1323 |
1324 |
1325 |
1326 |
1327 |
1328 |
1329 |
1330 |
1331 |
1332 | Optional options to send
1333 |
1334 |
1335 |
1336 |
1337 |
1338 |
1339 |
1340 |
1341 |
1342 |
1343 |
1344 |
1345 |
1346 |
1347 |
1348 |
1349 |
1350 |
1351 |
1352 | Returns:
1353 |
1354 |
1355 |
1356 |
1357 |
1358 |
1359 | Type
1360 |
1361 |
1362 |
1363 | void
1364 |
1365 |
1366 |
1367 |
1368 |
1369 |
1370 |
1371 |
1372 |
1373 |
1374 |
1375 |
1376 |
1377 | seek(position) → {void}
1378 |
1379 |
1380 |
1381 |
1382 |
1383 |
1384 |
1385 |
1386 |
1387 | Source:
1388 |
1391 |
1392 |
1393 |
1394 |
1395 |
1396 |
1397 |
1398 |
1399 |
1400 |
1401 |
1402 |
1403 |
1404 |
1405 |
1406 |
1407 |
1408 |
1409 |
1410 |
1411 |
1412 |
1413 |
1414 |
1415 |
1416 |
1417 |
1418 |
1419 |
1420 |
1421 |
1422 |
1423 |
1424 |
1425 |
1426 |
1427 |
1428 |
1429 | Used for seeking to a track position
1430 |
1431 |
1432 |
1433 |
1434 |
1435 |
1436 |
1437 |
1438 |
1439 |
1440 |
1441 |
1442 | Parameters:
1443 |
1444 |
1445 |
1446 |
1447 |
1448 |
1449 | Name
1450 |
1451 |
1452 | Type
1453 |
1454 |
1455 |
1456 |
1457 |
1458 | Description
1459 |
1460 |
1461 |
1462 |
1463 |
1464 |
1465 |
1466 |
1467 | position
1468 |
1469 |
1470 |
1471 |
1472 |
1473 | number
1474 |
1475 |
1476 |
1477 |
1478 |
1479 |
1480 |
1481 |
1482 |
1483 | The position to seek to
1484 |
1485 |
1486 |
1487 |
1488 |
1489 |
1490 |
1491 |
1492 |
1493 |
1494 |
1495 |
1496 |
1497 |
1498 |
1499 |
1500 |
1501 |
1502 |
1503 | Returns:
1504 |
1505 |
1506 |
1507 |
1508 |
1509 |
1510 | Type
1511 |
1512 |
1513 |
1514 | void
1515 |
1516 |
1517 |
1518 |
1519 |
1520 |
1521 |
1522 |
1523 |
1524 |
1525 |
1526 |
1527 |
1528 | setPause(pause) → {void}
1529 |
1530 |
1531 |
1532 |
1533 |
1534 |
1535 |
1536 |
1537 |
1538 | Source:
1539 |
1542 |
1543 |
1544 |
1545 |
1546 |
1547 |
1548 |
1549 |
1550 |
1551 |
1552 |
1553 |
1554 |
1555 |
1556 |
1557 |
1558 |
1559 |
1560 |
1561 |
1562 |
1563 |
1564 |
1565 |
1566 |
1567 |
1568 |
1569 |
1570 |
1571 |
1572 |
1573 |
1574 |
1575 |
1576 |
1577 |
1578 |
1579 |
1580 | Used to pause/resume the player
1581 |
1582 |
1583 |
1584 |
1585 |
1586 |
1587 |
1588 |
1589 |
1590 |
1591 |
1592 |
1593 | Parameters:
1594 |
1595 |
1596 |
1597 |
1598 |
1599 |
1600 | Name
1601 |
1602 |
1603 | Type
1604 |
1605 |
1606 |
1607 |
1608 |
1609 | Description
1610 |
1611 |
1612 |
1613 |
1614 |
1615 |
1616 |
1617 |
1618 | pause
1619 |
1620 |
1621 |
1622 |
1623 |
1624 | boolean
1625 |
1626 |
1627 |
1628 |
1629 |
1630 |
1631 |
1632 |
1633 |
1634 | Set pause to true/false
1635 |
1636 |
1637 |
1638 |
1639 |
1640 |
1641 |
1642 |
1643 |
1644 |
1645 |
1646 |
1647 |
1648 |
1649 |
1650 |
1651 |
1652 |
1653 |
1654 | Returns:
1655 |
1656 |
1657 |
1658 |
1659 |
1660 |
1661 | Type
1662 |
1663 |
1664 |
1665 | void
1666 |
1667 |
1668 |
1669 |
1670 |
1671 |
1672 |
1673 |
1674 |
1675 |
1676 |
1677 |
1678 |
1679 | setVolume(volume) → {void}
1680 |
1681 |
1682 |
1683 |
1684 |
1685 |
1686 |
1687 |
1688 |
1689 | Source:
1690 |
1693 |
1694 |
1695 |
1696 |
1697 |
1698 |
1699 |
1700 |
1701 |
1702 |
1703 |
1704 |
1705 |
1706 |
1707 |
1708 |
1709 |
1710 |
1711 |
1712 |
1713 |
1714 |
1715 |
1716 |
1717 |
1718 |
1719 |
1720 |
1721 |
1722 |
1723 |
1724 |
1725 |
1726 |
1727 |
1728 |
1729 |
1730 |
1731 | Set the volume of the player
1732 |
1733 |
1734 |
1735 |
1736 |
1737 |
1738 |
1739 |
1740 |
1741 |
1742 |
1743 |
1744 | Parameters:
1745 |
1746 |
1747 |
1748 |
1749 |
1750 |
1751 | Name
1752 |
1753 |
1754 | Type
1755 |
1756 |
1757 |
1758 |
1759 |
1760 | Description
1761 |
1762 |
1763 |
1764 |
1765 |
1766 |
1767 |
1768 |
1769 | volume
1770 |
1771 |
1772 |
1773 |
1774 |
1775 | number
1776 |
1777 |
1778 |
1779 |
1780 |
1781 |
1782 |
1783 |
1784 |
1785 | The volume level to set
1786 |
1787 |
1788 |
1789 |
1790 |
1791 |
1792 |
1793 |
1794 |
1795 |
1796 |
1797 |
1798 |
1799 |
1800 |
1801 |
1802 |
1803 |
1804 |
1805 | Returns:
1806 |
1807 |
1808 |
1809 |
1810 |
1811 |
1812 | Type
1813 |
1814 |
1815 |
1816 | void
1817 |
1818 |
1819 |
1820 |
1821 |
1822 |
1823 |
1824 |
1825 |
1826 |
1827 |
1828 |
1829 |
1830 | stop() → {void}
1831 |
1832 |
1833 |
1834 |
1835 |
1836 |
1837 |
1838 |
1839 |
1840 | Source:
1841 |
1844 |
1845 |
1846 |
1847 |
1848 |
1849 |
1850 |
1851 |
1852 |
1853 |
1854 |
1855 |
1856 |
1857 |
1858 |
1859 |
1860 |
1861 |
1862 |
1863 |
1864 |
1865 |
1866 |
1867 |
1868 |
1869 |
1870 |
1871 |
1872 |
1873 |
1874 |
1875 |
1876 |
1877 |
1878 |
1879 |
1880 |
1881 |
1882 | Stop playing
1883 |
1884 |
1885 |
1886 |
1887 |
1888 |
1889 |
1890 |
1891 |
1892 |
1893 |
1894 |
1895 |
1896 |
1897 |
1898 |
1899 |
1900 |
1901 |
1902 |
1903 |
1904 |
1905 |
1906 |
1907 | Returns:
1908 |
1909 |
1910 |
1911 |
1912 |
1913 |
1914 | Type
1915 |
1916 |
1917 |
1918 | void
1919 |
1920 |
1921 |
1922 |
1923 |
1924 |
1925 |
1926 |
1927 |
1928 |
1929 |
1930 |
1931 |
1932 | switchChannel(channelId, reactiveopt ) → {void}
1933 |
1934 |
1935 |
1936 |
1937 |
1938 |
1939 |
1940 |
1941 |
1942 | Source:
1943 |
1946 |
1947 |
1948 |
1949 |
1950 |
1951 |
1952 |
1953 |
1954 |
1955 |
1956 |
1957 |
1958 |
1959 |
1960 |
1961 |
1962 |
1963 |
1964 |
1965 |
1966 |
1967 |
1968 |
1969 |
1970 |
1971 |
1972 |
1973 |
1974 |
1975 |
1976 |
1977 |
1978 |
1979 |
1980 |
1981 |
1982 |
1983 |
1984 | Switch voice channel
1985 |
1986 |
1987 |
1988 |
1989 |
1990 |
1991 |
1992 |
1993 |
1994 |
1995 |
1996 |
1997 | Parameters:
1998 |
1999 |
2000 |
2001 |
2002 |
2003 |
2004 | Name
2005 |
2006 |
2007 | Type
2008 |
2009 |
2010 | Attributes
2011 |
2012 |
2013 |
2014 |
2015 | Description
2016 |
2017 |
2018 |
2019 |
2020 |
2021 |
2022 |
2023 |
2024 | channelId
2025 |
2026 |
2027 |
2028 |
2029 |
2030 | string
2031 |
2032 |
2033 |
2034 |
2035 |
2036 |
2037 |
2038 |
2039 |
2040 |
2041 |
2042 |
2043 |
2044 |
2045 |
2046 |
2047 |
2048 | Called when switching channels
2049 |
2050 |
2051 |
2052 |
2053 |
2054 |
2055 | reactive
2056 |
2057 |
2058 |
2059 |
2060 |
2061 | boolean
2062 |
2063 |
2064 |
2065 |
2066 |
2067 |
2068 |
2069 |
2070 | <optional>
2071 |
2072 |
2073 |
2074 |
2075 |
2076 |
2077 |
2078 |
2079 |
2080 |
2081 | Used if you want the bot to switch channels
2082 |
2083 |
2084 |
2085 |
2086 |
2087 |
2088 |
2089 |
2090 |
2091 |
2092 |
2093 |
2094 |
2095 |
2096 |
2097 |
2098 |
2099 |
2100 |
2101 | Returns:
2102 |
2103 |
2104 |
2105 |
2106 |
2107 |
2108 | Type
2109 |
2110 |
2111 |
2112 | void
2113 |
2114 |
2115 |
2116 |
2117 |
2118 |
2119 |
2120 |
2121 |
2122 |
2123 |
2124 |
2125 |
2126 |
2127 |
2128 |
2129 |
2130 |
2131 |
2132 |
2133 |
2134 |
2135 |
2136 |
2137 |
2138 |
2139 | Documentation generated by JSDoc 3.5.5 on Mon Jan 01 2018 08:16:30 GMT-0500 (EST) using the docdash theme.
2140 |
2141 |
2142 |
2143 |
2144 |
2145 |
--------------------------------------------------------------------------------
/docs/Player.js.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Player.js - Documentation
6 |
7 |
8 |
9 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | Classes
26 |
27 |
28 |
29 |
30 |
Player.js
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 | const Constants = require('eris').Constants;
41 |
42 | var EventEmitter;
43 |
44 | try {
45 | EventEmitter = require('eventemitter3');
46 | } catch (err) {
47 | EventEmitter = require('events').EventEmitter;
48 | }
49 |
50 | /**
51 | * Represents a player/voice connection to Lavalink
52 | * @extends EventEmitter
53 | * @prop {string} id Guild id for the player
54 | * @prop {PlayerManager} manager Reference to the player manager
55 | * @prop {Lavalink} node Lavalink node the player is connected to
56 | * @prop {object} shard The eris shard the player is associated with
57 | * @prop {string} hostname Hostname of the lavalink node
58 | * @prop {string} guildId Guild ID
59 | * @prop {string} channelId Channel ID
60 | * @prop {boolean} ready If the connection is ready
61 | * @prop {boolean} playing If the player is playing
62 | * @prop {object} state The lavalink player state
63 | * @prop {string} track The lavalink track to play
64 | */
65 | class Player extends EventEmitter {
66 | /**
67 | * Player constructor
68 | * @param {string} id Guild ID
69 | * @param {Object} data Player data
70 | * @param {string} data.channelId The channel id of the player
71 | * @param {string} data.guildId The guild id of the player
72 | * @param {string} data.hostname The hostname of the lavalink node
73 | * @param {PlayerManager} data.manager The PlayerManager associated with this player
74 | * @param {Lavalink} data.node The Lavalink node associated with this player
75 | * @param {Shard} data.shard The eris shard associated with this player
76 | * @param {Object} [data.options] Additional passed from the user to the player
77 | */
78 | constructor(id, { hostname, guildId, channelId, shard, node, manager, options }) {
79 | super();
80 | this.id = id;
81 | this.node = node;
82 | this.hostname = hostname;
83 | this.guildId = guildId;
84 | this.channelId = channelId;
85 | this.manager = manager || null;
86 | this.options = options;
87 | this.ready = false;
88 | this.playing = false;
89 | this.shard = shard;
90 | this.state = {};
91 | this.track = null;
92 | this.receivedEvents = [];
93 | this.sendQueue = [];
94 | this.timestamp = Date.now();
95 | }
96 |
97 | /**
98 | * Check the event queue
99 | * @private
100 | */
101 | checkEventQueue() {
102 | if (this.sendQueue.length > 0) {
103 | let event = this.sendQueue.splice(0,1);
104 | this.sendEvent(event[0]);
105 | }
106 | }
107 |
108 | /**
109 | * Queue an event to be sent to Lavalink
110 | * @param {*} data The payload to queue
111 | * @private
112 | */
113 | queueEvent(data) {
114 | if (this.sendQueue.length > 0) {
115 | this.sendQueue.push(data);
116 | } else {
117 | return this.sendEvent(data);
118 | }
119 | }
120 |
121 | /**
122 | * Send a payload to Lavalink
123 | * @param {*} data The payload to send
124 | * @private
125 | */
126 | async sendEvent(data) {
127 | this.receivedEvents.push(data);
128 | this.node.send(data);
129 | process.nextTick(() => this.checkEventQueue());
130 | }
131 |
132 | /**
133 | * Connect to the Lavalink node
134 | * @param {Object} data The data used to connect
135 | * @param {string} data.guildId The guild ID to connect
136 | * @param {string} data.sessionId The voice connection session ID
137 | * @param {object} data.event The event data from the voice server update
138 | * @returns {void}
139 | */
140 | connect(data) {
141 | this.emit('connect');
142 | this.queueEvent({
143 | op: 'voiceUpdate',
144 | guildId: data.guildId,
145 | sessionId: data.sessionId,
146 | event: data.event,
147 | });
148 |
149 | process.nextTick(() => this.emit('ready'));
150 | }
151 |
152 | /**
153 | * Disconnect from Lavalink
154 | * @param {*} [msg] An optional disconnect message
155 | * @returns {void}
156 | */
157 | async disconnect(msg) {
158 | this.playing = false;
159 | this.queueEvent({ op: 'disconnect', guildId: this.guildId });
160 | this.emit('disconnect', msg);
161 | }
162 |
163 | /**
164 | * Play a Lavalink track
165 | * @param {string} track The track to play
166 | * @param {Object} [options] Optional options to send
167 | * @returns {void}
168 | */
169 | play(track, options) {
170 | this.lastTrack = this.track;
171 | this.track = track;
172 | this.playOptions = options;
173 |
174 | if (this.node.draining) {
175 | this.state.position = 0;
176 | return this.manager.switchNode(this);
177 | }
178 |
179 | let payload = Object.assign({
180 | op: 'play',
181 | guildId: this.guildId,
182 | track: track,
183 | }, options);
184 |
185 | this.queueEvent(payload);
186 | this.playing = true;
187 | this.timestamp = Date.now();
188 | }
189 |
190 | /**
191 | * Stop playing
192 | * @returns {void}
193 | */
194 | stop() {
195 | let data = {
196 | op: 'stop',
197 | guildId: this.guildId,
198 | };
199 |
200 | this.queueEvent(data);
201 | this.playing = false;
202 | this.lastTrack = this.track;
203 | this.track = null;
204 | }
205 |
206 | /**
207 | * Update player state
208 | * @param {Object} state The state object received from Lavalink
209 | * @private
210 | */
211 | stateUpdate(state) {
212 | this.state = state;
213 | }
214 |
215 | /**
216 | * Used to pause/resume the player
217 | * @param {boolean} pause Set pause to true/false
218 | * @returns {void}
219 | */
220 | setPause(pause) {
221 | this.node.send({
222 | op: 'pause',
223 | guildId: this.guildId,
224 | pause: pause,
225 | });
226 | }
227 |
228 | /**
229 | * Used for seeking to a track position
230 | * @param {number} position The position to seek to
231 | * @returns {void}
232 | */
233 | seek(position) {
234 | this.node.send({
235 | op: 'seek',
236 | guildId: this.guildId,
237 | position: position,
238 | });
239 | }
240 |
241 | /**
242 | * Set the volume of the player
243 | * @param {number} volume The volume level to set
244 | * @returns {void}
245 | */
246 | setVolume(volume) {
247 | this.node.send({
248 | op: 'volume',
249 | guildId: this.guildId,
250 | volume: volume,
251 | });
252 | }
253 |
254 | /**
255 | * Called on track end
256 | * @param {Object} message The end reason
257 | * @private
258 | */
259 | onTrackEnd(message) {
260 | if (message.reason !== 'REPLACED') {
261 | this.playing = false;
262 | this.lastTrack = this.track;
263 | this.track = null;
264 | }
265 | this.emit('end', message);
266 | }
267 |
268 | /**
269 | * Called on track exception
270 | * @param {Object} message The exception encountered
271 | * @private
272 | */
273 | onTrackException(message) {
274 | this.emit('error', message);
275 | }
276 |
277 | /**
278 | * Called on track stuck
279 | * @param {Object} message The message if exists
280 | * @private
281 | */
282 | onTrackStuck(message) {
283 | this.stop();
284 | process.nextTick(() => this.emit('end', message));
285 | }
286 |
287 | /**
288 | * Switch voice channel
289 | * @param {string} channelId Called when switching channels
290 | * @param {boolean} [reactive] Used if you want the bot to switch channels
291 | * @returns {void}
292 | */
293 | switchChannel(channelId, reactive) {
294 | if(this.channelId === channelId) {
295 | return;
296 | }
297 |
298 | this.channelId = channelId;
299 | if (reactive === true) {
300 | this.updateVoiceState(channelId);
301 | }
302 | }
303 |
304 | getTimestamp() {
305 | return Date.now() - this.timestamp;
306 | }
307 |
308 | /**
309 | * Update the bot's voice state
310 | * @param {boolean} selfMute Whether the bot muted itself or not (audio sending is unaffected)
311 | * @param {boolean} selfDeaf Whether the bot deafened itself or not (audio receiving is unaffected)
312 | * @private
313 | */
314 | updateVoiceState(channelId, selfMute, selfDeaf) {
315 | if (this.shard.sendWS) {
316 | this.shard.sendWS(Constants.GatewayOPCodes.VOICE_STATE_UPDATE, {
317 | guild_id: this.id === 'call' ? null : this.id,
318 | channel_id: channelId || null,
319 | self_mute: !!selfMute,
320 | self_deaf: !!selfDeaf,
321 | });
322 | }
323 | }
324 | }
325 |
326 | module.exports = Player;
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 | Documentation generated by JSDoc 3.5.5 on Mon Jan 01 2018 08:16:30 GMT-0500 (EST) using the docdash theme.
340 |
341 |
342 |
343 |
344 |
345 |
346 |
--------------------------------------------------------------------------------
/docs/PlayerManager.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | PlayerManager - Documentation
6 |
7 |
8 |
9 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | Classes
26 |
27 |
28 |
29 |
30 |
PlayerManager
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | PlayerManager
44 |
45 |
46 | Player Manager
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
Constructor
58 |
59 |
60 |
new PlayerManager(client, nodes, optionsopt )
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 | Source:
71 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
Properties:
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 | Name
118 |
119 |
120 | Type
121 |
122 |
123 |
124 |
125 |
126 | Description
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 | baseObject
136 |
137 |
138 |
139 |
140 |
141 | Player
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 | The player class used to create new players
152 |
153 |
154 |
155 |
156 |
157 |
158 | client
159 |
160 |
161 |
162 |
163 |
164 | object
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 | The eris client
175 |
176 |
177 |
178 |
179 |
180 |
181 | defaultRegions
182 |
183 |
184 |
185 |
186 |
187 | object
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 | The default region config
198 |
199 |
200 |
201 |
202 |
203 |
204 | regions
205 |
206 |
207 |
208 |
209 |
210 | object
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 | The region config being used
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 | PlayerManager constructor
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
Parameters:
247 |
248 |
249 |
250 |
251 |
252 |
253 | Name
254 |
255 |
256 | Type
257 |
258 |
259 | Attributes
260 |
261 |
262 |
263 |
264 | Description
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 | client
274 |
275 |
276 |
277 |
278 |
279 | Client
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 | Eris client
298 |
299 |
300 |
301 |
302 |
303 |
304 | nodes
305 |
306 |
307 |
308 |
309 |
310 | Array.<Object>
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 | The Lavalink nodes to connect to
329 |
330 |
331 |
332 |
333 |
334 |
335 | options
336 |
337 |
338 |
339 |
340 |
341 | Object
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 | <optional>
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 | Setup options
362 | Properties
363 |
364 |
365 |
366 |
367 |
368 |
369 | Name
370 |
371 |
372 | Type
373 |
374 |
375 | Attributes
376 |
377 |
378 |
379 | Default
380 |
381 |
382 | Description
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 | defaultRegion
392 |
393 |
394 |
395 |
396 |
397 | string
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 | <optional>
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 | The default region
422 |
423 |
424 |
425 |
426 |
427 |
428 | failoverRate
429 |
430 |
431 |
432 |
433 |
434 | number
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 | <optional>
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 | 250
456 |
457 |
458 |
459 |
460 | Failover rate in ms
461 |
462 |
463 |
464 |
465 |
466 |
467 | failoverLimit
468 |
469 |
470 |
471 |
472 |
473 | number
474 |
475 |
476 |
477 |
478 |
479 |
480 |
481 |
482 | <optional>
483 |
484 |
485 |
486 |
487 |
488 |
489 |
490 |
491 |
492 |
493 |
494 | 1
495 |
496 |
497 |
498 |
499 | Number of connections to failover per rate limit
500 |
501 |
502 |
503 |
504 |
505 |
506 | player
507 |
508 |
509 |
510 |
511 |
512 | Object
513 |
514 |
515 |
516 |
517 |
518 |
519 |
520 |
521 | <optional>
522 |
523 |
524 |
525 |
526 |
527 |
528 |
529 |
530 |
531 |
532 |
533 |
534 |
535 |
536 | Optional Player class to replace the default Player
537 |
538 |
539 |
540 |
541 |
542 |
543 | reconnectThreshold
544 |
545 |
546 |
547 |
548 |
549 | number
550 |
551 |
552 |
553 |
554 |
555 |
556 |
557 |
558 | <optional>
559 |
560 |
561 |
562 |
563 |
564 |
565 |
566 |
567 |
568 |
569 |
570 | 2000
571 |
572 |
573 |
574 |
575 | The amount of time to skip ahead in a song when reconnecting in ms
576 |
577 |
578 |
579 |
580 |
581 |
582 | regions
583 |
584 |
585 |
586 |
587 |
588 | Object
589 |
590 |
591 |
592 |
593 |
594 |
595 |
596 |
597 | <optional>
598 |
599 |
600 |
601 |
602 |
603 |
604 |
605 |
606 |
607 |
608 |
609 |
610 |
611 |
612 | Region mapping object
613 |
614 |
615 |
616 |
617 |
618 |
619 |
620 |
621 |
622 |
623 |
624 |
625 |
626 |
627 |
628 |
629 |
630 |
631 |
632 |
633 |
634 |
635 |
636 |
637 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 | Extends
646 |
647 |
648 |
649 |
650 |
653 |
654 |
655 |
656 |
657 |
658 |
659 |
660 |
661 |
662 |
663 |
664 |
665 |
666 |
667 |
668 | Methods
669 |
670 |
671 |
672 |
673 |
674 |
675 | createNode(options) → {void}
676 |
677 |
678 |
679 |
680 |
681 |
682 |
683 |
684 |
685 | Source:
686 |
689 |
690 |
691 |
692 |
693 |
694 |
695 |
696 |
697 |
698 |
699 |
700 |
701 |
702 |
703 |
704 |
705 |
706 |
707 |
708 |
709 |
710 |
711 |
712 |
713 |
714 |
715 |
716 |
717 |
718 |
719 |
720 |
721 |
722 |
723 |
724 |
725 |
726 |
727 | Create a Lavalink node
728 |
729 |
730 |
731 |
732 |
733 |
734 |
735 |
736 |
737 |
738 |
739 |
740 | Parameters:
741 |
742 |
743 |
744 |
745 |
746 |
747 | Name
748 |
749 |
750 | Type
751 |
752 |
753 |
754 |
755 |
756 | Description
757 |
758 |
759 |
760 |
761 |
762 |
763 |
764 |
765 | options
766 |
767 |
768 |
769 |
770 |
771 | Object
772 |
773 |
774 |
775 |
776 |
777 |
778 |
779 |
780 |
781 | Lavalink node options
782 | Properties
783 |
784 |
785 |
786 |
787 |
788 |
789 | Name
790 |
791 |
792 | Type
793 |
794 |
795 |
796 |
797 |
798 | Description
799 |
800 |
801 |
802 |
803 |
804 |
805 |
806 |
807 | host
808 |
809 |
810 |
811 |
812 |
813 | string
814 |
815 |
816 |
817 |
818 |
819 |
820 |
821 |
822 |
823 | The hostname to connect to
824 |
825 |
826 |
827 |
828 |
829 |
830 | port
831 |
832 |
833 |
834 |
835 |
836 | string
837 |
838 |
839 |
840 |
841 |
842 |
843 |
844 |
845 |
846 | The port to connect with
847 |
848 |
849 |
850 |
851 |
852 |
853 | region
854 |
855 |
856 |
857 |
858 |
859 | string
860 |
861 |
862 |
863 |
864 |
865 |
866 |
867 |
868 |
869 | The region of the node
870 |
871 |
872 |
873 |
874 |
875 |
876 | numShards
877 |
878 |
879 |
880 |
881 |
882 | number
883 |
884 |
885 |
886 |
887 |
888 |
889 |
890 |
891 |
892 | The number of shards the bot is running
893 |
894 |
895 |
896 |
897 |
898 |
899 | userId
900 |
901 |
902 |
903 |
904 |
905 | string
906 |
907 |
908 |
909 |
910 |
911 |
912 |
913 |
914 |
915 | The user id of the bot
916 |
917 |
918 |
919 |
920 |
921 |
922 | password
923 |
924 |
925 |
926 |
927 |
928 | string
929 |
930 |
931 |
932 |
933 |
934 |
935 |
936 |
937 |
938 | The password for the Lavalink node
939 |
940 |
941 |
942 |
943 |
944 |
945 |
946 |
947 |
948 |
949 |
950 |
951 |
952 |
953 |
954 |
955 |
956 |
957 |
958 |
959 |
960 |
961 |
962 |
963 |
964 |
965 | Returns:
966 |
967 |
968 |
969 |
970 |
971 |
972 | Type
973 |
974 |
975 |
976 | void
977 |
978 |
979 |
980 |
981 |
982 |
983 |
984 |
985 |
986 |
987 |
988 |
989 |
990 | (async) join(guildId, channelId, options, playeropt ) → {Promise.<Player >}
991 |
992 |
993 |
994 |
995 |
996 |
997 |
998 |
999 |
1000 | Source:
1001 |
1004 |
1005 |
1006 |
1007 |
1008 |
1009 |
1010 |
1011 |
1012 |
1013 |
1014 |
1015 |
1016 |
1017 |
1018 |
1019 |
1020 |
1021 |
1022 |
1023 |
1024 |
1025 |
1026 |
1027 |
1028 |
1029 |
1030 |
1031 |
1032 |
1033 |
1034 |
1035 |
1036 |
1037 |
1038 |
1039 |
1040 |
1041 |
1042 | Join a voice channel
1043 |
1044 |
1045 |
1046 |
1047 |
1048 |
1049 |
1050 |
1051 |
1052 |
1053 |
1054 |
1055 | Parameters:
1056 |
1057 |
1058 |
1059 |
1060 |
1061 |
1062 | Name
1063 |
1064 |
1065 | Type
1066 |
1067 |
1068 | Attributes
1069 |
1070 |
1071 |
1072 |
1073 | Description
1074 |
1075 |
1076 |
1077 |
1078 |
1079 |
1080 |
1081 |
1082 | guildId
1083 |
1084 |
1085 |
1086 |
1087 |
1088 | string
1089 |
1090 |
1091 |
1092 |
1093 |
1094 |
1095 |
1096 |
1097 |
1098 |
1099 |
1100 |
1101 |
1102 |
1103 |
1104 |
1105 |
1106 | The guild ID
1107 |
1108 |
1109 |
1110 |
1111 |
1112 |
1113 | channelId
1114 |
1115 |
1116 |
1117 |
1118 |
1119 | string
1120 |
1121 |
1122 |
1123 |
1124 |
1125 |
1126 |
1127 |
1128 |
1129 |
1130 |
1131 |
1132 |
1133 |
1134 |
1135 |
1136 |
1137 | The channel ID
1138 |
1139 |
1140 |
1141 |
1142 |
1143 |
1144 | options
1145 |
1146 |
1147 |
1148 |
1149 |
1150 | Object
1151 |
1152 |
1153 |
1154 |
1155 |
1156 |
1157 |
1158 |
1159 |
1160 |
1161 |
1162 |
1163 |
1164 |
1165 |
1166 |
1167 |
1168 | Join options
1169 |
1170 |
1171 |
1172 |
1173 |
1174 |
1175 | player
1176 |
1177 |
1178 |
1179 |
1180 |
1181 | Player
1182 |
1183 |
1184 |
1185 |
1186 |
1187 |
1188 |
1189 |
1190 | <optional>
1191 |
1192 |
1193 |
1194 |
1195 |
1196 |
1197 |
1198 |
1199 |
1200 |
1201 | Optionally pass an existing player
1202 |
1203 |
1204 |
1205 |
1206 |
1207 |
1208 |
1209 |
1210 |
1211 |
1212 |
1213 |
1214 |
1215 |
1216 |
1217 |
1218 |
1219 |
1220 |
1221 | Returns:
1222 |
1223 |
1224 |
1225 |
1226 |
1227 |
1228 | Type
1229 |
1230 |
1231 |
1232 | Promise.<Player >
1233 |
1234 |
1235 |
1236 |
1237 |
1238 |
1239 |
1240 |
1241 |
1242 |
1243 |
1244 |
1245 |
1246 | (async) leave(guildId) → {void}
1247 |
1248 |
1249 |
1250 |
1251 |
1252 |
1253 |
1254 |
1255 |
1256 | Source:
1257 |
1260 |
1261 |
1262 |
1263 |
1264 |
1265 |
1266 |
1267 |
1268 |
1269 |
1270 |
1271 |
1272 |
1273 |
1274 |
1275 |
1276 |
1277 |
1278 |
1279 |
1280 |
1281 |
1282 |
1283 |
1284 |
1285 |
1286 |
1287 |
1288 |
1289 |
1290 |
1291 |
1292 |
1293 |
1294 |
1295 |
1296 |
1297 |
1298 | Leave a voice channel
1299 |
1300 |
1301 |
1302 |
1303 |
1304 |
1305 |
1306 |
1307 |
1308 |
1309 |
1310 |
1311 | Parameters:
1312 |
1313 |
1314 |
1315 |
1316 |
1317 |
1318 | Name
1319 |
1320 |
1321 | Type
1322 |
1323 |
1324 |
1325 |
1326 |
1327 | Description
1328 |
1329 |
1330 |
1331 |
1332 |
1333 |
1334 |
1335 |
1336 | guildId
1337 |
1338 |
1339 |
1340 |
1341 |
1342 | string
1343 |
1344 |
1345 |
1346 |
1347 |
1348 |
1349 |
1350 |
1351 |
1352 | The guild ID
1353 |
1354 |
1355 |
1356 |
1357 |
1358 |
1359 |
1360 |
1361 |
1362 |
1363 |
1364 |
1365 |
1366 |
1367 |
1368 |
1369 |
1370 |
1371 |
1372 | Returns:
1373 |
1374 |
1375 |
1376 |
1377 |
1378 |
1379 | Type
1380 |
1381 |
1382 |
1383 | void
1384 |
1385 |
1386 |
1387 |
1388 |
1389 |
1390 |
1391 |
1392 |
1393 |
1394 |
1395 |
1396 |
1397 | removeNode(host) → {void}
1398 |
1399 |
1400 |
1401 |
1402 |
1403 |
1404 |
1405 |
1406 |
1407 | Source:
1408 |
1411 |
1412 |
1413 |
1414 |
1415 |
1416 |
1417 |
1418 |
1419 |
1420 |
1421 |
1422 |
1423 |
1424 |
1425 |
1426 |
1427 |
1428 |
1429 |
1430 |
1431 |
1432 |
1433 |
1434 |
1435 |
1436 |
1437 |
1438 |
1439 |
1440 |
1441 |
1442 |
1443 |
1444 |
1445 |
1446 |
1447 |
1448 |
1449 | Remove a Lavalink node
1450 |
1451 |
1452 |
1453 |
1454 |
1455 |
1456 |
1457 |
1458 |
1459 |
1460 |
1461 |
1462 | Parameters:
1463 |
1464 |
1465 |
1466 |
1467 |
1468 |
1469 | Name
1470 |
1471 |
1472 | Type
1473 |
1474 |
1475 |
1476 |
1477 |
1478 | Description
1479 |
1480 |
1481 |
1482 |
1483 |
1484 |
1485 |
1486 |
1487 | host
1488 |
1489 |
1490 |
1491 |
1492 |
1493 | string
1494 |
1495 |
1496 |
1497 |
1498 |
1499 |
1500 |
1501 |
1502 |
1503 | The hostname of the node
1504 |
1505 |
1506 |
1507 |
1508 |
1509 |
1510 |
1511 |
1512 |
1513 |
1514 |
1515 |
1516 |
1517 |
1518 |
1519 |
1520 |
1521 |
1522 |
1523 | Returns:
1524 |
1525 |
1526 |
1527 |
1528 |
1529 |
1530 | Type
1531 |
1532 |
1533 |
1534 | void
1535 |
1536 |
1537 |
1538 |
1539 |
1540 |
1541 |
1542 |
1543 |
1544 |
1545 |
1546 |
1547 |
1548 | switchNode(player, leave) → {void}
1549 |
1550 |
1551 |
1552 |
1553 |
1554 |
1555 |
1556 |
1557 |
1558 | Source:
1559 |
1562 |
1563 |
1564 |
1565 |
1566 |
1567 |
1568 |
1569 |
1570 |
1571 |
1572 |
1573 |
1574 |
1575 |
1576 |
1577 |
1578 |
1579 |
1580 |
1581 |
1582 |
1583 |
1584 |
1585 |
1586 |
1587 |
1588 |
1589 |
1590 |
1591 |
1592 |
1593 |
1594 |
1595 |
1596 |
1597 |
1598 |
1599 |
1600 | Switch the voice node of a player
1601 |
1602 |
1603 |
1604 |
1605 |
1606 |
1607 |
1608 |
1609 |
1610 |
1611 |
1612 |
1613 | Parameters:
1614 |
1615 |
1616 |
1617 |
1618 |
1619 |
1620 | Name
1621 |
1622 |
1623 | Type
1624 |
1625 |
1626 |
1627 |
1628 |
1629 | Description
1630 |
1631 |
1632 |
1633 |
1634 |
1635 |
1636 |
1637 |
1638 | player
1639 |
1640 |
1641 |
1642 |
1643 |
1644 | Player
1645 |
1646 |
1647 |
1648 |
1649 |
1650 |
1651 |
1652 |
1653 |
1654 | The Player instance
1655 |
1656 |
1657 |
1658 |
1659 |
1660 |
1661 | leave
1662 |
1663 |
1664 |
1665 |
1666 |
1667 | boolean
1668 |
1669 |
1670 |
1671 |
1672 |
1673 |
1674 |
1675 |
1676 |
1677 | Whether to leave the channel or not on our side
1678 |
1679 |
1680 |
1681 |
1682 |
1683 |
1684 |
1685 |
1686 |
1687 |
1688 |
1689 |
1690 |
1691 |
1692 |
1693 |
1694 |
1695 |
1696 |
1697 | Returns:
1698 |
1699 |
1700 |
1701 |
1702 |
1703 |
1704 | Type
1705 |
1706 |
1707 |
1708 | void
1709 |
1710 |
1711 |
1712 |
1713 |
1714 |
1715 |
1716 |
1717 |
1718 |
1719 |
1720 |
1721 |
1722 |
1723 |
1724 |
1725 |
1726 |
1727 |
1728 |
1729 |
1730 |
1731 |
1732 |
1733 |
1734 |
1735 | Documentation generated by JSDoc 3.5.5 on Mon Jan 01 2018 08:16:30 GMT-0500 (EST) using the docdash theme.
1736 |
1737 |
1738 |
1739 |
1740 |
1741 |
--------------------------------------------------------------------------------
/docs/PlayerManager.js.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | PlayerManager.js - Documentation
6 |
7 |
8 |
9 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | Classes
26 |
27 |
28 |
29 |
30 |
PlayerManager.js
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 | /**
41 | * Created by Julian & NoobLance on 25.05.2017.
42 | * DISCLAIMER: We reuse some eris code
43 | */
44 |
45 | const { Collection } = require('eris');
46 | const Lavalink = require('./Lavalink');
47 | const Player = require('./Player');
48 |
49 | /**
50 | * Player Manager
51 | * @extends Map
52 | * @prop {Player} baseObject The player class used to create new players
53 | * @prop {object} client The eris client
54 | * @prop {object} defaultRegions The default region config
55 | * @prop {object} regions The region config being used
56 | */
57 | class PlayerManager extends Collection {
58 | /**
59 | * PlayerManager constructor
60 | * @param {Client} client Eris client
61 | * @param {Object[]} nodes The Lavalink nodes to connect to
62 | * @param {Object} [options] Setup options
63 | * @param {string} [options.defaultRegion] The default region
64 | * @param {number} [options.failoverRate=250] Failover rate in ms
65 | * @param {number} [options.failoverLimit=1] Number of connections to failover per rate limit
66 | * @param {Object} [options.player] Optional Player class to replace the default Player
67 | * @param {number} [options.reconnectThreshold=2000] The amount of time to skip ahead in a song when reconnecting in ms
68 | * @param {Object} [options.regions] Region mapping object
69 | */
70 | constructor(client, nodes, options) {
71 | super(options.player || Player);
72 |
73 | this.client = client;
74 | this.nodes = new Collection();
75 | this.pendingGuilds = {};
76 | this.options = options || {};
77 | this.failoverQueue = [];
78 | this.failoverRate = options.failoverRate || 250;
79 | this.failoverLimit = options.failoverLimit || 1;
80 |
81 | this.defaultRegions = {
82 | asia: ['hongkong', 'singapore', 'sydney'],
83 | eu: ['eu', 'amsterdam', 'frankfurt', 'russia'],
84 | us: ['us', 'brazil'],
85 | };
86 |
87 | this.regions = options.regions || this.defaultRegions;
88 |
89 | for (let node of nodes) {
90 | this.createNode(Object.assign({}, node, options));
91 | }
92 |
93 | this.shardReadyListener = this.shardReady.bind(this);
94 | this.client.on('shardReady', this.shardReadyListener);
95 | }
96 |
97 | /**
98 | * Create a Lavalink node
99 | * @param {Object} options Lavalink node options
100 | * @param {string} options.host The hostname to connect to
101 | * @param {string} options.port The port to connect with
102 | * @param {string} options.region The region of the node
103 | * @param {number} options.numShards The number of shards the bot is running
104 | * @param {string} options.userId The user id of the bot
105 | * @param {string} options.password The password for the Lavalink node
106 | * @returns {void}
107 | */
108 | createNode(options) {
109 | let node = new Lavalink({
110 | host: options.host,
111 | port: options.port,
112 | region: options.region,
113 | numShards: options.numShards,
114 | userId: options.userId,
115 | password: options.password,
116 | });
117 |
118 | node.on('error', this.onError.bind(this, node));
119 | node.on('disconnect', this.onDisconnect.bind(this, node));
120 | node.on('message', this.onMessage.bind(this, node));
121 |
122 | this.nodes.set(options.host, node);
123 | }
124 |
125 | /**
126 | * Remove a Lavalink node
127 | * @param {string} host The hostname of the node
128 | * @returns {void}
129 | */
130 | removeNode(host) {
131 | let node = this.nodes.get(host);
132 | if (!host) return;
133 | node.destroy();
134 | this.nodes.delete(host);
135 | this.onDisconnect(node);
136 | }
137 |
138 | /**
139 | * Check the failover queue
140 | * @private
141 | */
142 | checkFailoverQueue() {
143 | if (this.failoverQueue.length > 0) {
144 | let fns = this.failoverQueue.splice(0, this.failoverLimit);
145 | for (let fn of fns) {
146 | this.processQueue(fn);
147 | }
148 | }
149 | }
150 |
151 | /**
152 | * Queue a failover
153 | * @param {Function} fn The failover function to queue
154 | * @private
155 | */
156 | queueFailover(fn) {
157 | if (this.failoverQueue.length > 0) {
158 | this.failoverQueue.push(fn);
159 | } else {
160 | return this.processQueue(fn);
161 | }
162 | }
163 |
164 | /**
165 | * Process the failover queue
166 | * @param {Function} fn The failover function to call
167 | * @private
168 | */
169 | processQueue(fn) {
170 | fn();
171 | setTimeout(() => this.checkFailoverQueue(), this.failoverRate);
172 | }
173 |
174 | /**
175 | * Called when an error is received from a Lavalink node
176 | * @param {Lavalink} node The Lavalink node
177 | * @param {string|Error} err The error received
178 | * @private
179 | */
180 | onError(node, err) {
181 | this.client.emit(err);
182 | }
183 |
184 | /**
185 | * Called when a node disconnects
186 | * @param {Lavalink} node The Lavalink node
187 | * @param {*} msg The disconnect message if sent
188 | * @private
189 | */
190 | onDisconnect(node, msg) {
191 | let players = this.filter(player => player.node.host === node.host);
192 | for (let player of players) {
193 | this.queueFailover(this.switchNode.bind(this, player, true));
194 | }
195 | }
196 |
197 | /**
198 | * Called when a shard readies
199 | * @param {number} id Shard ID
200 | * @private
201 | */
202 | shardReady(id) {
203 | let players = this.filter(player => player.shard && player.shard.id === id);
204 | for (let player of players) {
205 | this.queueFailover(this.switchNode.bind(this, player));
206 | }
207 | }
208 |
209 | /**
210 | * Switch the voice node of a player
211 | * @param {Player} player The Player instance
212 | * @param {boolean} leave Whether to leave the channel or not on our side
213 | * @returns {void}
214 | */
215 | switchNode(player, leave) {
216 | let { guildId, channelId, track } = player,
217 | position = (player.state.position || 0) + (this.options.reconnectThreshold || 2000);
218 |
219 | let listeners = player.listeners('end'),
220 | endListeners = [];
221 |
222 | if (listeners && listeners.length) {
223 | for (let listener of listeners) {
224 | endListeners.push(listener);
225 | player.removeListener('end', listener);
226 | }
227 | }
228 |
229 | player.once('end', () => {
230 | for (let listener of endListeners) {
231 | player.on('end', listener);
232 | }
233 | });
234 |
235 | this.delete(guildId);
236 |
237 | player.playing = false;
238 |
239 | if (leave) {
240 | player.updateVoiceState(null);
241 | } else {
242 | player.node.send({ op: 'disconnect', guildId: guildId });
243 | }
244 |
245 | process.nextTick(() => {
246 | this.join(guildId, channelId, null, player).then(player => {
247 | player.play(track, { startTime: position });
248 | player.emit('reconnect');
249 | this.set(guildId, player);
250 | })
251 | .catch(err => {
252 | player.emit('disconnect', err);
253 | player.disconnect();
254 | });
255 | });
256 | }
257 |
258 | /**
259 | * Called when a message is received from the voice node
260 | * @param {Lavalink} node The Lavalink node
261 | * @param {*} message The message received
262 | * @private
263 | */
264 | onMessage(node, message) {
265 | if (!message.op) return;
266 |
267 | switch (message.op) {
268 | case 'validationReq': {
269 | let payload = {
270 | op: 'validationRes',
271 | guildId: message.guildId,
272 | };
273 |
274 | let guildValid = false;
275 | let channelValid = false;
276 |
277 | if (message.guildId && message.guildId.length) {
278 | guildValid = this.client.guilds.has(message.guildId);
279 | } else {
280 | guildValid = true;
281 | }
282 |
283 | if (message.channelId && message.channelId.length) {
284 | let voiceChannel = this.client.getChannel(message.channelId);
285 | if (voiceChannel) {
286 | payload.channelId = voiceChannel.id;
287 | channelValid = true;
288 | }
289 | } else {
290 | channelValid = true;
291 | }
292 |
293 | payload.valid = guildValid && channelValid;
294 |
295 | return node.send(payload);
296 | }
297 | case 'isConnectedReq': {
298 | let payload = {
299 | op: 'isConnectedRes',
300 | shardId: parseInt(message.shardId),
301 | connected: false,
302 | };
303 |
304 | let shard = this.client.shards.get(message.shardId);
305 | if (shard && (shard.status === 'connected' || shard.status === 'ready')) {
306 | payload.connected = true;
307 | }
308 |
309 | return node.send(payload);
310 | }
311 | case 'sendWS': {
312 | let shard = this.client.shards.get(message.shardId);
313 | if (shard === undefined) return;
314 |
315 | const payload = JSON.parse(message.message);
316 |
317 | shard.sendWS(payload.op, payload.d);
318 |
319 | if (payload.op === 4 && payload.d.channel_id === null) {
320 | this.delete(payload.d.guild_id);
321 | }
322 | }
323 | case 'playerUpdate': {
324 | let player = this.get(message.guildId);
325 | if (!player) return;
326 |
327 | return player.stateUpdate(message.state);
328 | }
329 | case 'event': {
330 | let player = this.get(message.guildId);
331 | if (!player) return;
332 |
333 | switch (message.type) {
334 | case 'TrackEndEvent':
335 | return player.onTrackEnd(message);
336 | case 'TrackExceptionEvent':
337 | return player.onTrackException(message);
338 | case 'TrackStuckEvent':
339 | return player.onTrackStuck(message);
340 | default:
341 | return player.emit('warn', `Unexpected event type: ${message.type}`);
342 | }
343 | }
344 | }
345 | }
346 |
347 | /**
348 | * Join a voice channel
349 | * @param {string} guildId The guild ID
350 | * @param {string} channelId The channel ID
351 | * @param {Object} options Join options
352 | * @param {Player} [player] Optionally pass an existing player
353 | * @returns {Promise<Player>}
354 | */
355 | async join(guildId, channelId, options, player) {
356 | options = options || {};
357 |
358 | player = player || this.get(guildId);
359 | if (player && player.channelId !== channelId) {
360 | player.switchChannel(channelId);
361 | return Promise.resolve(player);
362 | }
363 |
364 | let region = this.getRegionFromData(options.region || 'us');
365 | let node = await this.findIdealNode(region);
366 |
367 | if (!node) {
368 | return Promise.reject('No available voice nodes.');
369 | }
370 |
371 | return new Promise((res, rej) => {
372 | this.pendingGuilds[guildId] = {
373 | channelId: channelId,
374 | options: options || {},
375 | player: player || null,
376 | node: node,
377 | res: res,
378 | rej: rej,
379 | timeout: setTimeout(() => {
380 | node.send({ op: 'disconnect', guildId: guildId });
381 | delete this.pendingGuilds[guildId];
382 | rej(new Error('Voice connection timeout'));
383 | }, 10000),
384 | };
385 |
386 | node.send({
387 | op: 'connect',
388 | guildId: guildId,
389 | channelId: channelId,
390 | });
391 | });
392 | }
393 |
394 | /**
395 | * Leave a voice channel
396 | * @param {string} guildId The guild ID
397 | * @returns {void}
398 | */
399 | async leave(guildId) {
400 | let player = this.get(guildId);
401 | if (!player) {
402 | return;
403 | }
404 | player.disconnect();
405 | this.delete(player);
406 | }
407 |
408 | /**
409 | * Find the ideal voice node based on load and region
410 | * @param {string} region Guild region
411 | * @private
412 | */
413 | async findIdealNode(region) {
414 | let nodes = [...this.nodes.values()].filter(node => !node.draining && node.ws && node.connected);
415 |
416 | if (region) {
417 | let regionalNodes = nodes.filter(node => node.region === region);
418 | if (regionalNodes && regionalNodes.length) {
419 | nodes = regionalNodes;
420 | }
421 | }
422 |
423 | nodes = nodes.sort((a, b) => {
424 | let aload = a.stats.cpu ? (a.stats.cpu.systemLoad / a.stats.cpu.cores) * 100 : 0,
425 | bload = b.stats.cpu ? (b.stats.cpu.systemLoad / b.stats.cpu.cores) * 100 : 0;
426 | return aload - bload;
427 | });
428 | return nodes[0];
429 | }
430 |
431 | /**
432 | * Called by eris when a voice server update is received
433 | * @param {*} data The voice server update from eris
434 | * @private
435 | */
436 | async voiceServerUpdate(data) {
437 | if (this.pendingGuilds[data.guild_id] && this.pendingGuilds[data.guild_id].timeout) {
438 | clearTimeout(this.pendingGuilds[data.guild_id].timeout);
439 | this.pendingGuilds[data.guild_id].timeout = null;
440 | }
441 |
442 | let player = this.get(data.guild_id);
443 | if (!player) {
444 | if (!this.pendingGuilds[data.guild_id]) {
445 | return;
446 | }
447 |
448 | player = this.pendingGuilds[data.guild_id].player;
449 |
450 | if (player) {
451 | player.sessionId = data.sessionId;
452 | player.hostname = this.pendingGuilds[data.guild_id].hostname;
453 | player.node = this.pendingGuilds[data.guild_id].node;
454 | player.event = data;
455 | this.set(data.guild_id, player);
456 | }
457 |
458 | player = player || this.add(new this.baseObject(data.guild_id, {
459 | shard: data.shard,
460 | guildId: data.guild_id,
461 | sessionId: data.session_id,
462 | channelId: this.pendingGuilds[data.guild_id].channelId,
463 | hostname: this.pendingGuilds[data.guild_id].hostname,
464 | node: this.pendingGuilds[data.guild_id].node,
465 | options: this.pendingGuilds[data.guild_id].options,
466 | event: data,
467 | manager: this,
468 | }));
469 |
470 | player.connect({
471 | sessionId: data.session_id,
472 | guildId: data.guild_id,
473 | channelId: this.pendingGuilds[data.guild_id].channelId,
474 | event: {
475 | endpoint: data.endpoint,
476 | guild_id: data.guild_id,
477 | token: data.token,
478 | },
479 | });
480 | }
481 |
482 | let disconnectHandler = () => {
483 | player = this.get(data.guild_id);
484 | if (!this.pendingGuilds[data.guild_id]) {
485 | if (player) {
486 | player.removeListener('ready', readyHandler);
487 | }
488 | return;
489 | }
490 | player.removeListener('ready', readyHandler);
491 | this.pendingGuilds[data.guild_id].rej(new Error('Disconnected'));
492 | delete this.pendingGuilds[data.guild_id];
493 | };
494 |
495 | let readyHandler = () => {
496 | player = this.get(data.guild_id);
497 | if (!this.pendingGuilds[data.guild_id]) {
498 | if (player) {
499 | player.removeListener('disconnect', disconnectHandler);
500 | }
501 | return;
502 | }
503 | player.removeListener('disconnect', disconnectHandler);
504 | this.pendingGuilds[data.guild_id].res(player);
505 | delete this.pendingGuilds[data.guild_id];
506 | };
507 |
508 | player.once('ready', readyHandler).once('disconnect', disconnectHandler);
509 | }
510 |
511 | /**
512 | * Get ideal region from data
513 | * @param {string} endpoint Endpoint or region
514 | * @private
515 | */
516 | getRegionFromData(endpoint) {
517 | if (!endpoint) return this.options.defaultRegion || 'us';
518 |
519 | endpoint = endpoint.replace('vip-', '');
520 |
521 | for (let key in this.regions) {
522 | let nodes = this.nodes.filter(n => n.region === key);
523 | if (!nodes || !nodes.length) continue;
524 | if (!nodes.find(n => n.connected && !n.draining)) continue;
525 | for (let region of this.regions[key]) {
526 | if (endpoint.startsWith(region)) {
527 | return key;
528 | }
529 | }
530 | }
531 |
532 | return this.options.defaultRegion || 'us';
533 | }
534 | }
535 |
536 | module.exports = PlayerManager;
537 |
538 |
539 |
540 |
541 |
542 |
543 |
544 |
545 |
546 |
547 |
548 |
549 | Documentation generated by JSDoc 3.5.5 on Mon Jan 01 2018 08:16:30 GMT-0500 (EST) using the docdash theme.
550 |
551 |
552 |
553 |
554 |
555 |
556 |
--------------------------------------------------------------------------------
/docs/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Home - Documentation
6 |
7 |
8 |
9 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | Classes
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 | eris-lavalink A Lavalink client for eris
50 | Links
56 | Install npm install eris-lavalink
Implementation Start by creating the PlayerManager
and passing a list of nodes and optional list of regions
57 | const { PlayerManager } = require('eris-lavalink');
58 |
59 | let nodes = [
60 | { host: 'localhost', port: 8080, region: 'eu', password: 'youshallnotpass' }
61 | ];
62 |
63 | let regions = {
64 | eu: ['eu', 'amsterdam', 'frankfurt', 'russia', 'hongkong', 'singapore', 'sydney'],
65 | us: ['us', 'brazil'],
66 | };
67 |
68 | if (!(client.voiceConnections instanceof PlayerManager)) {
69 | client.voiceConnections = new PlayerManager(client, nodes, {
70 | numShards: shardCount, // number of shards
71 | userId: userid, // the user id of the bot
72 | regions: regions,
73 | defaultRegion: 'eu',
74 | });
75 | }
To resolve a track, use the Lavalink rest api
76 | const superagent = require('superagent');
77 |
78 | async function resolveTracks(node, search) {
79 | try {
80 | var result = await superagent.get(`http://${node.host}:2333/loadtracks?identifier=${search}`)
81 | .set('Authorization', node.password)
82 | .set('Accept', 'application/json');
83 | } catch (err) {
84 | throw err;
85 | }
86 |
87 | if (!result) {
88 | throw 'Unable play that video.';
89 | }
90 |
91 | return result.body; // array of tracks resolved from lavalink
92 | }
93 |
94 | resolveTracks(node, 'ytsearch:the 30 second video').then(tracks => {
95 | if (!tracks) {
96 | // no tracks to play
97 | }
98 | // do something with the tracks
99 | })
To join and leave voice channels, use the Lavalink client rather than using eris.
100 | // to get or join a channel
101 | function getPlayer(channel) {
102 | if (!channel || !channel.guild) {
103 | return Promise.reject('Not a guild channel.');
104 | }
105 |
106 | let player = client.voiceConnections.get(channel.guild.id);
107 | if (player) {
108 | return Promise.resolve(player);
109 | }
110 |
111 | let options = {};
112 | if (channel.guild.region) {
113 | options.region = channel.guild.region;
114 | }
115 |
116 | return client.voiceConnections.join(channel.guild.id, channel.id, options);
117 | }
118 |
119 | // play example
120 | getPlayer(channel).then(player => {
121 | player.play(track); // track is the base64 track we get from Lavalink
122 |
123 | player.on('disconnect', (err) => {
124 | if (err) {
125 | // log error
126 | }
127 | // do something
128 | });
129 |
130 | player.on('error', err => {
131 | // log error and handle it
132 | });
133 |
134 | player.on('stuck', msg => {
135 | // track stuck event
136 | })
137 |
138 | player.once('end', data => {
139 | // REPLACED reason is emitted when playing without stopping, I ignore these to prevent skip loops
140 | if (data.reason && data.reason === 'REPLACED') {
141 | return;
142 | }
143 |
144 | // start playing the next song
145 | });
146 | });
147 |
148 | // stop example
149 | getPlayer(channel).then(player => {
150 | player.stop();
151 | if (leave) {
152 | // disconnect and leave the channel
153 | player.leave();
154 | }
155 | })
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 | Documentation generated by JSDoc 3.5.5 on Mon Jan 01 2018 08:16:30 GMT-0500 (EST) using the docdash theme.
169 |
170 |
171 |
172 |
173 |
174 |
--------------------------------------------------------------------------------
/docs/scripts/linenumber.js:
--------------------------------------------------------------------------------
1 | /*global document */
2 | (function() {
3 | var source = document.getElementsByClassName('prettyprint source linenums');
4 | var i = 0;
5 | var lineNumber = 0;
6 | var lineId;
7 | var lines;
8 | var totalLines;
9 | var anchorHash;
10 |
11 | if (source && source[0]) {
12 | anchorHash = document.location.hash.substring(1);
13 | lines = source[0].getElementsByTagName('li');
14 | totalLines = lines.length;
15 |
16 | for (; i < totalLines; i++) {
17 | lineNumber++;
18 | lineId = 'line' + lineNumber;
19 | lines[i].id = lineId;
20 | if (lineId === anchorHash) {
21 | lines[i].className += ' selected';
22 | }
23 | }
24 | }
25 | })();
26 |
--------------------------------------------------------------------------------
/docs/scripts/prettify/Apache-License-2.0.txt:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/docs/scripts/prettify/lang-css.js:
--------------------------------------------------------------------------------
1 | PR.registerLangHandler(PR.createSimpleLexer([["pln",/^[\t\n\f\r ]+/,null," \t\r\n"]],[["str",/^"(?:[^\n\f\r"\\]|\\(?:\r\n?|\n|\f)|\\[\S\s])*"/,null],["str",/^'(?:[^\n\f\r'\\]|\\(?:\r\n?|\n|\f)|\\[\S\s])*'/,null],["lang-css-str",/^url\(([^"')]*)\)/i],["kwd",/^(?:url|rgb|!important|@import|@page|@media|@charset|inherit)(?=[^\w-]|$)/i,null],["lang-css-kw",/^(-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*)\s*:/i],["com",/^\/\*[^*]*\*+(?:[^*/][^*]*\*+)*\//],["com",
2 | /^(?:<\!--|--\>)/],["lit",/^(?:\d+|\d*\.\d+)(?:%|[a-z]+)?/i],["lit",/^#[\da-f]{3,6}/i],["pln",/^-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*/i],["pun",/^[^\s\w"']+/]]),["css"]);PR.registerLangHandler(PR.createSimpleLexer([],[["kwd",/^-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*/i]]),["css-kw"]);PR.registerLangHandler(PR.createSimpleLexer([],[["str",/^[^"')]+/]]),["css-str"]);
3 |
--------------------------------------------------------------------------------
/docs/scripts/prettify/prettify.js:
--------------------------------------------------------------------------------
1 | var q=null;window.PR_SHOULD_USE_CONTINUATION=!0;
2 | (function(){function L(a){function m(a){var f=a.charCodeAt(0);if(f!==92)return f;var b=a.charAt(1);return(f=r[b])?f:"0"<=b&&b<="7"?parseInt(a.substring(1),8):b==="u"||b==="x"?parseInt(a.substring(2),16):a.charCodeAt(1)}function e(a){if(a<32)return(a<16?"\\x0":"\\x")+a.toString(16);a=String.fromCharCode(a);if(a==="\\"||a==="-"||a==="["||a==="]")a="\\"+a;return a}function h(a){for(var f=a.substring(1,a.length-1).match(/\\u[\dA-Fa-f]{4}|\\x[\dA-Fa-f]{2}|\\[0-3][0-7]{0,2}|\\[0-7]{1,2}|\\[\S\s]|[^\\]/g),a=
3 | [],b=[],o=f[0]==="^",c=o?1:0,i=f.length;c122||(d<65||j>90||b.push([Math.max(65,j)|32,Math.min(d,90)|32]),d<97||j>122||b.push([Math.max(97,j)&-33,Math.min(d,122)&-33]))}}b.sort(function(a,f){return a[0]-f[0]||f[1]-a[1]});f=[];j=[NaN,NaN];for(c=0;ci[0]&&(i[1]+1>i[0]&&b.push("-"),b.push(e(i[1])));b.push("]");return b.join("")}function y(a){for(var f=a.source.match(/\[(?:[^\\\]]|\\[\S\s])*]|\\u[\dA-Fa-f]{4}|\\x[\dA-Fa-f]{2}|\\\d+|\\[^\dux]|\(\?[!:=]|[()^]|[^()[\\^]+/g),b=f.length,d=[],c=0,i=0;c=2&&a==="["?f[c]=h(j):a!=="\\"&&(f[c]=j.replace(/[A-Za-z]/g,function(a){a=a.charCodeAt(0);return"["+String.fromCharCode(a&-33,a|32)+"]"}));return f.join("")}for(var t=0,s=!1,l=!1,p=0,d=a.length;p=5&&"lang-"===b.substring(0,5))&&!(o&&typeof o[1]==="string"))c=!1,b="src";c||(r[f]=b)}i=d;d+=f.length;if(c){c=o[1];var j=f.indexOf(c),k=j+c.length;o[2]&&(k=f.length-o[2].length,j=k-c.length);b=b.substring(5);B(l+i,f.substring(0,j),e,p);B(l+i+j,c,C(b,c),p);B(l+i+k,f.substring(k),e,p)}else p.push(l+i,b)}a.e=p}var h={},y;(function(){for(var e=a.concat(m),
9 | l=[],p={},d=0,g=e.length;d=0;)h[n.charAt(k)]=r;r=r[1];n=""+r;p.hasOwnProperty(n)||(l.push(r),p[n]=q)}l.push(/[\S\s]/);y=L(l)})();var t=m.length;return e}function u(a){var m=[],e=[];a.tripleQuotedStrings?m.push(["str",/^(?:'''(?:[^'\\]|\\[\S\s]|''?(?=[^']))*(?:'''|$)|"""(?:[^"\\]|\\[\S\s]|""?(?=[^"]))*(?:"""|$)|'(?:[^'\\]|\\[\S\s])*(?:'|$)|"(?:[^"\\]|\\[\S\s])*(?:"|$))/,q,"'\""]):a.multiLineStrings?m.push(["str",/^(?:'(?:[^'\\]|\\[\S\s])*(?:'|$)|"(?:[^"\\]|\\[\S\s])*(?:"|$)|`(?:[^\\`]|\\[\S\s])*(?:`|$))/,
10 | q,"'\"`"]):m.push(["str",/^(?:'(?:[^\n\r'\\]|\\.)*(?:'|$)|"(?:[^\n\r"\\]|\\.)*(?:"|$))/,q,"\"'"]);a.verbatimStrings&&e.push(["str",/^@"(?:[^"]|"")*(?:"|$)/,q]);var h=a.hashComments;h&&(a.cStyleComments?(h>1?m.push(["com",/^#(?:##(?:[^#]|#(?!##))*(?:###|$)|.*)/,q,"#"]):m.push(["com",/^#(?:(?:define|elif|else|endif|error|ifdef|include|ifndef|line|pragma|undef|warning)\b|[^\n\r]*)/,q,"#"]),e.push(["str",/^<(?:(?:(?:\.\.\/)*|\/?)(?:[\w-]+(?:\/[\w-]+)+)?[\w-]+\.h|[a-z]\w*)>/,q])):m.push(["com",/^#[^\n\r]*/,
11 | q,"#"]));a.cStyleComments&&(e.push(["com",/^\/\/[^\n\r]*/,q]),e.push(["com",/^\/\*[\S\s]*?(?:\*\/|$)/,q]));a.regexLiterals&&e.push(["lang-regex",/^(?:^^\.?|[!+-]|!=|!==|#|%|%=|&|&&|&&=|&=|\(|\*|\*=|\+=|,|-=|->|\/|\/=|:|::|;|<|<<|<<=|<=|=|==|===|>|>=|>>|>>=|>>>|>>>=|[?@[^]|\^=|\^\^|\^\^=|{|\||\|=|\|\||\|\|=|~|break|case|continue|delete|do|else|finally|instanceof|return|throw|try|typeof)\s*(\/(?=[^*/])(?:[^/[\\]|\\[\S\s]|\[(?:[^\\\]]|\\[\S\s])*(?:]|$))+\/)/]);(h=a.types)&&e.push(["typ",h]);a=(""+a.keywords).replace(/^ | $/g,
12 | "");a.length&&e.push(["kwd",RegExp("^(?:"+a.replace(/[\s,]+/g,"|")+")\\b"),q]);m.push(["pln",/^\s+/,q," \r\n\t\xa0"]);e.push(["lit",/^@[$_a-z][\w$@]*/i,q],["typ",/^(?:[@_]?[A-Z]+[a-z][\w$@]*|\w+_t\b)/,q],["pln",/^[$_a-z][\w$@]*/i,q],["lit",/^(?:0x[\da-f]+|(?:\d(?:_\d+)*\d*(?:\.\d*)?|\.\d\+)(?:e[+-]?\d+)?)[a-z]*/i,q,"0123456789"],["pln",/^\\[\S\s]?/,q],["pun",/^.[^\s\w"-$'./@\\`]*/,q]);return x(m,e)}function D(a,m){function e(a){switch(a.nodeType){case 1:if(k.test(a.className))break;if("BR"===a.nodeName)h(a),
13 | a.parentNode&&a.parentNode.removeChild(a);else for(a=a.firstChild;a;a=a.nextSibling)e(a);break;case 3:case 4:if(p){var b=a.nodeValue,d=b.match(t);if(d){var c=b.substring(0,d.index);a.nodeValue=c;(b=b.substring(d.index+d[0].length))&&a.parentNode.insertBefore(s.createTextNode(b),a.nextSibling);h(a);c||a.parentNode.removeChild(a)}}}}function h(a){function b(a,d){var e=d?a.cloneNode(!1):a,f=a.parentNode;if(f){var f=b(f,1),g=a.nextSibling;f.appendChild(e);for(var h=g;h;h=g)g=h.nextSibling,f.appendChild(h)}return e}
14 | for(;!a.nextSibling;)if(a=a.parentNode,!a)return;for(var a=b(a.nextSibling,0),e;(e=a.parentNode)&&e.nodeType===1;)a=e;d.push(a)}var k=/(?:^|\s)nocode(?:\s|$)/,t=/\r\n?|\n/,s=a.ownerDocument,l;a.currentStyle?l=a.currentStyle.whiteSpace:window.getComputedStyle&&(l=s.defaultView.getComputedStyle(a,q).getPropertyValue("white-space"));var p=l&&"pre"===l.substring(0,3);for(l=s.createElement("LI");a.firstChild;)l.appendChild(a.firstChild);for(var d=[l],g=0;g=0;){var h=m[e];A.hasOwnProperty(h)?window.console&&console.warn("cannot override language handler %s",h):A[h]=a}}function C(a,m){if(!a||!A.hasOwnProperty(a))a=/^\s*=o&&(h+=2);e>=c&&(a+=2)}}catch(w){"console"in window&&console.log(w&&w.stack?w.stack:w)}}var v=["break,continue,do,else,for,if,return,while"],w=[[v,"auto,case,char,const,default,double,enum,extern,float,goto,int,long,register,short,signed,sizeof,static,struct,switch,typedef,union,unsigned,void,volatile"],
18 | "catch,class,delete,false,import,new,operator,private,protected,public,this,throw,true,try,typeof"],F=[w,"alignof,align_union,asm,axiom,bool,concept,concept_map,const_cast,constexpr,decltype,dynamic_cast,explicit,export,friend,inline,late_check,mutable,namespace,nullptr,reinterpret_cast,static_assert,static_cast,template,typeid,typename,using,virtual,where"],G=[w,"abstract,boolean,byte,extends,final,finally,implements,import,instanceof,null,native,package,strictfp,super,synchronized,throws,transient"],
19 | H=[G,"as,base,by,checked,decimal,delegate,descending,dynamic,event,fixed,foreach,from,group,implicit,in,interface,internal,into,is,lock,object,out,override,orderby,params,partial,readonly,ref,sbyte,sealed,stackalloc,string,select,uint,ulong,unchecked,unsafe,ushort,var"],w=[w,"debugger,eval,export,function,get,null,set,undefined,var,with,Infinity,NaN"],I=[v,"and,as,assert,class,def,del,elif,except,exec,finally,from,global,import,in,is,lambda,nonlocal,not,or,pass,print,raise,try,with,yield,False,True,None"],
20 | J=[v,"alias,and,begin,case,class,def,defined,elsif,end,ensure,false,in,module,next,nil,not,or,redo,rescue,retry,self,super,then,true,undef,unless,until,when,yield,BEGIN,END"],v=[v,"case,done,elif,esac,eval,fi,function,in,local,set,then,until"],K=/^(DIR|FILE|vector|(de|priority_)?queue|list|stack|(const_)?iterator|(multi)?(set|map)|bitset|u?(int|float)\d*)/,N=/\S/,O=u({keywords:[F,H,w,"caller,delete,die,do,dump,elsif,eval,exit,foreach,for,goto,if,import,last,local,my,next,no,our,print,package,redo,require,sub,undef,unless,until,use,wantarray,while,BEGIN,END"+
21 | I,J,v],hashComments:!0,cStyleComments:!0,multiLineStrings:!0,regexLiterals:!0}),A={};k(O,["default-code"]);k(x([],[["pln",/^[^]+/],["dec",/^]*(?:>|$)/],["com",/^<\!--[\S\s]*?(?:--\>|$)/],["lang-",/^<\?([\S\s]+?)(?:\?>|$)/],["lang-",/^<%([\S\s]+?)(?:%>|$)/],["pun",/^(?:<[%?]|[%?]>)/],["lang-",/^]*>([\S\s]+?)<\/xmp\b[^>]*>/i],["lang-js",/^