diff --git a/.gitignore b/.gitignore index dd7eaf3..5dc4fe7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules testing.js test/npm-debug.log +.nyc_output/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index fdff065..c8daf27 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,4 +13,4 @@ deploy: secure: gFR72hll7LrOld2tWIE7h2dZKsy01bN2Bw3a1VxBg56TGot3926DaL6FwZVLGbAb8/1YtINoW1GDdSBIR1Bf51qPVDfSMfWoW1JDkzr//WXq5nqYanyYXxwTlMk1Qdap8UyWF9Xv6ESLjg5pg+jS2Js/A9P7PNZk4/MXEFrg/t6vVJsb6VDXeHE0VgG9JZaOpgb/taLX7DCCOR/ToNTFtCui29PFS2ueVtIhs740jeo70eIMqNx5idkPuzXcIJE8lB+z22dfjKcKJlGeIyboIKeL3AGTMO/uBwjVEi7NUmWup3MDq9hE4+jvv3GPT43vvZwxA3ynhNvkMde43ymMwvPLudqEgDgJf9T1ZOEb0uBk5XfvnIqulZjEUZ8TAYRqS+3w3kBx2+ee6JT4/o07L8IFaSwqt1DYdFDKz2loFHW0oLJUCe+/T23QIVrHS1vwLu9FxIjCxBc3mGmmzwgNYpyeNaQXssyVDkTXP0zW2wrkqENeVx6PsS2G6RIA7/c5s04qhteeQ8FNVCtEltSa9zXCpEPtpl4GTSFyaU4YuFmEn4ghXjhi0Yt3005lL9BwPIp1vDnEz1ZxSk73/StYY3lPqmh9V66ESo7lNWUu0eN2lWkgVHjfQaJOt1oF1b6+GdDnvaFCsI/vVtNUFEA5BgCPaZAmqai2yZi4TFhe4V8= on: tags: true - repo: musiqpad/mqp-node-api + repo: musiqpad/mqp-node-api \ No newline at end of file diff --git a/README.md b/README.md index 6303f15..1a78d64 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ API for creating musiqpad bots -[![Version npm](https://img.shields.io/npm/v/mqp-api.svg?style=flat-square)](https://www.npmjs.com/package/mqp-api)[![npm Downloads](https://img.shields.io/npm/dm/mqp-api.svg?style=flat-square)](https://www.npmjs.com/package/mqp-api) +[![Version npm](https://img.shields.io/npm/v/mqp-api.svg?style=flat-square)](https://www.npmjs.com/package/mqp-api)[![npm Downloads](https://img.shields.io/npm/dm/mqp-api.svg?style=flat-square)](https://www.npmjs.com/package/mqp-api) [![Build Status](https://travis-ci.org/musiqpad/mqp-node-api.svg?branch=master)](https://travis-ci.org/musiqpad/mqp-server) [![NPM](https://nodei.co/npm/mqp-api.png)](https://npmjs.org/package/mqp-api) @@ -199,7 +199,7 @@ bot.sendPrivateMessage(7, "Hey, what's up?") -------------------------------------------------------------------------------- -joinQueue(): //leaveQueue, lockQueue, cycle and skip are the same. +joinQueue(): //leaveQueue, lockQueue, unlockQueue, toggleLockQueue, cycle and skip are the same. ```js bot.joinQueue() diff --git a/index.js b/index.js deleted file mode 100644 index 3875e93..0000000 --- a/index.js +++ /dev/null @@ -1,574 +0,0 @@ -const createHash = require('sha.js'); -const Promise = require('bluebird'); -const WebSocketClient = require('websocket').client; -const EventEmitter = require('events').EventEmitter; -const events = new EventEmitter(); -events.emit = Promise.promisify(events.emit); -var API = require('./src/api'); - -var app = function (args) { - var _this = this; - this.settings = { - useSSL: args.useSSL, - socketDomain: args.socketDomain, - socketPort: args.socketPort, - }; - - this.WsClient = new WebSocketClient(); - this.connect = function () { - return new Promise(function (resolve, reject) { - _this.WsClient.connect((_this.settings.useSSL ? 'wss' : 'ws') + '://' + - _this.settings.socketDomain + ':' + _this.settings.socketPort, 'echo-protocol'); - - _this.WsClient.on('connectFailed', function (error) { - reject('Connection Error: ' + error.toString()); - }); - - _this.WsClient.on('connect', function (connection) { - _this.connection = connection; - _this.sendJSON({ - type: 'joinRoom', - data: {}, - }); - _this.sendJSON({ - type: 'getUsers', - }); - events.on('getUsersReceived', function (data) { - _this.users = data.users; - _this.guests = data.users; - }); - - events.on('joinRoomReceived', function (data) { - _this.queue = data.queue.users; - events.once('getUsersReceived', function () { - _this.currentdj = (data.queue.currentdj ? _this.getUser(data.queue.currentdj) : null); - }); - - _this.roles = data.roles; - _this.roleOrder = data.roleOrder; - _this.historylimit = data.historylimit; - _this.description = data.description; - _this.isQueueLocked = data.queue.lock; - }); - - connection.on('error', function (error) { - events.emit('error', error); - }); - - connection.on('close', function (e) { - events.emit('closed', e); - }); - - connection.on('message', function (message) { - events.emit('rawSocket', message); - _this.handleResponse(message); - }); - - resolve(); - }); - }); - }; -}; - -app.prototype.login = function (args) { - var _this = this; - var sha256 = createHash('sha256'); - var inEmail = args.email; - var inPass = args.password; - var token = args.token; - var obj = { - type: 'login', - data: { - email: inEmail, - pw: inPass ? sha256.update(inPass, 'utf8').digest('hex').toString() : null, - token: token, - }, - }; - this.sendJSON(obj); - return new Promise(function (resolve, reject) { - events.once('loginReceived', function (data) { - if (data.error) - reject('Login error:' + data.error); - _this.user = data.user; - _this.users[data.user.uid] = data.user; - resolve(); - }); - }); -}; - -app.prototype.sendJSON = function (inObj) { - this.connection.send(JSON.stringify(inObj)); -}; - -app.prototype.getRoomInfo = function () { - this.sendJSON({ - type: 'getRoomInfo', - }); - return new Promise(function (resolve, reject) { - events.once('getRoomInfoReceived', function (data) { - if (data.error) - reject('Room Info error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.sendPrivateMessage = function (uid, msg) { - this.sendJSON({ - type: 'privateMessage', - data: { - uid: uid, - message: msg, - }, - }); - return new Promise(function (resolve, reject) { - events.once('privateMessageReceived', function (data) { - if (data.error) - reject('Private Message error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.joinQueue = function () { - this.sendJSON({ - type: 'djQueueJoin', - }); - return new Promise(function (resolve, reject) { - events.once('djQueueJoinReceived', function (data) { - if (data.error) - reject('djQueueJoin error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.leaveQueue = function () { - this.sendJSON({ - type: 'djQueueLeave', - }); - return new Promise(function (resolve, reject) { - events.once('djQueueLeaveReceived', function (data) { - if (data.error) - reject('djQueueLeave error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.lockQueue = function () { - if(this.isQueueLocked == true) - return Promise.reject('Queue is already locked'); - this.sendJSON({ - type: 'djQueueLock', - }); - return new Promise(function (resolve, reject) { - events.once('djQueueLockReceived', function (data) { - if (data.error) - reject('djQueueLock error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.unlockQueue = function () { - if(this.isQueueLocked == false) - return Promise.reject('Queue is already unlocked'); - this.sendJSON({ - type: 'djQueueLock', - }); - return new Promise(function (resolve, reject) { - events.once('djQueueLockReceived', function (data) { - if (data.error) - reject('djQueueLock error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.toggleLockQueue = function () { - this.sendJSON({ - type: 'djQueueLock', - }); - return new Promise(function (resolve, reject) { - events.once('djQueueLockReceived', function (data) { - if (data.error) - reject('djQueueLock error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.cycle = function () { - this.sendJSON({ - type: 'djQueueCycle', - }); - return new Promise(function (resolve, reject) { - events.once('djQueueCycleReceived', function (data) { - if (data.error) - reject('djQueueCycle error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.skip = function () { - this.sendJSON({ - type: 'djQueueModSkip', - }); - return new Promise(function (resolve, reject) { - events.once('djQueueModSkipReceived', function (data) { - if (data.error) - reject('djQueueModSkip error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.deleteChat = function (cid, uid) { - this.sendJSON({ - type: 'deleteChat', - data: { - cid: cid, - mid: uid, - }, - }); - return new Promise(function (resolve, reject) { - events.once('deleteChatReceived', function (data) { - if (data.error) - reject('deleteChat error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.move = function (uid, position) { - this.sendJSON({ - type: 'djQueueModMove', - data: { - uid: uid, - position: position, - }, - }); - return new Promise(function (resolve, reject) { - events.once('djQueueModMoveReceived', function (data) { - if (data.error) - reject('djQueueModMove error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.swap = function (uid, uid) { - this.sendJSON({ - type: 'djQueueModSwap', - data: { - uid1: uid1, - uid2: uid2, - }, - }); - return new Promise(function (resolve, reject) { - events.once('djQueueModSwapReceived', function (data) { - if (data.error) - reject('djQueueModSwap error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.addDj = function (uid) { - this.sendJSON({ - type: 'djQueueModAdd', - data: { - uid: uid, - }, - }); - return new Promise(function (resolve, reject) { - events.once('djQueueModAddReceived', function (data) { - if (data.error) - reject('djQueueModAdd error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.removeDj = function (uid) { - this.sendJSON({ - type: 'djQueueModRemove', - data: { - uid: uid, - }, - }); - return new Promise(function (resolve, reject) { - events.once('djQueueModRemoveReceived', function (data) { - if (data.error) - reject('djQueueModRemove error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.broadcast = function (message) { - this.sendJSON({ - type: 'broadcastMessage', - data: { - message: message, - }, - }); - return new Promise(function (resolve, reject) { - events.once('broadcastMessageReceived', function (data) { - if (data.error) - reject('broadcastMessage error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.setLimit = function (limit) { - this.sendJSON({ - type: 'djQueueLimit', - data: { - limit: limit, - }, - }); - return new Promise(function (resolve, reject) { - events.once('djQueueLimitReceived', function (data) { - if (data.error) - reject('djQueueLimit error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.setRole = function (uid, role) { - this.sendJSON({ - type: 'setRole', - data: { - uid: uid, - role: role, - }, - }); - return new Promise(function (resolve, reject) { - events.once('setRoleReceived', function (data) { - if (data.error) - reject('setRole error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.ban = function (uid, duration, reason) { - this.sendJSON({ - type: 'banUser', - data: { - uid: uid, - duration: duration, - reason: reason, - }, - }); - return new Promise(function (resolve, reject) { - events.once('banUserReceived', function (data) { - if (data.error) - reject('banUser error: ' + data.error + (data.text ? ' - ' + data.text : '')); - resolve(data); - }); - }); -}; - -app.prototype.unban = function (uid) { - this.sendJSON({ - type: 'unbanUser', - data: { - uid: uid, - }, - }); - return new Promise(function (resolve, reject) { - events.once('unbanUserReceived', function (data) { - if (data.error) - reject('unbanUser error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.whois = function (uid, un) { - this.sendJSON({ - type: 'whois', - data: { - uid: uid, - un: un, - }, - }); - return new Promise(function (resolve, reject) { - events.once('whoisReceived', function (data) { - if (data.error) - reject('whois error: ' + data.error); - resolve(data); - }); - }); -}; - -app.prototype.getUser = function (uid) { - if (!uid) - return this.user; - return this.users[uid] ? this.users[uid] : null; -}; - -app.prototype.getUsers = function () { - return this.users; -}; - -app.prototype.on = function (type, value) { - return events.on(type, value); -}; - -app.prototype.once = function (type, value) { - return events.once(type, value); -}; - -app.prototype.sendMessage = function (message) { - this.sendJSON({ - type: 'chat', - data: { - message: message, - }, - }); -}; - -app.prototype.handleResponse = function (e) { - var _this = this; - if (e.utf8Data == 'h') - return; - var data = null; - - try { - data = JSON.parse(e.utf8Data); - } catch (e) { - return; - } - - var message = data.data; - switch (data.type) { - case API.DATA.EVENTS.CHAT: - events.emit(API.DATA.EVENTS.CHAT, { - time: message.time, - msg: message.message, - message: message.message, - cid: message.cid, - uid: message.uid, - user: _this.getUser(message.uid), - }); - break; - - case API.DATA.EVENTS.SYSTEM_MESSAGE: - events.emit(API.DATA.EVENTS.SYSTEM_MESSAGE, message); - break; - - case API.DATA.EVENTS.BROADCAST_MESSAGE: - events.emit(API.DATA.EVENTS.BROADCAST_MESSAGE, message); - break; - - case API.DATA.EVENTS.USER_JOINED: - events.emit(API.DATA.EVENTS.USER_JOINED, message); - _this.sendJSON({ - type: 'getUsers', - }); - break; - - case API.DATA.EVENTS.USER_LEFT: - events.emit(API.DATA.EVENTS.USER_LEFT, message); - _this.sendJSON({ - type: 'getUsers', - }); - break; - - case API.DATA.EVENTS.USER_JOINED_QUEUE: - _this.queue = data.data.queueList; - events.emit(API.DATA.EVENTS.USER_JOINED_QUEUE, message); - break; - - case API.DATA.EVENTS.USER_LEFT_QUEUE: - _this.queue = data.data.queueList; - events.emit(API.DATA.EVENTS.USER_LEFT_QUEUE, message); - break; - - case API.DATA.EVENTS.ADVANCE: - _this.currentdj = (data.data.next.uid ? _this.getUser(data.data.next.uid) : null); - _this.queue.shift(); - events.emit(API.DATA.EVENTS.ADVANCE, message); - break; - - case API.DATA.EVENTS.DJ_QUEUE_LOCK: - _this.isQueueLocked = message.state; - events.emit(API.DATA.EVENTS.DJ_QUEUE_LOCK, message); - break; - - case API.DATA.EVENTS.DJ_QUEUE_CYCLE: - events.emit(API.DATA.EVENTS.DJ_QUEUE_CYCLE, message); - break; - - case API.DATA.EVENTS.DJ_QUEUE_SKIP: - events.emit(API.DATA.EVENTS.DJ_QUEUE_SKIP, message); - break; - - case API.DATA.EVENTS.DJ_QUEUE_MOD_SKIP: - events.emit(API.DATA.EVENTS.DJ_QUEUE_MOD_SKIP, message); - break; - - case API.DATA.EVENTS.VOTE_UPDATE: - events.emit(API.DATA.EVENTS.VOTE_UPDATE, message); - break; - - case API.DATA.EVENTS.USER_UPDATE: - events.emit(API.DATA.EVENTS.USER_UPDATE, message); - break; - - case API.DATA.EVENTS.DELETE_CHAT: - events.emit(API.DATA.EVENTS.DELETE_CHAT, message); - break; - - case API.DATA.EVENTS.USER_BANNED: - events.emit(API.DATA.EVENTS.USER_BANNED, message); - break; - - case API.DATA.EVENTS.USER_UNBANNED: - events.emit(API.DATA.EVENTS.USER_UNBANNED, message); - break; - - case API.DATA.EVENTS.USER_ROLE_CHANGE: - events.emit(API.DATA.EVENTS.USER_ROLE_CHANGE, message); - break; - - case API.DATA.EVENTS.DJ_QUEUE_REMOVE: - events.emit(API.DATA.EVENTS.DJ_QUEUE_REMOVE, message); - break; - - case API.DATA.EVENTS.DJ_QUEUE_MOD_SWAP: - _this.queue = data.data.queueList; - events.emit(API.DATA.EVENTS.DJ_QUEUE_MOD_SWAP, message); - break; - - case API.DATA.EVENTS.DJ_QUEUE_MOD_MOVE: - _this.queue = data.data.queueList; - events.emit(API.DATA.EVENTS.DJ_QUEUE_MOD_MOVE, message); - break; - - case API.DATA.EVENTS.DJ_QUEUE_ADD: - events.emit(API.DATA.EVENTS.DJ_QUEUE_ADD, message); - break; - - case API.DATA.EVENTS.DJ_QUEUE_LIMIT: - events.emit(API.DATA.EVENTS.DJ_QUEUE_LIMIT, message); - break; - - case API.DATA.EVENTS.PRIVATE_MESSAGE: - events.emit(API.DATA.EVENTS.PRIVATE_MESSAGE, message); - break; - - case API.DATA.EVENTS.SERVER_RESPONSE: - events.emit(data.requestType + 'Received', data.data); - break; - }; -}; - -module.exports = app; diff --git a/src/app.js b/src/app.js new file mode 100644 index 0000000..ce649fd --- /dev/null +++ b/src/app.js @@ -0,0 +1,251 @@ +const createHash = require('sha.js'); +const WebSocket = require('ws'); + +const events = require('./events'); +const API = require('./api'); +const cmds = require('./cmds.js'); +var _this; + +var app = function (args) { + + var _this = this; + this.settings = { + useSSL: args.useSSL, + socketDomain: args.socketDomain, + socketPort: args.socketPort, + }; + + this.connect = function () { + var ws = new WebSocket((this.settings.useSSL ? 'wss' : 'ws') + '://' + + this.settings.socketDomain + ':' + this.settings.socketPort); + this.ws = ws; + + return new Promise(function (resolve, reject) { + ws.on('open', function () { + _this.sendJSON({ + type: 'joinRoom', + data: {}, + }); + _this.sendJSON({ + type: 'getUsers', + }); + resolve(); + }); + + ws.on('message', function (message) { + events.emit('rawSocket', message); + _this.handleResponse(message); + }); + + ws.on('error', function (error) { + events.emit('error', error); + }); + + ws.on('close', function (e) { + reject('Connection Closed: ' + e.toString()); + events.emit('closed', e); + }); + }); + }; + + events.on('getUsersReceived', function (data) { + _this.users = data.users; + _this.guests = data.users; + }); + + events.on('joinRoomReceived', function (data) { + _this.queue = data.queue.users; + events.once('getUsersReceived', function () { + _this.currentdj = (data.queue.currentdj ? _this.getUser(data.queue.currentdj) : null); + }); + + _this.roles = data.roles; + _this.roleOrder = data.roleOrder; + _this.historylimit = data.historylimit; + _this.description = data.description; + _this.isQueueLocked = data.queue.lock; + }); + + for (var method in cmds) { + app.prototype[method] = cmds[method].bind(this); + } +}; + +app.prototype.login = function (args) { + var _this = this; + var sha256 = createHash('sha256'); + var inEmail = args.email; + var inPass = args.password; + var token = args.token; + var obj = { + type: 'login', + data: { + email: inEmail, + pw: inPass ? sha256.update(inPass, 'utf8').digest('hex').toString() : null, + token: token, + }, + }; + + this.sendJSON(obj); + return new Promise(function (resolve, reject) { + events.once('loginReceived', function (data) { + if (data.error) + reject('Login error:' + data.error); + _this.user = data.user; + _this.users[data.user.uid] = data.user; + resolve(); + }); + }); +}; + +app.prototype.sendJSON = function (inObj) { + this.ws.send(JSON.stringify(inObj)); + return; +}; + +app.prototype.on = function (type, value) { + return events.on(type, value); +}; + +app.prototype.once = function (type, value) { + return events.once(type, value); +}; + +app.prototype.handleResponse = function (e) { + var _this = this; + + if (e == 'h') + return; + var data = null; + + try { + data = JSON.parse(e); + } catch (e) { + return; + } + + var message = data.data; + switch (data.type) { + case API.DATA.EVENTS.CHAT: + events.emit(API.DATA.EVENTS.CHAT, { + time: message.time, + msg: message.message, + message: message.message, + cid: message.cid, + uid: message.uid, + user: _this.getUser(message.uid), + }); + break; + + case API.DATA.EVENTS.SYSTEM_MESSAGE: + events.emit(API.DATA.EVENTS.SYSTEM_MESSAGE, message); + break; + + case API.DATA.EVENTS.BROADCAST_MESSAGE: + events.emit(API.DATA.EVENTS.BROADCAST_MESSAGE, message); + break; + + case API.DATA.EVENTS.USER_JOINED: + events.emit(API.DATA.EVENTS.USER_JOINED, message); + _this.sendJSON({ + type: 'getUsers', + }); + break; + + case API.DATA.EVENTS.USER_LEFT: + events.emit(API.DATA.EVENTS.USER_LEFT, message); + _this.sendJSON({ + type: 'getUsers', + }); + break; + + case API.DATA.EVENTS.USER_JOINED_QUEUE: + _this.queue = data.data.queueList; + events.emit(API.DATA.EVENTS.USER_JOINED_QUEUE, message); + break; + + case API.DATA.EVENTS.USER_LEFT_QUEUE: + _this.queue = data.data.queueList; + events.emit(API.DATA.EVENTS.USER_LEFT_QUEUE, message); + break; + + case API.DATA.EVENTS.ADVANCE: + _this.currentdj = (data.data.next.uid ? _this.getUser(data.data.next.uid) : null); + _this.queue.shift(); + events.emit(API.DATA.EVENTS.ADVANCE, message); + break; + + case API.DATA.EVENTS.DJ_QUEUE_LOCK: + _this.isQueueLocked = message.state; + events.emit(API.DATA.EVENTS.DJ_QUEUE_LOCK, message); + break; + + case API.DATA.EVENTS.DJ_QUEUE_CYCLE: + events.emit(API.DATA.EVENTS.DJ_QUEUE_CYCLE, message); + break; + + case API.DATA.EVENTS.DJ_QUEUE_SKIP: + events.emit(API.DATA.EVENTS.DJ_QUEUE_SKIP, message); + break; + + case API.DATA.EVENTS.DJ_QUEUE_MOD_SKIP: + events.emit(API.DATA.EVENTS.DJ_QUEUE_MOD_SKIP, message); + break; + + case API.DATA.EVENTS.VOTE_UPDATE: + events.emit(API.DATA.EVENTS.VOTE_UPDATE, message); + break; + + case API.DATA.EVENTS.USER_UPDATE: + events.emit(API.DATA.EVENTS.USER_UPDATE, message); + break; + + case API.DATA.EVENTS.DELETE_CHAT: + events.emit(API.DATA.EVENTS.DELETE_CHAT, message); + break; + + case API.DATA.EVENTS.USER_BANNED: + events.emit(API.DATA.EVENTS.USER_BANNED, message); + break; + + case API.DATA.EVENTS.USER_UNBANNED: + events.emit(API.DATA.EVENTS.USER_UNBANNED, message); + break; + + case API.DATA.EVENTS.USER_ROLE_CHANGE: + events.emit(API.DATA.EVENTS.USER_ROLE_CHANGE, message); + break; + + case API.DATA.EVENTS.DJ_QUEUE_REMOVE: + events.emit(API.DATA.EVENTS.DJ_QUEUE_REMOVE, message); + break; + + case API.DATA.EVENTS.DJ_QUEUE_MOD_SWAP: + _this.queue = data.data.queueList; + events.emit(API.DATA.EVENTS.DJ_QUEUE_MOD_SWAP, message); + break; + + case API.DATA.EVENTS.DJ_QUEUE_MOD_MOVE: + _this.queue = data.data.queueList; + events.emit(API.DATA.EVENTS.DJ_QUEUE_MOD_MOVE, message); + break; + + case API.DATA.EVENTS.DJ_QUEUE_ADD: + events.emit(API.DATA.EVENTS.DJ_QUEUE_ADD, message); + break; + + case API.DATA.EVENTS.DJ_QUEUE_LIMIT: + events.emit(API.DATA.EVENTS.DJ_QUEUE_LIMIT, message); + break; + + case API.DATA.EVENTS.PRIVATE_MESSAGE: + events.emit(API.DATA.EVENTS.PRIVATE_MESSAGE, message); + break; + + case API.DATA.EVENTS.SERVER_RESPONSE: + events.emit(data.requestType + 'Received', data.data); + break; + }; +}; + +module.exports = app; diff --git a/src/cmds.js b/src/cmds.js new file mode 100644 index 0000000..fd67a1b --- /dev/null +++ b/src/cmds.js @@ -0,0 +1,374 @@ +const Promise = require('bluebird'); + +module.exports = { + getRoomInfo: function () { + var _this = this; + this.sendJSON({ + type: 'getRoomInfo', + }); + return new Promise(function (resolve, reject) { + _this.once('getRoomInfoReceived', function (data) { + if (data.error) + reject('getRoomInfo error: ' + data.error); + resolve(data); + }); + }); + }, + + sendPrivateMessage: function (uid, msg) { + var _this = this; + this.sendJSON({ + type: 'privateMessage', + data: { + uid: uid, + message: msg, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('privateMessageReceived', function (data) { + if (data.error) + reject('privateMessage error: ' + + data.error); + resolve(data); + }); + }); + }, + + joinQueue: function () { + var _this = this; + this.sendJSON({ + type: 'djQueueJoin', + }); + return new Promise(function (resolve, reject) { + _this.once('djQueueJoinReceived', function (data) { + if (data.error) + reject('djQueueJoin error: ' + data + .error); + resolve(data); + }); + }); + }, + + leaveQueue: function () { + var _this = this; + this.sendJSON({ + type: 'djQueueLeave', + }); + return new Promise(function (resolve, reject) { + _this.once('djQueueLeaveReceived', function (data) { + if (data.error) + reject('djQueueLeave error: ' + + data.error); + resolve(data); + }); + }); + }, + + lockQueue: function () { + if (this.isQueueLocked == true) + return Promise.reject('Queue is already locked'); + var _this = this; + this.sendJSON({ + type: 'djQueueLock', + }); + return new Promise(function (resolve, reject) { + _this.once('djQueueLockReceived', function (data) { + if (data.error) + reject('djQueueLock error: ' + data + .error); + resolve(data); + }); + }); + }, + + unlockQueue: function () { + if (this.isQueueLocked == false) + return Promise.reject('Queue is already unlocked'); + var _this = this; + this.sendJSON({ + type: 'djQueueLock', + }); + return new Promise(function (resolve, reject) { + _this.once('djQueueLockReceived', function (data) { + if (data.error) + reject('djQueueLock error: ' + data + .error); + resolve(data); + }); + }); + }, + + toggleLockQueue: function () { + var _this = this; + this.sendJSON({ + type: 'djQueueLock', + }); + return new Promise(function (resolve, reject) { + _this.once('djQueueLockReceived', function (data) { + if (data.error) + reject('djQueueLock error: ' + data + .error); + resolve(data); + }); + }); + }, + + cycle: function () { + var _this = this; + this.sendJSON({ + type: 'djQueueCycle', + }); + return new Promise(function (resolve, reject) { + _this.once('djQueueCycleReceived', function (data) { + if (data.error) + reject('djQueueCycle error: ' + + data.error); + resolve(data); + }); + }); + }, + + skip: function () { + var _this = this; + this.sendJSON({ + type: 'djQueueModSkip', + }); + return new Promise(function (resolve, reject) { + _this.once('djQueueModSkipReceived', function (data) { + if (data.error) + reject('djQueueModSkip error: ' + + data.error); + resolve(data); + }); + }); + }, + + deleteChat: function (cid, uid) { + var _this = this; + this.sendJSON({ + type: 'deleteChat', + data: { + cid: cid, + mid: uid, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('deleteChatReceived', function (data) { + if (data.error) + reject('deleteChat error: ' + data.error); + resolve(data); + }); + }); + }, + + move: function (uid, position) { + var _this = this; + this.sendJSON({ + type: 'djQueueModMove', + data: { + uid: uid, + position: position, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('djQueueModMoveReceived', function (data) { + if (data.error) + reject('djQueueModMove error: ' + + data.error); + resolve(data); + }); + }); + }, + + swap: function (uid1, uid2) { + var _this = this; + this.sendJSON({ + type: 'djQueueModSwap', + data: { + uid1: uid1, + uid2: uid2, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('djQueueModSwapReceived', function (data) { + if (data.error) + reject('djQueueModSwap error: ' + + data.error); + resolve(data); + }); + }); + }, + + addDj: function (uid) { + var _this = this; + this.sendJSON({ + type: 'djQueueModAdd', + data: { + uid: uid, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('djQueueModAddReceived', function (data) { + if (data.error) + reject('djQueueModAdd error: ' + + data.error); + resolve(data); + }); + }); + }, + + removeDj: function (uid) { + var _this = this; + this.sendJSON({ + type: 'djQueueModRemove', + data: { + uid: uid, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('djQueueModRemoveReceived', function ( + data) { + if (data.error) + reject('djQueueModRemove error: ' + + data.error); + resolve(data); + }); + }); + }, + + broadcast: function (message) { + var _this = this; + this.sendJSON({ + type: 'broadcastMessage', + data: { + message: message, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('broadcastMessageReceived', function ( + data) { + if (data.error) + reject('broadcastMessage error: ' + + data.error); + resolve(data); + }); + }); + }, + + setLimit: function (limit) { + var _this = this; + this.sendJSON({ + type: 'djQueueLimit', + data: { + limit: limit, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('djQueueLimitReceived', function (data) { + if (data.error) + reject('djQueueLimit error: ' + + data.error); + resolve(data); + }); + }); + }, + + setRole: function (uid, role) { + var _this = this; + this.sendJSON({ + type: 'setRole', + data: { + uid: uid, + role: role, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('setRoleReceived', function (data) { + if (data.error) + reject('setRole error: ' + data.error); + resolve(data); + }); + }); + }, + + ban: function (uid, duration, reason) { + var _this = this; + this.sendJSON({ + type: 'banUser', + data: { + uid: uid, + duration: duration, + reason: reason, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('banUserReceived', function (data) { + if (data.error) + reject('banUser error: ' + data.error + + (data.text ? ' - ' + data.text : + '')); + resolve(data); + }); + }); + }, + + unban: function (uid) { + var _this = this; + this.sendJSON({ + type: 'unbanUser', + data: { + uid: uid, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('unbanUserReceived', function (data) { + if (data.error) + reject('unbanUser error: ' + data.error); + resolve(data); + }); + }); + }, + + whois: function (uid, un) { + var _this = this; + this.sendJSON({ + type: 'whois', + data: { + uid: uid, + un: un, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('whoisReceived', function (data) { + if (data.error) + reject('whois error: ' + data.error); + resolve(data); + }); + }); + }, + + getUser: function (uid) { + if (!uid) + return this.user; + return this.users[uid] ? this.users[uid] : null; + }, + + getUsers: function () { + return this.users; + }, + + sendMessage: function (message) { + var _this = this; + this.sendJSON({ + type: 'chat', + data: { + message: message, + }, + }); + return new Promise(function (resolve, reject) { + _this.once('chatReceived', function (data) { + resolve(data); + }); + }); + }, +}; diff --git a/src/events.js b/src/events.js new file mode 100644 index 0000000..70bdf9c --- /dev/null +++ b/src/events.js @@ -0,0 +1,6 @@ +const Promise = require('bluebird'); +const EventEmitter = require('events').EventEmitter; +const events = new EventEmitter(); +events.emit = Promise.promisify(events.emit); + +module.exports = events; diff --git a/test/cmds.js b/test/cmds.js new file mode 100644 index 0000000..fc4a89c --- /dev/null +++ b/test/cmds.js @@ -0,0 +1,154 @@ +'use strict'; +const test = require('ava'); +const events = require('./../src/events'); +var cmds = require('./../src/cmds'); + +test.before(t => { + cmds.once = function (type, value) { + return events.once(type, value); + }; + + cmds.sendJSON = (data) => { + function handleResponse(msgObj) { + if ((JSON.stringify(data.data) == JSON.stringify(msgObj)) || (!data.data && (data.type == msgObj.type))) + events.emit(data.type + 'Received', data.type); + else { + events.emit(data.type + 'Received', 'Error'); + } + } + + setTimeout(function () { + var msgObj; + switch (data.type) { + case 'chat': + handleResponse({ message: 'message' }); + break; + case 'getRoomInfo': + handleResponse({ type: 'getRoomInfo' }); + break; + case 'privateMessage': + handleResponse({ uid: 1, message: 'msg' }); + break; + case 'djQueueJoin': + handleResponse({ type: 'djQueueJoin' }); + break; + case 'djQueueLeave': + handleResponse({ type: 'djQueueLeave' }); + break; + case 'djQueueLock': + handleResponse({ type: 'djQueueLock' }); + break; + case 'djQueueCycle': + handleResponse({ type: 'djQueueCycle' }); + break; + case 'djQueueModSkip': + handleResponse({ type: 'djQueueModSkip' }); + break; + case 'djQueueModMove': + handleResponse({ + uid: 1, + position: 2, + }); + break; + case 'deleteChat': + handleResponse({ + cid: 1, + mid: 2, + }); + break; + case 'djQueueModSwap': + handleResponse({ + uid1: 1, + uid2: 2, + }); + break; + case 'djQueueModAdd': + handleResponse({ + uid: 1, + }); + break; + default: + events.emit(data.type + 'Received', 'Error'); + }; + }, 0); // Promise needs to be registered first + }; +}); + +test('Successfully sends Message', t => { + return cmds.sendMessage('message').then(data => { + t.is(data, 'chat'); + }); +}); +test('Successfully gets RoomInfo', t => { + return cmds.getRoomInfo().then(data => { + t.is(data, 'getRoomInfo'); + }); +}); +test('Successfully sends privateMessage', t => { + return cmds.sendPrivateMessage(1, 'msg').then(data => { + t.is(data, 'privateMessage'); + }); +}); +test('Successfully joinQueue', t => { + return cmds.joinQueue().then(data => { + t.is(data, 'djQueueJoin'); + }); +}); +test('Successfully leaveQueue', t => { + return cmds.leaveQueue().then(data => { + t.is(data, 'djQueueLeave'); + }); +}); +test('Successfully lockQueue', t => { + return cmds.lockQueue().then(data => { + t.is(data, 'djQueueLock'); + }); +}); + +test('Successfully unlockQueue', t => { + return cmds.unlockQueue().then(data => { + t.is(data, 'djQueueLock'); + }); +}); + +test('Successfully toggleLockQueue', t => { + return cmds.toggleLockQueue().then(data => { + t.is(data, 'djQueueLock'); + }); +}); + +test('Successfully cycleQueue', t => { + return cmds.cycle().then(data => { + t.is(data, 'djQueueCycle'); + }); +}); + +test('Successfully skip', t => { + return cmds.skip().then(data => { + t.is(data, 'djQueueModSkip'); + }); +}); + +test('Successfully deleteChat', t => { + return cmds.deleteChat(1, 2).then(data => { + t.is(data, 'deleteChat'); + }); +}); + +test('Successfully djQueueModMove', t => { + return cmds.move(1, 2).then(data => { + t.is(data, 'djQueueModMove'); + }); +}); + +test('Successfully djQueueModSwap', t => { + return cmds.swap(1, 2).then(data => { + t.is(data, 'djQueueModSwap'); + }); +}); + +test('Successfully djQueueModAdd', t => { + return cmds.addDj(1).then(data => { + t.is(data, 'djQueueModAdd'); + }); +});