├── .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 | 21 | 22 | 23 | 24 | 27 | 28 |
29 | 30 |

Lavalink

31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 |
39 | 40 |
41 | 42 |

43 | Lavalink 44 |

45 | 46 |
Represents a Lavalink node
47 | 48 | 49 |
50 | 51 |
52 |
53 | 54 | 55 | 56 | 57 |

Constructor

58 | 59 | 60 | 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 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 |
NameTypeDescription
host 139 | 140 | 141 | string 142 | 143 | 144 | 145 | The hostname for the node
port 162 | 163 | 164 | number 165 | 166 | 167 | 168 | The port number for the node
address 185 | 186 | 187 | string 188 | 189 | 190 | 191 | The full ws address for the node
region 208 | 209 | 210 | string 211 | 212 | 213 | 214 | The region for this node
userId 231 | 232 | 233 | string 234 | 235 | 236 | 237 | The client user id
numShards 254 | 255 | 256 | number 257 | 258 | 259 | 260 | The total number of shards the bot is running
password 277 | 278 | 279 | string 280 | 281 | 282 | 283 | The password used to connect
connected 300 | 301 | 302 | boolean 303 | 304 | 305 | 306 | If it's connected to the node
draining 323 | 324 | 325 | boolean 326 | 327 | 328 | 329 | True if this node will no longer take new connections
stats 346 | 347 | 348 | object 349 | 350 | 351 | 352 | The Lavalink node stats
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 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 420 | 421 | 422 | 423 | 424 | 425 | 705 | 706 | 707 | 708 | 709 |
NameTypeDescription
options 413 | 414 | 415 | Object 416 | 417 | 418 | 419 | Lavalink node options 426 |
Properties
427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 466 | 467 | 468 | 475 | 476 | 477 | 478 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 501 | 502 | 503 | 510 | 511 | 512 | 513 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 536 | 537 | 538 | 545 | 546 | 547 | 548 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 571 | 572 | 573 | 580 | 581 | 582 | 583 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 606 | 607 | 608 | 615 | 616 | 617 | 618 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 641 | 642 | 643 | 650 | 651 | 652 | 653 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 676 | 677 | 678 | 687 | 688 | 689 | 690 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 |
NameTypeAttributesDefaultDescription
host 459 | 460 | 461 | string 462 | 463 | 464 | 465 | 469 | 470 | 471 | 472 | 473 | 474 | 479 | 480 | The hostname to connect to
port 494 | 495 | 496 | string 497 | 498 | 499 | 500 | 504 | 505 | 506 | 507 | 508 | 509 | 514 | 515 | The port to connect with
region 529 | 530 | 531 | string 532 | 533 | 534 | 535 | 539 | 540 | 541 | 542 | 543 | 544 | 549 | 550 | The region of the node
numShards 564 | 565 | 566 | number 567 | 568 | 569 | 570 | 574 | 575 | 576 | 577 | 578 | 579 | 584 | 585 | The number of shards the bot is running
userId 599 | 600 | 601 | string 602 | 603 | 604 | 605 | 609 | 610 | 611 | 612 | 613 | 614 | 619 | 620 | The user id of the bot
password 634 | 635 | 636 | string 637 | 638 | 639 | 640 | 644 | 645 | 646 | 647 | 648 | 649 | 654 | 655 | The password for the Lavalink node
timeout 669 | 670 | 671 | number 672 | 673 | 674 | 675 | 679 | 680 | <optional>
681 | 682 | 683 | 684 | 685 | 686 |
691 | 692 | 5000 693 | 694 | Optional timeout in ms used for the reconnect backoff
703 | 704 |
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 |
    736 |
  • EventEmitter
  • 737 |
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 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 976 | 977 | 978 |
NameTypeDescription
op 938 | 939 | 940 | string 941 | 942 | 943 | 944 | Op name
data 961 | 962 | 963 | * 964 | 965 | 966 | 967 | Data to send
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 | 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 | 21 | 22 | 23 | 24 | 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 | 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 | 21 | 22 | 23 | 24 | 27 | 28 |
29 | 30 |

Player

31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 |
39 | 40 |
41 | 42 |

43 | Player 44 |

