Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixes #20

  • Loading branch information...
commit 67c8b78d1d679634729f9c84e3a44d52f90eea13 1 parent 9489212
Mac Angell authored

Showing 1 changed file with 137 additions and 42 deletions. Show diff stats Hide diff stats

  1. +137 42 lib/DjAddOn.js
179 lib/DjAddOn.js
@@ -28,35 +28,109 @@ module.exports = function DjAddOn(bot) {
28 28 bot.addDj();
29 29 replyFunc('Ok, hope you are prepared for my playlist!');
30 30 },
31   - snagSong = function(msgData, issuerId, replyFunc) {
32   - bot.snag(function() {
33   - bot.playlistAll(playlistName, function(data) {
34   - if (data.success) {
35   - var song = bot.getCurrentSong();
36   - if (song) {
37   - bot.playlistAdd(playlistName, song._id, data.list.length);
38   - replyFunc('Ok, I\'ve added "' + song.song + '" to my playlist.');
  31 + getPlaylistInfoNoSwitch = function(playlistName, callback) {
  32 + bot.playlistListAll(function(data) {
  33 + if (data.success) {
  34 + var active = _.find(data.list, function(list) { return list.active; });
  35 + bot.playlistAll(playlistName, function(playlistData) {
  36 + if (playlistData.success) {
  37 + bot.playlistSwitch(active.name, function() {
  38 + callback(null, playlistData);
  39 + });
39 40 }
40 41 else {
41   - replyFunc('Sorry, there is no song playing at the moment.');
  42 + callback(new Error('Unable to retrieve playlist data'), null);
42 43 }
  44 + });
  45 + }
  46 + else {
  47 + callback(new Error('Unable to retrieve playlist data'), null);
  48 + }
  49 + });
  50 + },
  51 + addToPlaylistEndNoSwitch = function(playlistName, songId, callback) {
  52 + bot.playlistListAll(function(data) {
  53 + if (data.success) {
  54 + var active = _.find(data.list, function(list) { return list.active; });
  55 + bot.snag(function() {
  56 + bot.playlistAll(playlistName, function(playlistData) {
  57 + if (playlistData.success) {
  58 + bot.playlistAdd(playlistName, songId, playlistData.list.length, function() {
  59 + bot.playlistSwitch(active.name, function() {
  60 + callback(null, playlistData);
  61 + });
  62 + });
  63 + }
  64 + else {
  65 + callback(new Error('Unable to retrieve playlist data'), null);
  66 + }
  67 + });
  68 + });
  69 + }
  70 + else {
  71 + callback(new Error('Unable to retrieve playlist data'), null);
  72 + }
  73 + });
  74 + },
  75 + removeFromPlaylistNoSwitch = function(playlistName, songId, callback) {
  76 + bot.playlistListAll(function(data) {
  77 + if (data.success) {
  78 + var active = _.find(data.list, function(list) { return list.active; });
  79 + bot.playlistAll(playlistName, function(playlistData) {
  80 + if (playlistData.success) {
  81 + var songPos = -1;
  82 + for (var i = 0; i < playlistData.list.length; i++) {
  83 + if (playlistData.list[i]._id === songId) {
  84 + songPos = i;
  85 + break;
  86 + }
  87 + }
  88 + if (songPos >= 0) {
  89 + bot.playlistRemove(playlistName, songPos, function() {
  90 + bot.playlistSwitch(active.name, function() {
  91 + callback(null, playlistData);
  92 + });
  93 + });
  94 + }
  95 + else {
  96 + callback(new Error('Not Found'), null);
  97 + }
  98 + }
  99 + else {
  100 + callback(new Error('Unable to retrieve playlist data'), null);
  101 + }
  102 + });
  103 + }
  104 + else {
  105 + callback(new Error('Unable to retrieve playlist data'), null);
  106 + }
  107 + });
  108 + },
  109 + snagSong = function(msgData, issuerId, replyFunc) {
  110 + var song = bot.getCurrentSong();
  111 + if (song) {
  112 + addToPlaylistEndNoSwitch(playlistName, song._id, function(err, data) {
  113 + if (err) {
  114 + return replyFunc('Sorry, there was a problem adding the song to my playlist.');
43 115 }
  116 + replyFunc('Ok, I\'ve added "' + song.song + '" to my playlist.');
44 117 });
45   - });
  118 + }
  119 + else {
  120 + replyFunc('Sorry, there is no song playing at the moment.');
  121 + }
46 122 },
47 123 dropSong = function(msgData, issuerId, replyFunc) {
48 124 var song = bot.getCurrentSong();
49 125 if (song) {
50   - bot.playlistAll(playlistName, function(data) {
51   - if (data.success) {
52   - for (var i = 0; i < data.list.length; i++) {
53   - if (data.list[i]._id === song._id) {
54   - bot.playlistRemove(playlistName, i);
55   - return replyFunc('Ok, I\'ve removed "' + song.song + '" from my playlist.');
56   - }
  126 + removeFromPlaylistNoSwitch(playlistName, song._id, function(err, data) {
  127 + if (err) {
  128 + if (err.message === 'Not Found') {
  129 + return replyFunc('Sorry, I could not find this song in my playlist.');
57 130 }
58   - replyFunc('I couldn\'t find "' + song.song + '" anywhere in my playlist.');
  131 + return replyFunc('Sorry, there was a problem removing the song from my playlist.');
59 132 }
  133 + replyFunc('Ok, I\'ve removed "' + song.song + '" from my playlist.');
60 134 });
61 135 }
62 136 else {
@@ -64,36 +138,57 @@ module.exports = function DjAddOn(bot) {
64 138 }
65 139 },
66 140 wipeSongs = function(msgData, issuerId, replyFunc) {
67   - bot.playlistAll(playlistName, function(data) {
  141 + bot.playlistListAll(function(data) {
68 142 if (data.success) {
69   - if (data.list.length < 1) {
70   - replyFunc('My playlist is currently empty.');
71   - }
72   - for (var i = 0; i < data.list.length; i++) {
73   - (function(idx) {
74   - setTimeout(function() {
75   - bot.playlistRemove(playlistName, 0);
76   - }, 500*idx);
77   - }(i));
78   - }
79   - replyFunc('My playlist is in the process of being cleared.');
  143 + bot.playlistAll(playlistName, function(playlistData) {
  144 + if (playlistData.success) {
  145 + if (playlistData.list.length < 1) {
  146 + replyFunc('My playlist is currently empty.');
  147 + }
  148 + var active = _.find(data.list, function(list) {
  149 + return list.active;
  150 + });
  151 + for (var i = 0; i < playlistData.list.length; i++) {
  152 + (function(idx, isLast) {
  153 + setTimeout(function() {
  154 + if (isLast) {
  155 + bot.playlistRemove(playlistName, 0, function() {
  156 + bot.playlistSwitch(active.name);
  157 + });
  158 + }
  159 + else {
  160 + bot.playlistRemove(playlistName, 0);
  161 + }
  162 + }, 500*idx);
  163 + }(i, (i===playlistData.list.length-1)));
  164 + }
  165 + replyFunc('My playlist is in the process of being cleared.');
  166 + }
  167 + else {
  168 + replyFunc('Sorry, there was a problem removing all songs from my playlist.');
  169 + }
  170 + });
80 171 }
81   - });
  172 + else {
  173 + replyFunc('Sorry, there was a problem removing all songs from my playlist.');
  174 + }
  175 + });
82 176 },
83 177 announceTopSongs = function(msgData, issuerId, replyFunc) {
84   - bot.playlistAll(playlistName, function(data) {
85   - if (data.success) {
86   - if (data.list.length < 1) {
87   - replyFunc('My playlist is currently empty.');
88   - }
89   - var messages = [], i;
90   - for (i = 0; i < data.list.length; i++) {
91   - if (i < 10) {
92   - messages.push(':small_blue_diamond: "' + data.list[i].metadata.song + '" by ' + data.list[i].metadata.artist);
93   - }
  178 + getPlaylistInfoNoSwitch(playlistName, function(err, data) {
  179 + if (err) {
  180 + return replyFunc('Sorry, there was a problem retrieving the songs in my playlist.');
  181 + }
  182 + if (data.list.length < 1) {
  183 + return replyFunc('My playlist is currently empty.');
  184 + }
  185 + var messages = [], i;
  186 + for (i = 0; i < data.list.length; i++) {
  187 + if (i < 10) {
  188 + messages.push(':small_blue_diamond: "' + data.list[i].metadata.song + '" by ' + data.list[i].metadata.artist);
94 189 }
95   - replyFunc(messages);
96 190 }
  191 + replyFunc(messages);
97 192 });
98 193 },
99 194 skipSong = function(msgData, issuerId, replyFunc) {

0 comments on commit 67c8b78

Please sign in to comment.
Something went wrong with that request. Please try again.