Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

Commit

Permalink
Revert "The start of a new module! (#50)" (#51)
Browse files Browse the repository at this point in the history
This reverts commit 32fabc7.
  • Loading branch information
MrJacz committed Mar 18, 2019
1 parent 32fabc7 commit 4fde3ce
Show file tree
Hide file tree
Showing 19 changed files with 418 additions and 278 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Expand Up @@ -58,5 +58,3 @@ jspm_packages/
package-lock.json
example/config.json

dist/

3 changes: 1 addition & 2 deletions .npmignore
Expand Up @@ -7,5 +7,4 @@ testing
.travis.yml
docs.js
tslint.json
example
azure-pipelines.yml
example
14 changes: 14 additions & 0 deletions .travis.yml
@@ -0,0 +1,14 @@
language: node_js
node_js:
- "10"
- "11"
install: npm install
jobs:
include:
- stage: test
script: npm test
- stage: deploy
script: bash ./scripts/travis-deploy.sh
cache:
directories:
- node_modules
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018-2019 Jacz
Copyright (c) 2018 Jacz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
64 changes: 22 additions & 42 deletions azure-pipelines.yml
@@ -1,52 +1,32 @@
# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- master

pool:
vmImage: 'ubuntu-16.04'
strategy:
matrix:
node_11_x:
node_version: 11.x
node_10_x:
node_version: 10.x
node_11_x:
node_version: 11.x

steps:
- task: NodeTool@0
inputs:
versionSpec: $(node_version)
displayName: 'Install Node.js'
- script: yarn
displayName: 'Install Dependencies'
- script: yarn run test:lint
displayName: 'Run test lint'
condition: succeededOrFailed()
- bash: |
set -e
if [ "$TRAVIS_BRANCH" != "master" -o -n "$TRAVIS_TAG" -o "$TRAVIS_PULL_REQUEST" != "false" ]; then
echo -e "Not building for a non master branch push - building without deploying."
npm run docs
exit 0
fi
echo -e "Building for a master branch push - building and deploying."
REPO=$(git config remote.origin.url)
SHA=$(git rev-parse --verify HEAD)
TARGET_BRANCH="gh-pages"
git clone $REPO out -b $TARGET_BRANCH
yarn run docs
rsync -vau docs/ out/
cd out
git add --all .
git config user.name "Azure Pipelines"
git config user.email "${COMMIT_EMAIL}"
git commit -m "Docs build: ${SHA}" || true
git push "https://${GH_TOKEN}@github.com/MrJacz/discord.js-lavalink.git" $TARGET_BRANCH
env:
COMMIT_EMAIL: $(commitEmail)
GH_TOKEN: $(githubToken)
displayName: 'Build docs'
- task: NodeTool@0
inputs:
versionSpec: $(node_version)
displayName: 'Install Node.js'

- script: |
npm install
displayName: 'npm install'

- script: |
npm run test
displayName: 'run tests'


2 changes: 1 addition & 1 deletion docs.js
Expand Up @@ -23,7 +23,7 @@ Docma.create()
},
src: [
{ readme: "./README.md" },
{ lavalink: "./dist/*/**/*.js" }
{ lavalink: "./src/*/**/*.js" }
],
dest: "./docs",
jsdoc: { plugins: ["jsdoc-dynamic"] },
Expand Down
2 changes: 1 addition & 1 deletion example/app.js
Expand Up @@ -2,7 +2,7 @@ const config = require("./config.json");
const fetch = require("node-fetch");
const { URLSearchParams } = require("url");
const { Client } = require("discord.js");
const { PlayerManager } = require("../dist/index.js");
const { PlayerManager } = require("../src/index");