45 | 46 |
Represents a player/voice connection to Lavalink
47 | 48 | 49 |
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 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 |
NameTypeDescription
id 139 | 140 | 141 | string 142 | 143 | 144 | 145 | Guild id for the player
manager 162 | 163 | 164 | PlayerManager 165 | 166 | 167 | 168 | Reference to the player manager
node 185 | 186 | 187 | Lavalink 188 | 189 | 190 | 191 | Lavalink node the player is connected to
shard 208 | 209 | 210 | object 211 | 212 | 213 | 214 | The eris shard the player is associated with
hostname 231 | 232 | 233 | string 234 | 235 | 236 | 237 | Hostname of the lavalink node
guildId 254 | 255 | 256 | string 257 | 258 | 259 | 260 | Guild ID
channelId 277 | 278 | 279 | string 280 | 281 | 282 | 283 | Channel ID
ready 300 | 301 | 302 | boolean 303 | 304 | 305 | 306 | If the connection is ready
playing 323 | 324 | 325 | boolean 326 | 327 | 328 | 329 | If the player is playing
state 346 | 347 | 348 | object 349 | 350 | 351 | 352 | The lavalink player state
track 369 | 370 | 371 | string 372 | 373 | 374 | 375 | The lavalink track to play
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 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 466 | 467 | 468 | 469 | 470 | 471 | 719 | 720 | 721 | 722 | 723 |
NameTypeDescription
id 436 | 437 | 438 | string 439 | 440 | 441 | 442 | Guild ID
data 459 | 460 | 461 | Object 462 | 463 | 464 | 465 | Player data 472 |
Properties
473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 510 | 511 | 512 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 541 | 542 | 543 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 572 | 573 | 574 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 603 | 604 | 605 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 634 | 635 | 636 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 665 | 666 | 667 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 696 | 697 | 698 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 |
NameTypeAttributesDescription
channelId 503 | 504 | 505 | string 506 | 507 | 508 | 509 | 513 | 514 | 515 | 516 | 517 | 518 | The channel id of the player
guildId 534 | 535 | 536 | string 537 | 538 | 539 | 540 | 544 | 545 | 546 | 547 | 548 | 549 | The guild id of the player
hostname 565 | 566 | 567 | string 568 | 569 | 570 | 571 | 575 | 576 | 577 | 578 | 579 | 580 | The hostname of the lavalink node
manager 596 | 597 | 598 | PlayerManager 599 | 600 | 601 | 602 | 606 | 607 | 608 | 609 | 610 | 611 | The PlayerManager associated with this player
node 627 | 628 | 629 | Lavalink 630 | 631 | 632 | 633 | 637 | 638 | 639 | 640 | 641 | 642 | The Lavalink node associated with this player
shard 658 | 659 | 660 | Shard 661 | 662 | 663 | 664 | 668 | 669 | 670 | 671 | 672 | 673 | The eris shard associated with this player
options 689 | 690 | 691 | Object 692 | 693 | 694 | 695 | 699 | 700 | <optional>
701 | 702 | 703 | 704 | 705 | 706 |
Additional passed from the user to the player
717 | 718 |
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 |
    750 |
  • EventEmitter
  • 751 |
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 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 875 | 876 | 877 | 878 | 879 | 880 | 976 | 977 | 978 | 979 | 980 |
NameTypeDescription
data 868 | 869 | 870 | Object 871 | 872 | 873 | 874 | The data used to connect 881 |
Properties
882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 |
NameTypeDescription
guildId 910 | 911 | 912 | string 913 | 914 | 915 | 916 | The guild ID to connect
sessionId 933 | 934 | 935 | string 936 | 937 | 938 | 939 | The voice connection session ID
event 956 | 957 | 958 | object 959 | 960 | 961 | 962 | The event data from the voice server update
974 | 975 |
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 | 1093 | 1094 | 1095 | 1096 | 1097 | 1098 | 1099 | 1100 | 1101 | 1102 | 1103 | 1104 | 1105 | 1106 | 1107 | 1108 | 1109 | 1110 | 1111 | 1112 | 1113 | 1114 | 1115 | 1123 | 1124 | 1125 | 1134 | 1135 | 1136 | 1137 | 1138 | 1139 | 1140 | 1141 | 1142 | 1143 |
NameTypeAttributesDescription
msg 1116 | 1117 | 1118 | * 1119 | 1120 | 1121 | 1122 | 1126 | 1127 | <optional>
1128 | 1129 | 1130 | 1131 | 1132 | 1133 |
An optional disconnect message
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 | 1256 | 1257 | 1258 | 1259 | 1260 | 1261 | 1262 | 1263 | 1264 | 1265 | 1266 | 1267 | 1268 | 1269 | 1270 | 1271 | 1272 | 1273 | 1274 | 1275 | 1276 | 1277 | 1278 | 1286 | 1287 | 1288 | 1295 | 1296 | 1297 | 1298 | 1299 | 1300 | 1301 | 1302 | 1303 | 1304 | 1305 | 1306 | 1307 | 1308 | 1309 | 1317 | 1318 | 1319 | 1328 | 1329 | 1330 | 1331 | 1332 | 1333 | 1334 | 1335 | 1336 | 1337 |
NameTypeAttributesDescription
track 1279 | 1280 | 1281 | string 1282 | 1283 | 1284 | 1285 | 1289 | 1290 | 1291 | 1292 | 1293 | 1294 | The track to play
options 1310 | 1311 | 1312 | Object 1313 | 1314 | 1315 | 1316 | 1320 | 1321 | <optional>
1322 | 1323 | 1324 | 1325 | 1326 | 1327 |
Optional options to send
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 | 1450 | 1451 | 1452 | 1453 | 1454 | 1455 | 1456 | 1457 | 1458 | 1459 | 1460 | 1461 | 1462 | 1463 | 1464 | 1465 | 1466 | 1467 | 1468 | 1469 | 1470 | 1478 | 1479 | 1480 | 1481 | 1482 | 1483 | 1484 | 1485 | 1486 | 1487 | 1488 |
NameTypeDescription
position 1471 | 1472 | 1473 | number 1474 | 1475 | 1476 | 1477 | The position to seek to
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 | 1601 | 1602 | 1603 | 1604 | 1605 | 1606 | 1607 | 1608 | 1609 | 1610 | 1611 | 1612 | 1613 | 1614 | 1615 | 1616 | 1617 | 1618 | 1619 | 1620 | 1621 | 1629 | 1630 | 1631 | 1632 | 1633 | 1634 | 1635 | 1636 | 1637 | 1638 | 1639 |
NameTypeDescription
pause 1622 | 1623 | 1624 | boolean 1625 | 1626 | 1627 | 1628 | Set pause to true/false
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 | 1752 | 1753 | 1754 | 1755 | 1756 | 1757 | 1758 | 1759 | 1760 | 1761 | 1762 | 1763 | 1764 | 1765 | 1766 | 1767 | 1768 | 1769 | 1770 | 1771 | 1772 | 1780 | 1781 | 1782 | 1783 | 1784 | 1785 | 1786 | 1787 | 1788 | 1789 | 1790 |
NameTypeDescription
volume 1773 | 1774 | 1775 | number 1776 | 1777 | 1778 | 1779 | The volume level to set
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 | 2005 | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | 2024 | 2025 | 2026 | 2027 | 2035 | 2036 | 2037 | 2044 | 2045 | 2046 | 2047 | 2048 | 2049 | 2050 | 2051 | 2052 | 2053 | 2054 | 2055 | 2056 | 2057 | 2058 | 2066 | 2067 | 2068 | 2077 | 2078 | 2079 | 2080 | 2081 | 2082 | 2083 | 2084 | 2085 | 2086 |
NameTypeAttributesDescription
channelId 2028 | 2029 | 2030 | string 2031 | 2032 | 2033 | 2034 | 2038 | 2039 | 2040 | 2041 | 2042 | 2043 | Called when switching channels
reactive 2059 | 2060 | 2061 | boolean 2062 | 2063 | 2064 | 2065 | 2069 | 2070 | <optional>
2071 | 2072 | 2073 | 2074 | 2075 | 2076 |
Used if you want the bot to switch channels
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 | 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 | 21 | 22 | 23 | 24 | 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 | 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 | 21 | 22 | 23 | 24 | 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 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 |
NameTypeDescription
baseObject 139 | 140 | 141 | Player 142 | 143 | 144 | 145 | The player class used to create new players
client 162 | 163 | 164 | object 165 | 166 | 167 | 168 | The eris client
defaultRegions 185 | 186 | 187 | object 188 | 189 | 190 | 191 | The default region config
regions 208 | 209 | 210 | object 211 | 212 | 213 | 214 | The region config being used
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 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 284 | 285 | 286 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 315 | 316 | 317 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 346 | 347 | 348 | 357 | 358 | 359 | 360 | 361 | 620 | 621 | 622 | 623 | 624 |
NameTypeAttributesDescription
client 277 | 278 | 279 | Client 280 | 281 | 282 | 283 | 287 | 288 | 289 | 290 | 291 | 292 | Eris client
nodes 308 | 309 | 310 | Array.<Object> 311 | 312 | 313 | 314 | 318 | 319 | 320 | 321 | 322 | 323 | The Lavalink nodes to connect to
options 339 | 340 | 341 | Object 342 | 343 | 344 | 345 | 349 | 350 | <optional>
351 | 352 | 353 | 354 | 355 | 356 |
Setup options 362 |
Properties
363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 402 | 403 | 404 | 413 | 414 | 415 | 416 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 439 | 440 | 441 | 450 | 451 | 452 | 453 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 478 | 479 | 480 | 489 | 490 | 491 | 492 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 517 | 518 | 519 | 528 | 529 | 530 | 531 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 554 | 555 | 556 | 565 | 566 | 567 | 568 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 593 | 594 | 595 | 604 | 605 | 606 | 607 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 |
NameTypeAttributesDefaultDescription
defaultRegion 395 | 396 | 397 | string 398 | 399 | 400 | 401 | 405 | 406 | <optional>
407 | 408 | 409 | 410 | 411 | 412 |
417 | 418 | The default region
failoverRate 432 | 433 | 434 | number 435 | 436 | 437 | 438 | 442 | 443 | <optional>
444 | 445 | 446 | 447 | 448 | 449 |
454 | 455 | 250 456 | 457 | Failover rate in ms
failoverLimit 471 | 472 | 473 | number 474 | 475 | 476 | 477 | 481 | 482 | <optional>
483 | 484 | 485 | 486 | 487 | 488 |
493 | 494 | 1 495 | 496 | Number of connections to failover per rate limit
player 510 | 511 | 512 | Object 513 | 514 | 515 | 516 | 520 | 521 | <optional>
522 | 523 | 524 | 525 | 526 | 527 |
532 | 533 | Optional Player class to replace the default Player
reconnectThreshold 547 | 548 | 549 | number 550 | 551 | 552 | 553 | 557 | 558 | <optional>
559 | 560 | 561 | 562 | 563 | 564 |
569 | 570 | 2000 571 | 572 | The amount of time to skip ahead in a song when reconnecting in ms
regions 586 | 587 | 588 | Object 589 | 590 | 591 | 592 | 596 | 597 | <optional>
598 | 599 | 600 | 601 | 602 | 603 |
608 | 609 | Region mapping object
618 | 619 |
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 |
    651 |
  • Map
  • 652 |
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 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 776 | 777 | 778 | 779 | 780 | 781 | 946 | 947 | 948 | 949 | 950 |
NameTypeDescription
options 769 | 770 | 771 | Object 772 | 773 | 774 | 775 | Lavalink node options 782 |
Properties
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 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 |
NameTypeDescription
host 811 | 812 | 813 | string 814 | 815 | 816 | 817 | The hostname to connect to
port 834 | 835 | 836 | string 837 | 838 | 839 | 840 | The port to connect with
region 857 | 858 | 859 | string 860 | 861 | 862 | 863 | The region of the node
numShards 880 | 881 | 882 | number 883 | 884 | 885 | 886 | The number of shards the bot is running
userId 903 | 904 | 905 | string 906 | 907 | 908 | 909 | The user id of the bot
password 926 | 927 | 928 | string 929 | 930 | 931 | 932 | The password for the Lavalink node
944 | 945 |
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 | 1063 | 1064 | 1065 | 1066 | 1067 | 1068 | 1069 | 1070 | 1071 | 1072 | 1073 | 1074 | 1075 | 1076 | 1077 | 1078 | 1079 | 1080 | 1081 | 1082 | 1083 | 1084 | 1085 | 1093 | 1094 | 1095 | 1102 | 1103 | 1104 | 1105 | 1106 | 1107 | 1108 | 1109 | 1110 | 1111 | 1112 | 1113 | 1114 | 1115 | 1116 | 1124 | 1125 | 1126 | 1133 | 1134 | 1135 | 1136 | 1137 | 1138 | 1139 | 1140 | 1141 | 1142 | 1143 | 1144 | 1145 | 1146 | 1147 | 1155 | 1156 | 1157 | 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | 1170 | 1171 | 1172 | 1173 | 1174 | 1175 | 1176 | 1177 | 1178 | 1186 | 1187 | 1188 | 1197 | 1198 | 1199 | 1200 | 1201 | 1202 | 1203 | 1204 | 1205 | 1206 |
NameTypeAttributesDescription
guildId 1086 | 1087 | 1088 | string 1089 | 1090 | 1091 | 1092 | 1096 | 1097 | 1098 | 1099 | 1100 | 1101 | The guild ID
channelId 1117 | 1118 | 1119 | string 1120 | 1121 | 1122 | 1123 | 1127 | 1128 | 1129 | 1130 | 1131 | 1132 | The channel ID
options 1148 | 1149 | 1150 | Object 1151 | 1152 | 1153 | 1154 | 1158 | 1159 | 1160 | 1161 | 1162 | 1163 | Join options
player 1179 | 1180 | 1181 | Player 1182 | 1183 | 1184 | 1185 | 1189 | 1190 | <optional>
1191 | 1192 | 1193 | 1194 | 1195 | 1196 |
Optionally pass an existing player
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 | 1319 | 1320 | 1321 | 1322 | 1323 | 1324 | 1325 | 1326 | 1327 | 1328 | 1329 | 1330 | 1331 | 1332 | 1333 | 1334 | 1335 | 1336 | 1337 | 1338 | 1339 | 1347 | 1348 | 1349 | 1350 | 1351 | 1352 | 1353 | 1354 | 1355 | 1356 | 1357 |
NameTypeDescription
guildId 1340 | 1341 | 1342 | string 1343 | 1344 | 1345 | 1346 | The guild ID
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 | 1470 | 1471 | 1472 | 1473 | 1474 | 1475 | 1476 | 1477 | 1478 | 1479 | 1480 | 1481 | 1482 | 1483 | 1484 | 1485 | 1486 | 1487 | 1488 | 1489 | 1490 | 1498 | 1499 | 1500 | 1501 | 1502 | 1503 | 1504 | 1505 | 1506 | 1507 | 1508 |
NameTypeDescription
host 1491 | 1492 | 1493 | string 1494 | 1495 | 1496 | 1497 | The hostname of the node
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 | 1621 | 1622 | 1623 | 1624 | 1625 | 1626 | 1627 | 1628 | 1629 | 1630 | 1631 | 1632 | 1633 | 1634 | 1635 | 1636 | 1637 | 1638 | 1639 | 1640 | 1641 | 1649 | 1650 | 1651 | 1652 | 1653 | 1654 | 1655 | 1656 | 1657 | 1658 | 1659 | 1660 | 1661 | 1662 | 1663 | 1664 | 1672 | 1673 | 1674 | 1675 | 1676 | 1677 | 1678 | 1679 | 1680 | 1681 | 1682 |
NameTypeDescription
player 1642 | 1643 | 1644 | Player 1645 | 1646 | 1647 | 1648 | The Player instance
leave 1665 | 1666 | 1667 | boolean 1668 | 1669 | 1670 | 1671 | Whether to leave the channel or not on our side
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 | 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 | 21 | 22 | 23 | 24 | 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 | 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 | 21 | 22 | 23 | 24 | 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 | 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",/^[^]*(?:>|$)/],["com",/^<\!--[\S\s]*?(?:--\>|$)/],["lang-",/^<\?([\S\s]+?)(?:\?>|$)/],["lang-",/^<%([\S\s]+?)(?:%>|$)/],["pun",/^(?:<[%?]|[%?]>)/],["lang-",/^]*>([\S\s]+?)<\/xmp\b[^>]*>/i],["lang-js",/^]*>([\S\s]*?)(<\/script\b[^>]*>)/i],["lang-css",/^]*>([\S\s]*?)(<\/style\b[^>]*>)/i],["lang-in.tag",/^(<\/?[a-z][^<>]*>)/i]]), 22 | ["default-markup","htm","html","mxml","xhtml","xml","xsl"]);k(x([["pln",/^\s+/,q," \t\r\n"],["atv",/^(?:"[^"]*"?|'[^']*'?)/,q,"\"'"]],[["tag",/^^<\/?[a-z](?:[\w-.:]*\w)?|\/?>$/i],["atn",/^(?!style[\s=]|on)[a-z](?:[\w:-]*\w)?/i],["lang-uq.val",/^=\s*([^\s"'>]*(?:[^\s"'/>]|\/(?=\s)))/],["pun",/^[/<->]+/],["lang-js",/^on\w+\s*=\s*"([^"]+)"/i],["lang-js",/^on\w+\s*=\s*'([^']+)'/i],["lang-js",/^on\w+\s*=\s*([^\s"'>]+)/i],["lang-css",/^style\s*=\s*"([^"]+)"/i],["lang-css",/^style\s*=\s*'([^']+)'/i],["lang-css", 23 | /^style\s*=\s*([^\s"'>]+)/i]]),["in.tag"]);k(x([],[["atv",/^[\S\s]+/]]),["uq.val"]);k(u({keywords:F,hashComments:!0,cStyleComments:!0,types:K}),["c","cc","cpp","cxx","cyc","m"]);k(u({keywords:"null,true,false"}),["json"]);k(u({keywords:H,hashComments:!0,cStyleComments:!0,verbatimStrings:!0,types:K}),["cs"]);k(u({keywords:G,cStyleComments:!0}),["java"]);k(u({keywords:v,hashComments:!0,multiLineStrings:!0}),["bsh","csh","sh"]);k(u({keywords:I,hashComments:!0,multiLineStrings:!0,tripleQuotedStrings:!0}), 24 | ["cv","py"]);k(u({keywords:"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",hashComments:!0,multiLineStrings:!0,regexLiterals:!0}),["perl","pl","pm"]);k(u({keywords:J,hashComments:!0,multiLineStrings:!0,regexLiterals:!0}),["rb"]);k(u({keywords:w,cStyleComments:!0,regexLiterals:!0}),["js"]);k(u({keywords:"all,and,by,catch,class,else,extends,false,finally,for,if,in,is,isnt,loop,new,no,not,null,of,off,on,or,return,super,then,true,try,unless,until,when,while,yes", 25 | hashComments:3,cStyleComments:!0,multilineStrings:!0,tripleQuotedStrings:!0,regexLiterals:!0}),["coffee"]);k(x([],[["str",/^[\S\s]+/]]),["regex"]);window.prettyPrintOne=function(a,m,e){var h=document.createElement("PRE");h.innerHTML=a;e&&D(h,e);E({g:m,i:e,h:h});return h.innerHTML};window.prettyPrint=function(a){function m(){for(var e=window.PR_SHOULD_USE_CONTINUATION?l.now()+250:Infinity;p=0){var k=k.match(g),f,b;if(b= 26 | !k){b=n;for(var o=void 0,c=b.firstChild;c;c=c.nextSibling)var i=c.nodeType,o=i===1?o?b:c:i===3?N.test(c.nodeValue)?b:o:o;b=(f=o===b?void 0:o)&&"CODE"===f.tagName}b&&(k=f.className.match(g));k&&(k=k[1]);b=!1;for(o=n.parentNode;o;o=o.parentNode)if((o.tagName==="pre"||o.tagName==="code"||o.tagName==="xmp")&&o.className&&o.className.indexOf("prettyprint")>=0){b=!0;break}b||((b=(b=n.className.match(/\blinenums\b(?::(\d+))?/))?b[1]&&b[1].length?+b[1]:!0:!1)&&D(n,b),d={g:k,h:n,i:b},E(d))}}p ul { 202 | padding: 0 10px; 203 | } 204 | 205 | nav > ul > li > a { 206 | color: #606; 207 | } 208 | 209 | nav ul ul { 210 | margin-bottom: 10px 211 | } 212 | 213 | nav ul ul + ul { 214 | margin-top: -10px; 215 | } 216 | 217 | nav ul ul a { 218 | color: hsl(207, 1%, 60%); 219 | border-left: 1px solid hsl(207, 10%, 86%); 220 | } 221 | 222 | nav ul ul a, 223 | nav ul ul a:active { 224 | padding-left: 20px 225 | } 226 | 227 | nav h2 { 228 | font-size: 12px; 229 | margin: 0; 230 | padding: 0; 231 | } 232 | 233 | nav > h2 > a { 234 | display: block; 235 | margin: 10px 0 -10px; 236 | color: #606 !important; 237 | } 238 | 239 | footer { 240 | color: hsl(0, 0%, 28%); 241 | margin-left: 250px; 242 | display: block; 243 | padding: 15px; 244 | font-style: italic; 245 | font-size: 90%; 246 | } 247 | 248 | .ancestors { 249 | color: #999 250 | } 251 | 252 | .ancestors a { 253 | color: #999 !important; 254 | } 255 | 256 | .clear { 257 | clear: both 258 | } 259 | 260 | .important { 261 | font-weight: bold; 262 | color: #950B02; 263 | } 264 | 265 | .yes-def { 266 | text-indent: -1000px 267 | } 268 | 269 | .type-signature { 270 | color: #CA79CA 271 | } 272 | 273 | .type-signature:last-child { 274 | color: #eee; 275 | } 276 | 277 | .name, .signature { 278 | font-family: Consolas, Monaco, 'Andale Mono', monospace 279 | } 280 | 281 | .signature { 282 | color: #fc83ff; 283 | } 284 | 285 | .details { 286 | margin-top: 6px; 287 | border-left: 2px solid #DDD; 288 | line-height: 20px; 289 | font-size: 14px; 290 | } 291 | 292 | .details dt { 293 | width: 120px; 294 | float: left; 295 | padding-left: 10px; 296 | } 297 | 298 | .details dd { 299 | margin-left: 70px; 300 | margin-top: 6px; 301 | margin-bottom: 6px; 302 | } 303 | 304 | .details ul { 305 | margin: 0 306 | } 307 | 308 | .details ul { 309 | list-style-type: none 310 | } 311 | 312 | .details pre.prettyprint { 313 | margin: 0 314 | } 315 | 316 | .details .object-value { 317 | padding-top: 0 318 | } 319 | 320 | .description { 321 | margin-bottom: 1em; 322 | margin-top: 1em; 323 | } 324 | 325 | .code-caption { 326 | font-style: italic; 327 | font-size: 107%; 328 | margin: 0; 329 | } 330 | 331 | .prettyprint { 332 | font-size: 14px; 333 | overflow: auto; 334 | } 335 | 336 | .prettyprint.source { 337 | width: inherit; 338 | line-height: 18px; 339 | display: block; 340 | background-color: #0d152a; 341 | color: #aeaeae; 342 | } 343 | 344 | .prettyprint code { 345 | line-height: 18px; 346 | display: block; 347 | background-color: #0d152a; 348 | color: #4D4E53; 349 | } 350 | 351 | .prettyprint > code { 352 | padding: 15px; 353 | } 354 | 355 | .prettyprint .linenums code { 356 | padding: 0 15px 357 | } 358 | 359 | .prettyprint .linenums li:first-of-type code { 360 | padding-top: 15px 361 | } 362 | 363 | .prettyprint code span.line { 364 | display: inline-block 365 | } 366 | 367 | .prettyprint.linenums { 368 | padding-left: 70px; 369 | -webkit-user-select: none; 370 | -moz-user-select: none; 371 | -ms-user-select: none; 372 | user-select: none; 373 | } 374 | 375 | .prettyprint.linenums ol { 376 | padding-left: 0 377 | } 378 | 379 | .prettyprint.linenums li { 380 | border-left: 3px #34446B solid; 381 | } 382 | 383 | .prettyprint.linenums li.selected, .prettyprint.linenums li.selected * { 384 | background-color: #34446B; 385 | } 386 | 387 | .prettyprint.linenums li * { 388 | -webkit-user-select: text; 389 | -moz-user-select: text; 390 | -ms-user-select: text; 391 | user-select: text; 392 | } 393 | 394 | .params, .props { 395 | border-spacing: 0; 396 | border: 1px solid #ddd; 397 | border-collapse: collapse; 398 | border-radius: 3px; 399 | box-shadow: 0 1px 3px rgba(0,0,0,0.1); 400 | width: 100%; 401 | font-size: 14px; 402 | margin: 1em 0; 403 | } 404 | 405 | .params .type { 406 | white-space: nowrap; 407 | } 408 | 409 | .params code { 410 | white-space: pre; 411 | } 412 | 413 | .params td, .params .name, .props .name, .name code { 414 | color: #4D4E53; 415 | font-family: Consolas, Monaco, 'Andale Mono', monospace; 416 | font-size: 100%; 417 | } 418 | 419 | .params td, .params th, .props td, .props th { 420 | margin: 0px; 421 | text-align: left; 422 | vertical-align: top; 423 | padding: 10px; 424 | display: table-cell; 425 | } 426 | 427 | .params td { 428 | border-top: 1px solid #eee 429 | } 430 | 431 | .params thead tr, .props thead tr { 432 | background-color: #fff; 433 | font-weight: bold; 434 | } 435 | 436 | .params .params thead tr, .props .props thead tr { 437 | background-color: #fff; 438 | font-weight: bold; 439 | } 440 | 441 | .params td.description > p:first-child, .props td.description > p:first-child { 442 | margin-top: 0; 443 | padding-top: 0; 444 | } 445 | 446 | .params td.description > p:last-child, .props td.description > p:last-child { 447 | margin-bottom: 0; 448 | padding-bottom: 0; 449 | } 450 | 451 | span.param-type, .params td .param-type, .param-type dd { 452 | color: #606; 453 | font-family: Consolas, Monaco, 'Andale Mono', monospace 454 | } 455 | 456 | .param-type dt, .param-type dd { 457 | display: inline-block 458 | } 459 | 460 | .param-type { 461 | margin: 14px 0; 462 | } 463 | 464 | .disabled { 465 | color: #454545 466 | } 467 | 468 | /* navicon button */ 469 | .navicon-button { 470 | display: none; 471 | position: relative; 472 | padding: 2.0625rem 1.5rem; 473 | transition: 0.25s; 474 | cursor: pointer; 475 | -webkit-user-select: none; 476 | -moz-user-select: none; 477 | -ms-user-select: none; 478 | user-select: none; 479 | opacity: .8; 480 | } 481 | .navicon-button .navicon:before, .navicon-button .navicon:after { 482 | transition: 0.25s; 483 | } 484 | .navicon-button:hover { 485 | transition: 0.5s; 486 | opacity: 1; 487 | } 488 | .navicon-button:hover .navicon:before, .navicon-button:hover .navicon:after { 489 | transition: 0.25s; 490 | } 491 | .navicon-button:hover .navicon:before { 492 | top: .825rem; 493 | } 494 | .navicon-button:hover .navicon:after { 495 | top: -.825rem; 496 | } 497 | 498 | /* navicon */ 499 | .navicon { 500 | position: relative; 501 | width: 2.5em; 502 | height: .3125rem; 503 | background: #000; 504 | transition: 0.3s; 505 | border-radius: 2.5rem; 506 | } 507 | .navicon:before, .navicon:after { 508 | display: block; 509 | content: ""; 510 | height: .3125rem; 511 | width: 2.5rem; 512 | background: #000; 513 | position: absolute; 514 | z-index: -1; 515 | transition: 0.3s 0.25s; 516 | border-radius: 1rem; 517 | } 518 | .navicon:before { 519 | top: .625rem; 520 | } 521 | .navicon:after { 522 | top: -.625rem; 523 | } 524 | 525 | /* open */ 526 | .nav-trigger:checked + label:not(.steps) .navicon:before, 527 | .nav-trigger:checked + label:not(.steps) .navicon:after { 528 | top: 0 !important; 529 | } 530 | 531 | .nav-trigger:checked + label .navicon:before, 532 | .nav-trigger:checked + label .navicon:after { 533 | transition: 0.5s; 534 | } 535 | 536 | /* Minus */ 537 | .nav-trigger:checked + label { 538 | -webkit-transform: scale(0.75); 539 | transform: scale(0.75); 540 | } 541 | 542 | /* × and + */ 543 | .nav-trigger:checked + label.plus .navicon, 544 | .nav-trigger:checked + label.x .navicon { 545 | background: transparent; 546 | } 547 | 548 | .nav-trigger:checked + label.plus .navicon:before, 549 | .nav-trigger:checked + label.x .navicon:before { 550 | -webkit-transform: rotate(-45deg); 551 | transform: rotate(-45deg); 552 | background: #FFF; 553 | } 554 | 555 | .nav-trigger:checked + label.plus .navicon:after, 556 | .nav-trigger:checked + label.x .navicon:after { 557 | -webkit-transform: rotate(45deg); 558 | transform: rotate(45deg); 559 | background: #FFF; 560 | } 561 | 562 | .nav-trigger:checked + label.plus { 563 | -webkit-transform: scale(0.75) rotate(45deg); 564 | transform: scale(0.75) rotate(45deg); 565 | } 566 | 567 | .nav-trigger:checked ~ nav { 568 | left: 0 !important; 569 | } 570 | 571 | .nav-trigger:checked ~ .overlay { 572 | display: block; 573 | } 574 | 575 | .nav-trigger { 576 | position: fixed; 577 | top: 0; 578 | clip: rect(0, 0, 0, 0); 579 | } 580 | 581 | .overlay { 582 | display: none; 583 | position: fixed; 584 | top: 0; 585 | bottom: 0; 586 | left: 0; 587 | right: 0; 588 | width: 100%; 589 | height: 100%; 590 | background: hsla(0, 0%, 0%, 0.5); 591 | z-index: 1; 592 | } 593 | 594 | @media only screen and (min-width: 320px) and (max-width: 680px) { 595 | body { 596 | overflow-x: hidden; 597 | } 598 | 599 | nav { 600 | background: #FFF; 601 | width: 250px; 602 | height: 100%; 603 | position: fixed; 604 | top: 0; 605 | right: 0; 606 | bottom: 0; 607 | left: -250px; 608 | z-index: 3; 609 | padding: 0 10px; 610 | transition: left 0.2s; 611 | } 612 | 613 | .navicon-button { 614 | display: inline-block; 615 | position: fixed; 616 | top: 1.5em; 617 | right: 0; 618 | z-index: 2; 619 | } 620 | 621 | #main { 622 | width: 100%; 623 | min-width: 360px; 624 | } 625 | 626 | #main h1.page-title { 627 | margin: 1em 0; 628 | } 629 | 630 | #main section { 631 | padding: 0; 632 | } 633 | 634 | footer { 635 | margin-left: 0; 636 | } 637 | } 638 | 639 | /** Add a '#' to static members */ 640 | [data-type="member"] a::before { 641 | content: '#'; 642 | display: inline-block; 643 | margin-left: -14px; 644 | margin-right: 5px; 645 | } 646 | -------------------------------------------------------------------------------- /docs/styles/prettify.css: -------------------------------------------------------------------------------- 1 | .pln { 2 | color: #ddd; 3 | } 4 | 5 | /* string content */ 6 | .str { 7 | color: #61ce3c; 8 | } 9 | 10 | /* a keyword */ 11 | .kwd { 12 | color: #fbde2d; 13 | } 14 | 15 | /* a comment */ 16 | .com { 17 | color: #aeaeae; 18 | } 19 | 20 | /* a type name */ 21 | .typ { 22 | color: #8da6ce; 23 | } 24 | 25 | /* a literal value */ 26 | .lit { 27 | color: #fbde2d; 28 | } 29 | 30 | /* punctuation */ 31 | .pun { 32 | color: #ddd; 33 | } 34 | 35 | /* lisp open bracket */ 36 | .opn { 37 | color: #000000; 38 | } 39 | 40 | /* lisp close bracket */ 41 | .clo { 42 | color: #000000; 43 | } 44 | 45 | /* a markup tag name */ 46 | .tag { 47 | color: #8da6ce; 48 | } 49 | 50 | /* a markup attribute name */ 51 | .atn { 52 | color: #fbde2d; 53 | } 54 | 55 | /* a markup attribute value */ 56 | .atv { 57 | color: #ddd; 58 | } 59 | 60 | /* a declaration */ 61 | .dec { 62 | color: #EF5050; 63 | } 64 | 65 | /* a variable name */ 66 | .var { 67 | color: #c82829; 68 | } 69 | 70 | /* a function name */ 71 | .fun { 72 | color: #4271ae; 73 | } 74 | 75 | /* Specify class=linenums on a pre to get line numbering */ 76 | ol.linenums { 77 | margin-top: 0; 78 | margin-bottom: 0; 79 | } 80 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eris-lavalink", 3 | "version": "1.0.2", 4 | "license": "MIT", 5 | "description": "A Lavalink client for eris", 6 | "homepage": "https://github.com/briantanner/eris-lavalink", 7 | "author": { 8 | "name": "Brian Tanner", 9 | "email": "brian@tanner.io" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/briantanner/eris-lavalink.git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/briantanner/eris-lavalink/issues" 17 | }, 18 | "main": "src/index.js", 19 | "engineStrict": true, 20 | "dependencies": { 21 | "eris": "github:abalabahaha/eris#dev", 22 | "ws": "^3.1.0" 23 | }, 24 | "devDependencies": { 25 | "docdash": "^0.4.0" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Lavalink.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const WebSocket = require('ws'); 4 | 5 | var EventEmitter; 6 | 7 | try { 8 | EventEmitter = require('eventemitter3'); 9 | } catch (err) { 10 | EventEmitter = require('events').EventEmitter; 11 | } 12 | 13 | /** 14 | * Represents a Lavalink node 15 | * @extends EventEmitter 16 | * @prop {string} host The hostname for the node 17 | * @prop {number} port The port number for the node 18 | * @prop {string} address The full ws address for the node 19 | * @prop {string} region The region for this node 20 | * @prop {string} userId The client user id 21 | * @prop {number} numShards The total number of shards the bot is running 22 | * @prop {string} password The password used to connect 23 | * @prop {boolean} connected If it's connected to the node 24 | * @prop {boolean} draining True if this node will no longer take new connections 25 | * @prop {object} stats The Lavalink node stats 26 | */ 27 | class Lavalink extends EventEmitter { 28 | /** 29 | * Lavalink constructor 30 | * @param {Object} options Lavalink node options 31 | * @param {string} options.host The hostname to connect to 32 | * @param {string} options.port The port to connect with 33 | * @param {string} options.region The region of the node 34 | * @param {number} options.numShards The number of shards the bot is running 35 | * @param {string} options.userId The user id of the bot 36 | * @param {string} options.password The password for the Lavalink node 37 | * @param {number} [options.timeout=5000] Optional timeout in ms used for the reconnect backoff 38 | */ 39 | constructor(options) { 40 | super(); 41 | 42 | this.host = options.host; 43 | this.port = options.port || 80; 44 | this.address = `ws://${this.host}:${this.port}`; 45 | this.region = options.region || null; 46 | this.userId = options.userId; 47 | this.numShards = options.numShards; 48 | this.password = options.password || 'youshallnotpass'; 49 | this.connected = false; 50 | this.draining = false; 51 | this.retries = 0; 52 | this.reconnectTimeout = options.timeout || 5000; 53 | this.reconnectInterval = null; 54 | this.stats = { players: 0, playingPlayers: 0 }; 55 | this.disconnectHandler = this.disconnected.bind(this); 56 | 57 | this.connect(); 58 | } 59 | 60 | /** 61 | * Connect to the websocket server 62 | * @private 63 | */ 64 | connect() { 65 | this.ws = new WebSocket(this.address, { 66 | headers: { 67 | 'Authorization': this.password, 68 | 'Num-Shards': this.numShards, 69 | 'User-Id': this.userId, 70 | }, 71 | }); 72 | 73 | this.ws.on('open', this.ready.bind(this)); 74 | this.ws.on('message', this.onMessage.bind(this)); 75 | this.ws.on('close', this.disconnectHandler); 76 | this.ws.on('error', (err) => { 77 | this.emit('error', err); 78 | }); 79 | } 80 | 81 | /** 82 | * Reconnect to the websocket 83 | * @private 84 | */ 85 | reconnect() { 86 | let interval = this.retryInterval(); 87 | this.reconnectInterval = setTimeout(this.reconnect.bind(this), interval); 88 | this.retries++; 89 | this.connect(); 90 | } 91 | 92 | /** 93 | * Destroy the websocket connection 94 | */ 95 | destroy() { 96 | if (this.ws) { 97 | this.ws.removeListener('close', this.disconnectHandler); 98 | this.ws.close(); 99 | } 100 | } 101 | 102 | /** 103 | * Called when the websocket is open 104 | * @private 105 | */ 106 | ready() { 107 | if (this.reconnectInterval) { 108 | clearTimeout(this.reconnectInterval); 109 | this.reconnectInterval = null; 110 | } 111 | 112 | this.connected = true; 113 | this.retries = 0; 114 | this.emit('ready'); 115 | } 116 | 117 | /** 118 | * Called when the websocket disconnects 119 | * @private 120 | */ 121 | disconnected() { 122 | this.connected = false; 123 | if (!this.reconnectInterval) { 124 | this.emit('disconnect'); 125 | } 126 | 127 | delete this.ws; 128 | 129 | if (!this.reconnectInterval) { 130 | this.reconnectInterval = setTimeout(this.reconnect.bind(this), this.reconnectTimeout); 131 | } 132 | } 133 | 134 | /** 135 | * Get the retry interval 136 | * @private 137 | */ 138 | retryInterval() { 139 | let retries = Math.min(this.retries-1, 5); 140 | return Math.pow(retries + 5, 2) * 1000; 141 | } 142 | 143 | /** 144 | * Send data to Lavalink 145 | * @param {string} op Op name 146 | * @param {*} data Data to send 147 | */ 148 | send(data) { 149 | const ws = this.ws; 150 | if (!ws) return; 151 | 152 | try { 153 | var payload = JSON.stringify(data); 154 | } catch (err) { 155 | return this.emit('error', 'Unable to stringify payload.'); 156 | } 157 | 158 | ws.send(payload); 159 | } 160 | 161 | /** 162 | * Handle message from the server 163 | * @param {string} message Raw websocket message 164 | * @private 165 | */ 166 | onMessage(message) { 167 | try { 168 | var data = JSON.parse(message); 169 | } catch (e) { 170 | return this.emit('error', 'Unable to parse ws message.'); 171 | } 172 | 173 | if (data.op && data.op === 'stats') { 174 | this.stats = data; 175 | } 176 | 177 | this.emit('message', data); 178 | } 179 | } 180 | 181 | module.exports = Lavalink; 182 | -------------------------------------------------------------------------------- /src/Player.js: -------------------------------------------------------------------------------- 1 | const Constants = require('eris').Constants; 2 | 3 | var EventEmitter; 4 | 5 | try { 6 | EventEmitter = require('eventemitter3'); 7 | } catch (err) { 8 | EventEmitter = require('events').EventEmitter; 9 | } 10 | 11 | /** 12 | * Represents a player/voice connection to Lavalink 13 | * @extends EventEmitter 14 | * @prop {string} id Guild id for the player 15 | * @prop {PlayerManager} manager Reference to the player manager 16 | * @prop {Lavalink} node Lavalink node the player is connected to 17 | * @prop {object} shard The eris shard the player is associated with 18 | * @prop {string} hostname Hostname of the lavalink node 19 | * @prop {string} guildId Guild ID 20 | * @prop {string} channelId Channel ID 21 | * @prop {boolean} ready If the connection is ready 22 | * @prop {boolean} playing If the player is playing 23 | * @prop {object} state The lavalink player state 24 | * @prop {string} track The lavalink track to play 25 | */ 26 | class Player extends EventEmitter { 27 | /** 28 | * Player constructor 29 | * @param {string} id Guild ID 30 | * @param {Object} data Player data 31 | * @param {string} data.channelId The channel id of the player 32 | * @param {string} data.guildId The guild id of the player 33 | * @param {string} data.hostname The hostname of the lavalink node 34 | * @param {PlayerManager} data.manager The PlayerManager associated with this player 35 | * @param {Lavalink} data.node The Lavalink node associated with this player 36 | * @param {Shard} data.shard The eris shard associated with this player 37 | * @param {Object} [data.options] Additional passed from the user to the player 38 | */ 39 | constructor(id, { hostname, guildId, channelId, shard, node, manager, options }) { 40 | super(); 41 | this.id = id; 42 | this.node = node; 43 | this.hostname = hostname; 44 | this.guildId = guildId; 45 | this.channelId = channelId; 46 | this.manager = manager || null; 47 | this.options = options; 48 | this.ready = false; 49 | this.playing = false; 50 | this.paused = false; 51 | this.shard = shard; 52 | this.state = {}; 53 | this.track = null; 54 | this.sendQueue = []; 55 | this.timestamp = Date.now(); 56 | } 57 | 58 | /** 59 | * Check the event queue 60 | * @private 61 | */ 62 | checkEventQueue() { 63 | if (this.sendQueue.length > 0) { 64 | let event = this.sendQueue.splice(0,1); 65 | this.sendEvent(event[0]); 66 | } 67 | } 68 | 69 | /** 70 | * Queue an event to be sent to Lavalink 71 | * @param {*} data The payload to queue 72 | * @private 73 | */ 74 | queueEvent(data) { 75 | if (this.sendQueue.length > 0) { 76 | this.sendQueue.push(data); 77 | } else { 78 | return this.sendEvent(data); 79 | } 80 | } 81 | 82 | /** 83 | * Send a payload to Lavalink 84 | * @param {*} data The payload to send 85 | * @private 86 | */ 87 | async sendEvent(data) { 88 | this.node.send(data); 89 | process.nextTick(() => this.checkEventQueue()); 90 | } 91 | 92 | /** 93 | * Connect to the Lavalink node 94 | * @param {Object} data The data used to connect 95 | * @param {string} data.guildId The guild ID to connect 96 | * @param {string} data.sessionId The voice connection session ID 97 | * @param {object} data.event The event data from the voice server update 98 | * @returns {void} 99 | */ 100 | connect(data) { 101 | this.emit('connect'); 102 | this.queueEvent({ 103 | op: 'voiceUpdate', 104 | guildId: data.guildId, 105 | sessionId: data.sessionId, 106 | event: data.event, 107 | }); 108 | 109 | process.nextTick(() => this.emit('ready')); 110 | } 111 | 112 | /** 113 | * Disconnect from Lavalink 114 | * @param {*} [msg] An optional disconnect message 115 | * @returns {void} 116 | */ 117 | disconnect(msg) { 118 | this._disconnect(); 119 | this.emit('disconnect', msg); 120 | } 121 | 122 | _disconnect() { 123 | this.playing = false; 124 | 125 | if (this.paused) { 126 | this.resume(); 127 | } 128 | 129 | this.queueEvent({ op: 'destroy', guildId: this.guildId }); 130 | 131 | this.stop(); 132 | } 133 | 134 | /** 135 | * Play a Lavalink track 136 | * @param {string} track The track to play 137 | * @param {Object} [options] Optional options to send 138 | * @returns {void} 139 | */ 140 | play(track, options) { 141 | this.lastTrack = this.track; 142 | this.track = track; 143 | this.playOptions = options; 144 | 145 | if (this.node.draining) { 146 | this.state.position = 0; 147 | return this.manager.switchNode(this); 148 | } 149 | 150 | let payload = Object.assign({ 151 | op: 'play', 152 | guildId: this.guildId, 153 | track: track, 154 | }, options); 155 | 156 | this.queueEvent(payload); 157 | this.playing = !this.paused; 158 | this.timestamp = Date.now(); 159 | } 160 | 161 | /** 162 | * Stop playing 163 | * @returns {void} 164 | */ 165 | stop() { 166 | let payload = { 167 | op: 'stop', 168 | guildId: this.guildId, 169 | }; 170 | 171 | this.queueEvent(payload); 172 | this.playing = false; 173 | this.lastTrack = this.track; 174 | this.track = null; 175 | } 176 | 177 | /** 178 | * Update player state 179 | * @param {Object} state The state object received from Lavalink 180 | * @private 181 | */ 182 | stateUpdate(state) { 183 | this.state = state; 184 | } 185 | 186 | /** 187 | * Used to pause/resume the player 188 | * @param {boolean} pause Set pause to true/false 189 | * @returns {void} 190 | */ 191 | setPause(pause) { 192 | this.node.send({ 193 | op: 'pause', 194 | guildId: this.guildId, 195 | pause: pause, 196 | }); 197 | 198 | this.paused = pause; 199 | this.playing = !pause; 200 | } 201 | 202 | /** 203 | * Used to pause the player 204 | */ 205 | pause() { 206 | if (this.playing) { 207 | this.setPause(true); 208 | } 209 | } 210 | 211 | /** 212 | * Used to resume the player 213 | */ 214 | resume() { 215 | if (!this.playing && this.paused) { 216 | this.setPause(false) 217 | } 218 | } 219 | 220 | /** 221 | * Used for seeking to a track position 222 | * @param {number} position The position to seek to 223 | * @returns {void} 224 | */ 225 | seek(position) { 226 | this.node.send({ 227 | op: 'seek', 228 | guildId: this.guildId, 229 | position: position, 230 | }); 231 | } 232 | 233 | /** 234 | * Set the volume of the player 235 | * @param {number} volume The volume level to set 236 | * @returns {void} 237 | */ 238 | setVolume(volume) { 239 | this.node.send({ 240 | op: 'volume', 241 | guildId: this.guildId, 242 | volume: volume, 243 | }); 244 | } 245 | 246 | /** 247 | * Called on track end 248 | * @param {Object} message The end reason 249 | * @private 250 | */ 251 | onTrackEnd(message) { 252 | if (message.reason !== 'REPLACED') { 253 | this.playing = false; 254 | this.lastTrack = this.track; 255 | this.track = null; 256 | } 257 | this.emit('end', message); 258 | } 259 | 260 | /** 261 | * Called on track exception 262 | * @param {Object} message The exception encountered 263 | * @private 264 | */ 265 | onTrackException(message) { 266 | this.emit('error', message); 267 | } 268 | 269 | /** 270 | * Called on track stuck 271 | * @param {Object} message The message if exists 272 | * @private 273 | */ 274 | onTrackStuck(message) { 275 | this.stop(); 276 | process.nextTick(() => this.emit('end', message)); 277 | } 278 | 279 | /** 280 | * Switch voice channel 281 | * @param {string} channelId Called when switching channels 282 | * @param {boolean} [reactive] Used if you want the bot to switch channels 283 | * @returns {void} 284 | */ 285 | switchChannel(channelId, reactive) { 286 | if(this.channelId === channelId) { 287 | return; 288 | } 289 | 290 | this.channelId = channelId; 291 | if (reactive === true) { 292 | this.updateVoiceState(channelId); 293 | } 294 | } 295 | 296 | getTimestamp() { 297 | return Date.now() - this.timestamp; 298 | } 299 | 300 | /** 301 | * Update the bot's voice state 302 | * @param {boolean} selfMute Whether the bot muted itself or not (audio sending is unaffected) 303 | * @param {boolean} selfDeaf Whether the bot deafened itself or not (audio receiving is unaffected) 304 | * @private 305 | */ 306 | updateVoiceState(channelId, selfMute, selfDeaf) { 307 | if (this.shard.sendWS) { 308 | this.shard.sendWS(Constants.GatewayOPCodes.VOICE_STATE_UPDATE, { 309 | guild_id: this.id === 'call' ? null : this.id, 310 | channel_id: channelId || null, 311 | self_mute: !!selfMute, 312 | self_deaf: !!selfDeaf, 313 | }); 314 | } 315 | } 316 | } 317 | 318 | module.exports = Player; 319 | -------------------------------------------------------------------------------- /src/PlayerManager.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Julian & NoobLance on 25.05.2017. 3 | * DISCLAIMER: We reuse some eris code 4 | */ 5 | 6 | const { Collection } = require('eris'); 7 | const Lavalink = require('./Lavalink'); 8 | const Player = require('./Player'); 9 | 10 | /** 11 | * Player Manager 12 | * @extends Map 13 | * @prop {Player} baseObject The player class used to create new players 14 | * @prop {object} client The eris client 15 | * @prop {object} defaultRegions The default region config 16 | * @prop {object} regions The region config being used 17 | */ 18 | class PlayerManager extends Collection { 19 | /** 20 | * PlayerManager constructor 21 | * @param {Client} client Eris client 22 | * @param {Object[]} nodes The Lavalink nodes to connect to 23 | * @param {Object} [options] Setup options 24 | * @param {string} [options.defaultRegion] The default region 25 | * @param {number} [options.failoverRate=250] Failover rate in ms 26 | * @param {number} [options.failoverLimit=1] Number of connections to failover per rate limit 27 | * @param {Object} [options.player] Optional Player class to replace the default Player 28 | * @param {number} [options.reconnectThreshold=2000] The amount of time to skip ahead in a song when reconnecting in ms 29 | * @param {Object} [options.regions] Region mapping object 30 | */ 31 | constructor(client, nodes, options) { 32 | super(options.player || Player); 33 | 34 | this.client = client; 35 | this.nodes = new Collection(); 36 | this.pendingGuilds = {}; 37 | this.options = options || {}; 38 | this.failoverQueue = []; 39 | this.failoverRate = options.failoverRate || 250; 40 | this.failoverLimit = options.failoverLimit || 1; 41 | 42 | this.defaultRegions = { 43 | asia: ['hongkong', 'singapore', 'sydney'], 44 | eu: ['eu', 'amsterdam', 'frankfurt', 'russia'], 45 | us: ['us', 'brazil'], 46 | }; 47 | 48 | this.regions = options.regions || this.defaultRegions; 49 | 50 | for (let node of nodes) { 51 | this.createNode(Object.assign({}, node, options)); 52 | } 53 | 54 | this.shardReadyListener = this.shardReady.bind(this); 55 | this.client.on('shardReady', this.shardReadyListener); 56 | } 57 | 58 | /** 59 | * Create a Lavalink node 60 | * @param {Object} options Lavalink node options 61 | * @param {string} options.host The hostname to connect to 62 | * @param {string} options.port The port to connect with 63 | * @param {string} options.region The region of the node 64 | * @param {number} options.numShards The number of shards the bot is running 65 | * @param {string} options.userId The user id of the bot 66 | * @param {string} options.password The password for the Lavalink node 67 | * @returns {void} 68 | */ 69 | createNode(options) { 70 | let node = new Lavalink({ 71 | host: options.host, 72 | port: options.port, 73 | region: options.region, 74 | numShards: options.numShards, 75 | userId: options.userId, 76 | password: options.password, 77 | }); 78 | 79 | node.on('error', this.onError.bind(this, node)); 80 | node.on('disconnect', this.onDisconnect.bind(this, node)); 81 | node.on('message', this.onMessage.bind(this, node)); 82 | 83 | this.nodes.set(options.host, node); 84 | } 85 | 86 | /** 87 | * Remove a Lavalink node 88 | * @param {string} host The hostname of the node 89 | * @returns {void} 90 | */ 91 | removeNode(host) { 92 | let node = this.nodes.get(host); 93 | if (!host) return; 94 | node.destroy(); 95 | this.nodes.delete(host); 96 | this.onDisconnect(node); 97 | } 98 | 99 | /** 100 | * Check the failover queue 101 | * @private 102 | */ 103 | checkFailoverQueue() { 104 | if (this.failoverQueue.length > 0) { 105 | let fns = this.failoverQueue.splice(0, this.failoverLimit); 106 | for (let fn of fns) { 107 | this.processQueue(fn); 108 | } 109 | } 110 | } 111 | 112 | /** 113 | * Queue a failover 114 | * @param {Function} fn The failover function to queue 115 | * @private 116 | */ 117 | queueFailover(fn) { 118 | if (this.failoverQueue.length > 0) { 119 | this.failoverQueue.push(fn); 120 | } else { 121 | return this.processQueue(fn); 122 | } 123 | } 124 | 125 | /** 126 | * Process the failover queue 127 | * @param {Function} fn The failover function to call 128 | * @private 129 | */ 130 | processQueue(fn) { 131 | fn(); 132 | setTimeout(() => this.checkFailoverQueue(), this.failoverRate); 133 | } 134 | 135 | /** 136 | * Called when an error is received from a Lavalink node 137 | * @param {Lavalink} node The Lavalink node 138 | * @param {string|Error} err The error received 139 | * @private 140 | */ 141 | onError(node, err) { 142 | this.client.emit('error', err); 143 | } 144 | 145 | /** 146 | * Called when a node disconnects 147 | * @param {Lavalink} node The Lavalink node 148 | * @param {*} msg The disconnect message if sent 149 | * @private 150 | */ 151 | onDisconnect(node, msg) { 152 | let players = this.filter(player => player.node.host === node.host); 153 | for (let player of players) { 154 | this.queueFailover(this.switchNode.bind(this, player, true)); 155 | } 156 | } 157 | 158 | /** 159 | * Called when a shard readies 160 | * @param {number} id Shard ID 161 | * @private 162 | */ 163 | shardReady(id) { 164 | let players = this.filter(player => player.shard && player.shard.id === id); 165 | for (let player of players) { 166 | this.queueFailover(this.switchNode.bind(this, player)); 167 | } 168 | } 169 | 170 | /** 171 | * Switch the voice node of a player 172 | * @param {Player} player The Player instance 173 | * @param {boolean} leave Whether to leave the channel or not on our side 174 | * @returns {void} 175 | */ 176 | switchNode(player, leave) { 177 | let { guildId, channelId, track, paused } = player, 178 | position = (player.state.position || 0) + (this.options.reconnectThreshold || 2000); 179 | 180 | let listeners = player.listeners('end'), 181 | endListeners = []; 182 | 183 | if (listeners && listeners.length) { 184 | for (let listener of listeners) { 185 | endListeners.push(listener); 186 | player.removeListener('end', listener); 187 | } 188 | } 189 | 190 | player.once('end', () => { 191 | for (let listener of endListeners) { 192 | player.on('end', listener); 193 | } 194 | }); 195 | 196 | this.delete(guildId); 197 | 198 | player.playing = false; 199 | 200 | if (leave) { 201 | player.updateVoiceState(null); 202 | } 203 | 204 | process.nextTick(() => { 205 | this.join(guildId, channelId, null, player).then(player => { 206 | if (paused) { 207 | player.pause(); 208 | } 209 | player.play(track, { startTime: position }); 210 | player.emit('reconnect'); 211 | this.set(guildId, player); 212 | }) 213 | .catch(err => { 214 | player.disconnect(); 215 | }); 216 | }); 217 | } 218 | 219 | /** 220 | * Called when a message is received from the voice node 221 | * @param {Lavalink} node The Lavalink node 222 | * @param {*} message The message received 223 | * @private 224 | */ 225 | onMessage(node, message) { 226 | if (!message.op) return; 227 | 228 | switch (message.op) { 229 | case 'playerUpdate': { 230 | let player = this.get(message.guildId); 231 | if (!player) return; 232 | 233 | return player.stateUpdate(message.state); 234 | } 235 | case 'event': { 236 | let player = this.get(message.guildId); 237 | if (!player) return; 238 | 239 | switch (message.type) { 240 | case 'TrackEndEvent': 241 | return player.onTrackEnd(message); 242 | case 'TrackExceptionEvent': 243 | return player.onTrackException(message); 244 | case 'TrackStuckEvent': 245 | return player.onTrackStuck(message); 246 | default: 247 | return player.emit('warn', `Unexpected event type: ${message.type}`); 248 | } 249 | } 250 | } 251 | } 252 | 253 | /** 254 | * Join a voice channel 255 | * @param {string} guildId The guild ID 256 | * @param {string} channelId The channel ID 257 | * @param {Object} options Join options 258 | * @param {Player} [player] Optionally pass an existing player 259 | * @returns {Promise} 260 | */ 261 | async join(guildId, channelId, options, player) { 262 | options = options || {}; 263 | 264 | player = player || this.get(guildId); 265 | if (player && player.channelId !== channelId) { 266 | player.switchChannel(channelId); 267 | return Promise.resolve(player); 268 | } 269 | 270 | let region, node; 271 | 272 | if(options.node) { 273 | node = this.nodes.get(options.node); 274 | region = node.region; 275 | } else { 276 | region = this.getRegionFromData(options.region || 'us'); 277 | node = await this.findIdealNode(region); 278 | } 279 | 280 | if (!node) { 281 | return Promise.reject('No available voice nodes.'); 282 | } 283 | 284 | return new Promise((res, rej) => { 285 | this.pendingGuilds[guildId] = { 286 | channelId: channelId, 287 | options: options || {}, 288 | player: player || null, 289 | node: node, 290 | res: res, 291 | rej: rej, 292 | timeout: setTimeout(() => { 293 | delete this.pendingGuilds[guildId]; 294 | rej(new Error('Voice connection timeout')); 295 | }, 10000), 296 | }; 297 | }); 298 | } 299 | 300 | /** 301 | * Leave a voice channel 302 | * @param {string} guildId The guild ID 303 | * @returns {void} 304 | */ 305 | async leave(guildId) { 306 | let player = this.get(guildId); 307 | if (!player) { 308 | return; 309 | } 310 | player._disconnect(); 311 | this.delete(guildId); 312 | } 313 | 314 | /** 315 | * Find the ideal voice node based on load and region 316 | * @param {string} region Guild region 317 | * @returns {Lavalink} node Node with the lowest load for a region 318 | */ 319 | async findIdealNode(region) { 320 | let nodes = [...this.nodes.values()].filter(node => !node.draining && node.ws && node.connected); 321 | 322 | if (region) { 323 | let regionalNodes = nodes.filter(node => node.region === region); 324 | if (regionalNodes && regionalNodes.length) { 325 | nodes = regionalNodes; 326 | } 327 | } 328 | 329 | nodes = nodes.sort((a, b) => { 330 | let aload = a.stats.cpu ? (a.stats.cpu.systemLoad / a.stats.cpu.cores) * 100 : 0, 331 | bload = b.stats.cpu ? (b.stats.cpu.systemLoad / b.stats.cpu.cores) * 100 : 0; 332 | return aload - bload; 333 | }); 334 | return nodes[0]; 335 | } 336 | 337 | /** 338 | * Called by eris when a voice server update is received 339 | * @param {*} data The voice server update from eris 340 | * @private 341 | */ 342 | async voiceServerUpdate(data) { 343 | if (this.pendingGuilds[data.guild_id] && this.pendingGuilds[data.guild_id].timeout) { 344 | clearTimeout(this.pendingGuilds[data.guild_id].timeout); 345 | this.pendingGuilds[data.guild_id].timeout = null; 346 | } 347 | 348 | let player = this.get(data.guild_id); 349 | if (!player) { 350 | if (!this.pendingGuilds[data.guild_id]) { 351 | return; 352 | } 353 | 354 | player = this.pendingGuilds[data.guild_id].player; 355 | 356 | if (player) { 357 | player.sessionId = data.sessionId; 358 | player.hostname = this.pendingGuilds[data.guild_id].hostname; 359 | player.node = this.pendingGuilds[data.guild_id].node; 360 | player.event = data; 361 | this.set(data.guild_id, player); 362 | } 363 | 364 | player = player || this.add(new this.baseObject(data.guild_id, { 365 | shard: data.shard, 366 | guildId: data.guild_id, 367 | sessionId: data.session_id, 368 | channelId: this.pendingGuilds[data.guild_id].channelId, 369 | hostname: this.pendingGuilds[data.guild_id].hostname, 370 | node: this.pendingGuilds[data.guild_id].node, 371 | options: this.pendingGuilds[data.guild_id].options, 372 | event: data, 373 | manager: this, 374 | })); 375 | } 376 | 377 | const channelId = player.channelId || this.pendingGuilds[data.guild_id].channelId; 378 | if (!channelId) { 379 | if (this.pendingGuilds[data.guild_id]) { 380 | delete this.pendingGuilds[data.guild_id]; 381 | return this.pendingGuilds[data.guild_id].rej(new Error('Invalid Channel ID')); 382 | } 383 | return; 384 | } 385 | 386 | player.connect({ 387 | sessionId: data.session_id, 388 | guildId: data.guild_id, 389 | channelId: channelId, 390 | event: { 391 | endpoint: data.endpoint, 392 | guild_id: data.guild_id, 393 | token: data.token, 394 | }, 395 | }); 396 | 397 | let disconnectHandler = () => { 398 | player = this.get(data.guild_id); 399 | if (!this.pendingGuilds[data.guild_id]) { 400 | if (player) { 401 | player.removeListener('ready', readyHandler); 402 | } 403 | return; 404 | } 405 | player.removeListener('ready', readyHandler); 406 | this.pendingGuilds[data.guild_id].rej(new Error('Disconnected')); 407 | delete this.pendingGuilds[data.guild_id]; 408 | }; 409 | 410 | let readyHandler = () => { 411 | player = this.get(data.guild_id); 412 | if (!this.pendingGuilds[data.guild_id]) { 413 | if (player) { 414 | player.removeListener('disconnect', disconnectHandler); 415 | } 416 | return; 417 | } 418 | player.removeListener('disconnect', disconnectHandler); 419 | this.pendingGuilds[data.guild_id].res(player); 420 | delete this.pendingGuilds[data.guild_id]; 421 | }; 422 | 423 | player.once('ready', readyHandler).once('disconnect', disconnectHandler); 424 | } 425 | 426 | /** 427 | * Get ideal region from data 428 | * @param {string} endpoint Endpoint or region 429 | * @private 430 | */ 431 | getRegionFromData(endpoint) { 432 | if (!endpoint) return this.options.defaultRegion || 'us'; 433 | 434 | endpoint = endpoint.replace('vip-', ''); 435 | 436 | for (let key in this.regions) { 437 | let nodes = this.nodes.filter(n => n.region === key); 438 | if (!nodes || !nodes.length) continue; 439 | if (!nodes.find(n => n.connected && !n.draining)) continue; 440 | for (let region of this.regions[key]) { 441 | if (endpoint.startsWith(region)) { 442 | return key; 443 | } 444 | } 445 | } 446 | 447 | return this.options.defaultRegion || 'us'; 448 | } 449 | } 450 | 451 | module.exports = PlayerManager; 452 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Player = require('./Player'); 4 | const PlayerManager = require('./PlayerManager'); 5 | const Lavalink = require('./Lavalink'); 6 | 7 | module.exports = { 8 | Player, 9 | PlayerManager, 10 | Lavalink, 11 | }; 12 | --------------------------------------------------------------------------------