|
|
|
@ -4,6 +4,7 @@ const cmd = require('./cmd'),
|
|
|
|
|
config = require('../config.json'),
|
|
|
|
|
servercmd = require('../commands/servercommands'),
|
|
|
|
|
sqlite3 = require('sqlite3'),
|
|
|
|
|
Discord = require('discord.js'),
|
|
|
|
|
handlers = {},
|
|
|
|
|
dbDir = './data/gdb';
|
|
|
|
|
let logger = require('winston');
|
|
|
|
@ -74,6 +75,26 @@ exports.GuildHandler = class {
|
|
|
|
|
)`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Answers a message via mention if mentioning is active or with just sending it to the same channel.
|
|
|
|
|
* @param msg
|
|
|
|
|
* @param answer
|
|
|
|
|
*/
|
|
|
|
|
answerMessage(msg, answer) {
|
|
|
|
|
if (answer instanceof Promise || answer) {
|
|
|
|
|
if (answer instanceof Discord.RichEmbed) {
|
|
|
|
|
(this.mention)? msg.reply('', answer) : msg.channel.send('', answer);
|
|
|
|
|
} else if (answer instanceof Promise) {
|
|
|
|
|
answer
|
|
|
|
|
.then((answer) => this.answerMessage(msg, answer))
|
|
|
|
|
.catch((error) => this.answerMessage(msg, error));
|
|
|
|
|
} else {
|
|
|
|
|
(this.mention)? msg.reply(answer) : msg.channel.send(answer);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
logger.warning(`Empty answer won't be send.`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* handles the message by letting the servant parse the command. Depending on the message setting it
|
|
|
|
|
* replies or just sends the answer.
|
|
|
|
@ -91,13 +112,7 @@ exports.GuildHandler = class {
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
let answer = this.servant.parseCommand(msg);
|
|
|
|
|
if (!answer) return;
|
|
|
|
|
if (this.mention) {
|
|
|
|
|
msg.reply(answer);
|
|
|
|
|
} else {
|
|
|
|
|
msg.channel.send(answer);
|
|
|
|
|
}
|
|
|
|
|
this.answerMessage(msg, this.servant.parseCommand(msg));
|
|
|
|
|
} else {
|
|
|
|
|
this.msgsQueue.push(msg);
|
|
|
|
|
}
|
|
|
|
@ -107,15 +122,20 @@ exports.GuildHandler = class {
|
|
|
|
|
* Connect to a voice-channel if not connected and play the url
|
|
|
|
|
* @param vc
|
|
|
|
|
* @param url
|
|
|
|
|
* @param next
|
|
|
|
|
*/
|
|
|
|
|
connectAndPlay(vc, url) {
|
|
|
|
|
if (!this.dj.connected) {
|
|
|
|
|
this.dj.connect(vc).then(() => {
|
|
|
|
|
this.dj.playYouTube(url);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.dj.playYouTube(url);
|
|
|
|
|
}
|
|
|
|
|
connectAndPlay(vc, url, next) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
if (!this.dj.connected) {
|
|
|
|
|
this.dj.connect(vc).then(() => {
|
|
|
|
|
this.dj.playYouTube(url, next);
|
|
|
|
|
resolve();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.dj.playYouTube(url, next);
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -128,74 +148,86 @@ exports.GuildHandler = class {
|
|
|
|
|
|
|
|
|
|
// play command
|
|
|
|
|
this.servant.createCommand(servercmd.music.play, (msg, kwargs, argv) => {
|
|
|
|
|
let vc = msg.member.voiceChannel;
|
|
|
|
|
let url = kwargs['url'];
|
|
|
|
|
if (!vc)
|
|
|
|
|
return 'You are not connected to a VoiceChannel';
|
|
|
|
|
if (!url)
|
|
|
|
|
return servercmd.music.play.response.no_url;
|
|
|
|
|
if (!url.match(/http/g)) {
|
|
|
|
|
if (argv)
|
|
|
|
|
url += ' ' + argv.join(' ');
|
|
|
|
|
this.db.get('SELECT url FROM playlists WHERE name = ?', [url], (err, row) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err.message);
|
|
|
|
|
}
|
|
|
|
|
if (!row) {
|
|
|
|
|
return servercmd.music.play.response.url_invalid;
|
|
|
|
|
}
|
|
|
|
|
url = row.url;
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let vc = this.dj.voiceChannel || msg.member.voiceChannel;
|
|
|
|
|
let url = kwargs['url'];
|
|
|
|
|
if (!vc)
|
|
|
|
|
reject(servercmd.music.play.response.no_voicechannel);
|
|
|
|
|
if (!url)
|
|
|
|
|
reject(servercmd.music.play.response.no_url);
|
|
|
|
|
if (!url.match(/http/g)) {
|
|
|
|
|
if (argv && argv.length > 0)
|
|
|
|
|
url += ' ' + argv.join(' '); // join to get the whole expression behind the command
|
|
|
|
|
this.db.get('SELECT url FROM playlists WHERE name = ?', [url], (err, row) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err.message);
|
|
|
|
|
}
|
|
|
|
|
if (!row) {
|
|
|
|
|
reject(servercmd.music.play.response.url_invalid);
|
|
|
|
|
logger.verbose('Got invalid url for play command.');
|
|
|
|
|
} else {
|
|
|
|
|
url = row.url;
|
|
|
|
|
try {
|
|
|
|
|
this.connectAndPlay(vc, url).then(() => {
|
|
|
|
|
resolve(servercmd.music.play.response.success);
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
reject(servercmd.music.play.response.failure);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
this.connectAndPlay(vc, url);
|
|
|
|
|
this.connectAndPlay(vc, url).then(() => {
|
|
|
|
|
resolve(servercmd.music.play.response.success);
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
return servercmd.music.play.response.failure;
|
|
|
|
|
reject(servercmd.music.play.response.failure);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
this.connectAndPlay(vc, url);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
return servercmd.music.play.response.failure;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return servercmd.music.play.response.success;
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// playnext command
|
|
|
|
|
this.servant.createCommand(servercmd.music.playnext,(msg, kwargs, argv) => {
|
|
|
|
|
let vc = msg.member.voiceChannel;
|
|
|
|
|
if (!this.dj.connected) this.dj.voiceChannel = vc;
|
|
|
|
|
let url = kwargs['url'];
|
|
|
|
|
if (!url) return servercmd.music.playnext.response.no_url;
|
|
|
|
|
if (!url.match(/http/g)) {
|
|
|
|
|
if (argv)
|
|
|
|
|
url += ' ' + argv.join(' ');
|
|
|
|
|
this.db.get('SELECT url FROM playlists WHERE name = ?', [url], (err, row) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err.message);
|
|
|
|
|
}
|
|
|
|
|
if (!row) {
|
|
|
|
|
return servercmd.music.play.response.url_invalid;
|
|
|
|
|
}
|
|
|
|
|
url = row.url;
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let vc = msg.member.voiceChannel;
|
|
|
|
|
if (!this.dj.connected) this.dj.voiceChannel = vc;
|
|
|
|
|
let url = kwargs['url'];
|
|
|
|
|
if (!url) reject(servercmd.music.playnext.response.no_url);
|
|
|
|
|
if (!url.match(/http/g)) {
|
|
|
|
|
if (argv)
|
|
|
|
|
url += ' ' + argv.join(' ');
|
|
|
|
|
this.db.get('SELECT url FROM playlists WHERE name = ?', [url], (err, row) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err.message);
|
|
|
|
|
}
|
|
|
|
|
if (!row) {
|
|
|
|
|
reject(servercmd.music.play.response.url_invalid);
|
|
|
|
|
}
|
|
|
|
|
url = row.url;
|
|
|
|
|
try {
|
|
|
|
|
this.connectAndPlay(url, true).then(() => {
|
|
|
|
|
resolve(servercmd.music.playnext.response.success);
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
reject(servercmd.music.play.response.failure);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
this.dj.playYouTube(url, true);
|
|
|
|
|
this.connectAndPlay(url, true).then(() => {
|
|
|
|
|
resolve(servercmd.music.playnext.response.success);
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
return servercmd.music.play.response.failure;
|
|
|
|
|
logger.error(err);
|
|
|
|
|
reject(servercmd.music.playnext.response.failure);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
this.dj.playYouTube(url, true);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(err);
|
|
|
|
|
return servercmd.music.playnext.response.failure;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return servercmd.music.playnext.response.success;
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// join command
|
|
|
|
@ -274,44 +306,50 @@ exports.GuildHandler = class {
|
|
|
|
|
|
|
|
|
|
// saves playlists
|
|
|
|
|
this.servant.createCommand(servercmd.music.save, (msg, kwargs, argv) => {
|
|
|
|
|
let saveName = argv.join(' ');
|
|
|
|
|
this.db.get('SELECT COUNT(*) count FROM playlists WHERE name = ?', [saveName], (err, row) => {
|
|
|
|
|
if(err) {
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
}
|
|
|
|
|
if (!row || row.count === 0) {
|
|
|
|
|
this.db.run('INSERT INTO playlists (name, url) VALUES (?, ?)', [saveName, kwargs.url], (err) => {
|
|
|
|
|
if (err)
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.db.run('UPDATE playlists SET url = ? WHERE name = ?', [kwargs.url, saveName], (err) => {
|
|
|
|
|
if (err)
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let saveName = argv.join(' ');
|
|
|
|
|
this.db.get('SELECT COUNT(*) count FROM playlists WHERE name = ?', [saveName], (err, row) => {
|
|
|
|
|
if(err) {
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
}
|
|
|
|
|
if (!row || row.count === 0) {
|
|
|
|
|
this.db.run('INSERT INTO playlists (name, url) VALUES (?, ?)', [saveName, kwargs.url], (err) => {
|
|
|
|
|
if (err)
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
else
|
|
|
|
|
resolve(`Saved song/playlist as ${saveName}`);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.db.run('UPDATE playlists SET url = ? WHERE name = ?', [kwargs.url, saveName], (err) => {
|
|
|
|
|
if (err)
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
else
|
|
|
|
|
resolve(`Saved song/playlist as ${saveName}`);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return `Saved song/playlist as ${saveName}`
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// saved command - prints out saved playlists
|
|
|
|
|
this.servant.createCommand(servercmd.music.saved, (msg) => {
|
|
|
|
|
let response = '';
|
|
|
|
|
this.db.all('SELECT name, url FROM playlists', (err, rows) => {
|
|
|
|
|
if (err)
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
for (let row of rows) {
|
|
|
|
|
response += `[${row.name}](${row.url})\n`;
|
|
|
|
|
}
|
|
|
|
|
if (rows.length === 0) {
|
|
|
|
|
msg.channel.send(servercmd.music.saved.response.no_saved);
|
|
|
|
|
} else {
|
|
|
|
|
msg.channel.send('', {embed: {
|
|
|
|
|
"title": "Saved Songs and Playlists",
|
|
|
|
|
"description": response,
|
|
|
|
|
"fields": []
|
|
|
|
|
}});
|
|
|
|
|
}
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let response = '';
|
|
|
|
|
this.db.all('SELECT name, url FROM playlists', (err, rows) => {
|
|
|
|
|
if (err)
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
for (let row of rows) {
|
|
|
|
|
response += `[${row.name}](${row.url})\n`;
|
|
|
|
|
}
|
|
|
|
|
if (rows.length === 0) {
|
|
|
|
|
msg.channel.send(servercmd.music.saved.response.no_saved);
|
|
|
|
|
} else {
|
|
|
|
|
let richEmbed = new Discord.RichEmbed()
|
|
|
|
|
.setTitle('Saved Songs and Playlists')
|
|
|
|
|
.setDescription(response);
|
|
|
|
|
resolve(richEmbed);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|