class MusicClient extends Client {

Expand Down
21 changes: 9 additions & 12 deletions package.json
@@ -1,14 +1,12 @@
{
"name": "discord.js-lavalink",
"version": "3.0.0",
"version": "2.2.2",
"description": "A discord.js lavalink client",
"main": "dist/src/index",
"main": "src/index.js",
"types": "typings/index.d.ts",
"scripts": {
"prepublishOnly": "yarn compile",
"lint": "tslint --fix 'src/**/*.ts'",
"test:lint": "tslint 'src/**/*.ts'",
"compile": "tsc -p .",
"watch": "tsc -p . -w",
"test": "eslint src && tslint 'typings/*.ts'",
"lint": "eslint --fix src test && tslint --fix 'typings/*.ts'",
"docs": "node docs.js"
},
"repository": {
Expand All @@ -21,7 +19,6 @@
"discord",
"api",
"lava",
"link",
"lavalink",
"lavalink.js",
"discord.js"
Expand All @@ -33,19 +30,19 @@
},
"homepage": "https://github.com/MrJacz/discord.js-lavalink#readme",
"devDependencies": {
"@types/node": "^11.11.3",
"@types/node": "^11.9.5",
"@types/ws": "^6.0.1",
"discord.js": "discordjs/discord.js",
"docma": "^3.2.2",
"eslint": "^5.15.2",
"eslint": "^5.14.1",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"jsdoc-dynamic": "^1.0.4",
"tslint": "^5.14.0",
"tslint": "^5.12.1",
"typescript": "^3.3.3333"
},
"dependencies": {
"ws": "^6.2.0"
"ws": "^6.1.4"
},
"peerDependencies": {
"discord.js": "^11.4.2"
Expand Down
29 changes: 29 additions & 0 deletions scripts/travis-deploy.sh
@@ -0,0 +1,29 @@
#!/bin/bash
# Based on https://github.com/hydrabolt/discord.js-site/blob/master/deploy/deploy.sh

set -e

if [ "$TRAVIS_BRANCH" != "master" -o -n "$TRAVIS_TAG" -o "$TRAVIS_PULL_REQUEST" != "false" ]; then
echo -e "Not building for a non master branch push - building without deploying."
npm run docs
exit 0
fi

echo -e "Building for a master branch push - building and deploying."

REPO=$(git config remote.origin.url)
SHA=$(git rev-parse --verify HEAD)

TARGET_BRANCH="gh-pages"
git clone $REPO dist -b $TARGET_BRANCH

npm run docs

rsync -vau docs/ dist/

cd dist
git add --all .
git config user.name "Travis CI"
git config user.email "${COMMIT_EMAIL}"
git commit -m "Docs build: ${SHA}" || true
git push "https://${GH_TOKEN}@github.com/MrJacz/discord.js-lavalink.git" $TARGET_BRANCH
16 changes: 16 additions & 0 deletions src/index.js
@@ -0,0 +1,16 @@
module.exports = {
Node: require("./lib/LavalinkNode"),
Player: require("./lib/Player"),
PlayerManager: require("./lib/PlayerManager"),
version: require("../package.json").version
};

/**
* @external Client
* @see {@link https://discord.js.org/#/docs/main/master/class/Client}
*/

/**
* @external Collection
* @see {@link https://discord.js.org/#/docs/main/master/class/Collection}
*/
5 changes: 0 additions & 5 deletions src/index.ts

This file was deleted.

77 changes: 42 additions & 35 deletions src/lib/LavalinkNode.ts → src/lib/LavalinkNode.js
@@ -1,23 +1,30 @@
const WebSocket = require("ws");
const { EventEmitter } = require("events");

import * as WebSocket from "ws";
import { EventEmitter } from "events";
import { PlayerManager } from "./PlayerManager";
import { NodeStats, NodeOptions } from "./Types";

export class LavalinkNode extends EventEmitter {
public manager: PlayerManager;
public host: string;
public port?: number | string;
public address: string;
public region?: string;
public password?: string;
public ready: boolean;
public ws?: WebSocket;
public reconnect?: NodeJS.Timer;
public reconnectInterval?: number;
public stats: NodeStats;

public constructor(manager: PlayerManager, options: NodeOptions) {
/**
* Lavalink Websocket
* @extends {EventEmitter}
*/
class LavalinkNode extends EventEmitter {

/**
* LavalinkNode Options
* @typedef {Object} LavalinkNodeOptions
* @memberof LavalinkNode
* @property {string} host Lavalink host
* @property {number|string} [port=2333] Lavalink port
* @property {string} [address] Lavalink address
* @property {string} [region] Lavalink region
* @property {string} [password="youshallnotpass"] Lavalink password
* @property {number} [reconnectInterval=5000] Reconnectinterval
*/

/**
* LavaLink options
* @param {PlayerManager} manager The PlayerManager that created the Node
* @param {LavalinkNodeOptions} options LavaLink options
*/
constructor(manager, options = {}) {
super();

/**
Expand Down Expand Up @@ -79,22 +86,19 @@ export class LavalinkNode extends EventEmitter {
* Player stats
* @type {Object}
*/
this.stats = {
players: 0,
playingPlayers: 0
};
this.stats = {};

this._connect();
this.connect();
}

/**
* Connects to the WebSocket server
*/
private _connect() {
connect() {
this.ws = new WebSocket(this.address, {
headers: {
"User-Id": this.manager.user,
"Num-Shards": String(this.manager.shards),
"Num-Shards": this.manager.shards,
Authorization: this.password
}
});
Expand All @@ -109,7 +113,7 @@ export class LavalinkNode extends EventEmitter {
* Function for the onOpen WS event
* @private
*/
private _ready() {
_ready() {
this.ready = true;
/**
* Emmited when the node gets ready
Expand All @@ -123,7 +127,7 @@ export class LavalinkNode extends EventEmitter {
* @param {Object} data Object to send
* @returns {boolean}
*/
public send(data: object): boolean {
send(data) {
if (!this.ws) return false;
let payload;
try {
Expand All @@ -140,7 +144,7 @@ export class LavalinkNode extends EventEmitter {
* Destroys the WebSocket
* @returns {boolean}
*/
public destroy(): boolean {
destroy() {
if (!this.ws) return false;
this.ws.close(1000, "destroy");
this.ws = null;
Expand All @@ -151,15 +155,15 @@ export class LavalinkNode extends EventEmitter {
* Reconnects the websocket
* @private
*/
private _reconnect() {
_reconnect() {
this.reconnect = setTimeout(() => {
this.ws.removeAllListeners();
/**
* Emmited when the node is attempting a reconnect
* @event LavalinkNode#reconnecting
*/
this.emit("reconnecting");
this._connect();
this.connect();
}, this.reconnectInterval);
}

Expand All @@ -170,8 +174,8 @@ export class LavalinkNode extends EventEmitter {
* @returns {void}
* @private
*/
private _close(code, reason) {
this.ready = false;
_close(code, reason) {
this.connected = false;
if (code !== 1000 || reason !== "destroy") return this._reconnect();
this.ws = null;
/**
Expand All @@ -188,7 +192,7 @@ export class LavalinkNode extends EventEmitter {
* @returns {void}
* @private
*/
private _message(msg) {
_message(msg) {
try {
const data = JSON.parse(msg);
if (data.op === "stats") this.stats = data;
Expand All @@ -208,7 +212,7 @@ export class LavalinkNode extends EventEmitter {
* @param {Error} error error from WS
* @private
*/
private _error(error) {
_error(error) {
/**
* Emitted whenever the Node's WebSocket encounters a connection error.
* @event LavalinkNode#error
Expand All @@ -217,4 +221,7 @@ export class LavalinkNode extends EventEmitter {
this.emit("error", error);
}


}

module.exports = LavalinkNode;

0 comments on commit 4fde3ce

Please sign in to comment.