|
|
@ -2,10 +2,10 @@ const cmd = require('./cmd'),
|
|
|
|
music = require('./music'),
|
|
|
|
music = require('./music'),
|
|
|
|
utils = require('./utils'),
|
|
|
|
utils = require('./utils'),
|
|
|
|
config = require('../config.json'),
|
|
|
|
config = require('../config.json'),
|
|
|
|
|
|
|
|
sqliteAsync = require('./sqliteAsync'),
|
|
|
|
|
|
|
|
fs = require('fs-extra'),
|
|
|
|
servercmd = require('../commands/servercommands'),
|
|
|
|
servercmd = require('../commands/servercommands'),
|
|
|
|
sqlite3 = require('sqlite3'),
|
|
|
|
|
|
|
|
Discord = require('discord.js'),
|
|
|
|
Discord = require('discord.js'),
|
|
|
|
handlers = {},
|
|
|
|
|
|
|
|
dataDir = config.dataPath || './data';
|
|
|
|
dataDir = config.dataPath || './data';
|
|
|
|
let logger = require('winston');
|
|
|
|
let logger = require('winston');
|
|
|
|
|
|
|
|
|
|
|
@ -26,26 +26,16 @@ exports.GuildHandler = class {
|
|
|
|
this.mention = false;
|
|
|
|
this.mention = false;
|
|
|
|
this.prefix = prefix || config.prefix;
|
|
|
|
this.prefix = prefix || config.prefix;
|
|
|
|
this.servant = new cmd.Servant(this.prefix);
|
|
|
|
this.servant = new cmd.Servant(this.prefix);
|
|
|
|
this.ready = false;
|
|
|
|
}
|
|
|
|
this.msgsQueue = [];
|
|
|
|
|
|
|
|
// checking if the data direcotry exists and if the gdb directory exists and creates them if they don't
|
|
|
|
|
|
|
|
utils.dirExistence(dataDir, () => {
|
|
|
|
|
|
|
|
utils.dirExistence(dataDir + '/gdb', () => {
|
|
|
|
|
|
|
|
this.db = new sqlite3.Database(`${dataDir}/gdb/${guild}.db`, (err) => {
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
|
|
|
logger.debug(`Connected to the database for ${guild}`);
|
|
|
|
|
|
|
|
this.createTables();
|
|
|
|
|
|
|
|
// register commands
|
|
|
|
|
|
|
|
this.registerMusicCommands();
|
|
|
|
|
|
|
|
this.ready = true;
|
|
|
|
|
|
|
|
// handle all messages that have been received while not being ready
|
|
|
|
|
|
|
|
for (let i = 0; i < this.msgsQueue.length; i++)
|
|
|
|
|
|
|
|
this.handleMessage(this.msgsQueue.shift());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
async initDatabase() {
|
|
|
|
});
|
|
|
|
await fs.ensureDir(dataDir + '/gdb');
|
|
|
|
});
|
|
|
|
this.db = new sqliteAsync.Database(`${dataDir}/gdb/${this.guild}.db`);
|
|
|
|
|
|
|
|
await this.db.init();
|
|
|
|
|
|
|
|
logger.debug(`Connected to the database for ${this.guild}`);
|
|
|
|
|
|
|
|
await this.createTables();
|
|
|
|
|
|
|
|
// register commands
|
|
|
|
|
|
|
|
this.registerMusicCommands();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -62,15 +52,15 @@ exports.GuildHandler = class {
|
|
|
|
* messages - logs all messages send on the server
|
|
|
|
* messages - logs all messages send on the server
|
|
|
|
* playlists - save playlists to play them later
|
|
|
|
* playlists - save playlists to play them later
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
createTables() {
|
|
|
|
async createTables() {
|
|
|
|
this.db.run(`${utils.sql.tableExistCreate} messages (
|
|
|
|
await this.db.run(`${utils.sql.tableExistCreate} messages (
|
|
|
|
${utils.sql.pkIdSerial},
|
|
|
|
${utils.sql.pkIdSerial},
|
|
|
|
creation_timestamp DATETIME NOT NULL,
|
|
|
|
creation_timestamp DATETIME NOT NULL,
|
|
|
|
author VARCHAR(128) NOT NULL,
|
|
|
|
author VARCHAR(128) NOT NULL,
|
|
|
|
author_name VARCHAR(128),
|
|
|
|
author_name VARCHAR(128),
|
|
|
|
content TEXT NOT NULL
|
|
|
|
content TEXT NOT NULL
|
|
|
|
)`);
|
|
|
|
)`);
|
|
|
|
this.db.run(`${utils.sql.tableExistCreate} playlists (
|
|
|
|
await this.db.run(`${utils.sql.tableExistCreate} playlists (
|
|
|
|
${utils.sql.pkIdSerial},
|
|
|
|
${utils.sql.pkIdSerial},
|
|
|
|
name VARCHAR(32) UNIQUE NOT NULL,
|
|
|
|
name VARCHAR(32) UNIQUE NOT NULL,
|
|
|
|
url VARCHAR(255) NOT NULL
|
|
|
|
url VARCHAR(255) NOT NULL
|
|
|
@ -88,14 +78,13 @@ exports.GuildHandler = class {
|
|
|
|
(this.mention) ? msg.reply('', answer) : msg.channel.send('', answer);
|
|
|
|
(this.mention) ? msg.reply('', answer) : msg.channel.send('', answer);
|
|
|
|
} else if (answer instanceof Promise) {
|
|
|
|
} else if (answer instanceof Promise) {
|
|
|
|
answer
|
|
|
|
answer
|
|
|
|
.then((answer) => this.answerMessage(msg, answer))
|
|
|
|
.then((resolvedAnswer) => this.answerMessage(msg, resolvedAnswer))
|
|
|
|
.catch((error) => this.answerMessage(msg, error));
|
|
|
|
.catch((error) => this.answerMessage(msg, error));
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
(this.mention) ? msg.reply(answer) : msg.channel.send(answer);
|
|
|
|
(this.mention) ? msg.reply(answer) : msg.channel.send(answer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
logger.debug(`Empty answer won't be send.`);
|
|
|
|
logger.debug(`Empty answer won't be send.`);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -103,22 +92,14 @@ exports.GuildHandler = class {
|
|
|
|
* replies or just sends the answer.
|
|
|
|
* replies or just sends the answer.
|
|
|
|
* @param msg
|
|
|
|
* @param msg
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
handleMessage(msg) {
|
|
|
|
async handleMessage(msg) {
|
|
|
|
if (this.ready) {
|
|
|
|
if (this.db)
|
|
|
|
if (this.db)
|
|
|
|
await this.db.run(
|
|
|
|
this.db.run(
|
|
|
|
'INSERT INTO messages (author, creation_timestamp, author_name, content) values (?, ?, ?, ?)',
|
|
|
|
'INSERT INTO messages (author, creation_timestamp, author_name, content) values (?, ?, ?, ?)',
|
|
|
|
[msg.author.id, msg.createdTimestamp, msg.author.username, msg.content]
|
|
|
|
[msg.author.id, msg.createdTimestamp, msg.author.username, msg.content],
|
|
|
|
);
|
|
|
|
(err) => {
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
this.answerMessage(msg, this.servant.parseCommand(msg));
|
|
|
|
logger.error(err.message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.answerMessage(msg, this.servant.parseCommand(msg));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.msgsQueue.push(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -127,18 +108,13 @@ exports.GuildHandler = class {
|
|
|
|
* @param url
|
|
|
|
* @param url
|
|
|
|
* @param next
|
|
|
|
* @param next
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
connectAndPlay(vc, url, next) {
|
|
|
|
async connectAndPlay(vc, url, next) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!this.dj.connected) {
|
|
|
|
if (!this.dj.connected) {
|
|
|
|
await this.dj.connect(vc);
|
|
|
|
this.dj.connect(vc).then(() => {
|
|
|
|
this.dj.playYouTube(url, next);
|
|
|
|
this.dj.playYouTube(url, next);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
this.dj.playYouTube(url, next);
|
|
|
|
}).catch((err) => reject(err));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.dj.playYouTube(url, next);
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -147,82 +123,38 @@ exports.GuildHandler = class {
|
|
|
|
registerMusicCommands() {
|
|
|
|
registerMusicCommands() {
|
|
|
|
this.dj = new music.DJ();
|
|
|
|
this.dj = new music.DJ();
|
|
|
|
|
|
|
|
|
|
|
|
// play command
|
|
|
|
let playCb = async (msg, kwargs, argv, template, next) => {
|
|
|
|
this.servant.createCommand(servercmd.music.play, (msg, kwargs, argv) => {
|
|
|
|
let vc = this.dj.voiceChannel || msg.member.voiceChannel;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let url = kwargs['url'];
|
|
|
|
let vc = this.dj.voiceChannel || msg.member.voiceChannel;
|
|
|
|
if (!vc)
|
|
|
|
let url = kwargs['url'];
|
|
|
|
return template.response.no_voicechannel;
|
|
|
|
if (!vc)
|
|
|
|
if (!url)
|
|
|
|
reject(servercmd.music.play.response.no_voicechannel);
|
|
|
|
return template.response.no_url;
|
|
|
|
if (!url)
|
|
|
|
if (!utils.YouTube.isValidEntityUrl(url)) {
|
|
|
|
reject(servercmd.music.play.response.no_url);
|
|
|
|
if (argv && argv.length > 0)
|
|
|
|
if (!utils.YouTube.isValidEntityUrl(url)) {
|
|
|
|
url += ' ' + argv.join(' '); // join to get the whole expression behind the command
|
|
|
|
if (argv && argv.length > 0)
|
|
|
|
let row = await this.db.get('SELECT url FROM playlists WHERE name = ?', [url]);
|
|
|
|
url += ' ' + argv.join(' '); // join to get the whole expression behind the command
|
|
|
|
if (!row) {
|
|
|
|
this.db.get('SELECT url FROM playlists WHERE name = ?', [url], (err, row) => {
|
|
|
|
logger.debug('Got invalid url for play command.');
|
|
|
|
if (err)
|
|
|
|
return template.response.url_invalid;
|
|
|
|
logger.error(err.message);
|
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
|
|
|
|
reject(servercmd.music.play.response.url_invalid);
|
|
|
|
|
|
|
|
logger.verbose('Got invalid url for play command.');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
url = row.url;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.connectAndPlay(vc, url).then(() => {
|
|
|
|
|
|
|
|
resolve(servercmd.music.play.response.success);
|
|
|
|
|
|
|
|
}).catch((err) => {
|
|
|
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
|
|
|
reject(servercmd.music.play.response.failure);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.connectAndPlay(vc, url).then(() => {
|
|
|
|
await this.connectAndPlay(vc, row.url, next);
|
|
|
|
resolve(servercmd.music.play.response.success);
|
|
|
|
return template.response.success;
|
|
|
|
}).catch((err) => {
|
|
|
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
|
|
|
reject(servercmd.music.play.response.failure);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
|
|
|
|
await this.connectAndPlay(vc, url, next);
|
|
|
|
|
|
|
|
return template.response.success;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// play command
|
|
|
|
|
|
|
|
this.servant.createCommand(servercmd.music.play, async (msg, kwargs, argv) => {
|
|
|
|
|
|
|
|
return await playCb(msg, kwargs, argv, servercmd.music.play, false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// playnext command
|
|
|
|
// playnext command
|
|
|
|
this.servant.createCommand(servercmd.music.playnext, (msg, kwargs, argv) => {
|
|
|
|
this.servant.createCommand(servercmd.music.playnext, async (msg, kwargs, argv) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return await playCb(msg, kwargs, argv, servercmd.music.playnext, true);
|
|
|
|
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 (!utils.YouTube.isValidEntityUrl(url)) {
|
|
|
|
|
|
|
|
if (argv)
|
|
|
|
|
|
|
|
url += ' ' + argv.join(' ');
|
|
|
|
|
|
|
|
this.db.get('SELECT url FROM playlists WHERE name = ?', [url], (err, row) => {
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
|
|
|
|
reject(servercmd.music.play.response.url_invalid);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
url = row.url;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.connectAndPlay(vc, url, true).then(() => {
|
|
|
|
|
|
|
|
resolve(servercmd.music.playnext.response.success);
|
|
|
|
|
|
|
|
}).catch((err) => {
|
|
|
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
|
|
|
reject(servercmd.music.play.response.failure);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.connectAndPlay(vc, url, true).then(() => {
|
|
|
|
|
|
|
|
resolve(servercmd.music.playnext.response.success);
|
|
|
|
|
|
|
|
}).catch((err) => {
|
|
|
|
|
|
|
|
logger.error(err);
|
|
|
|
|
|
|
|
reject(servercmd.music.playnext.response.failure);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// join command
|
|
|
|
// join command
|
|
|
@ -326,75 +258,29 @@ exports.GuildHandler = class {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// saves playlists
|
|
|
|
// saves playlists
|
|
|
|
this.servant.createCommand(servercmd.music.save, (msg, kwargs, argv) => {
|
|
|
|
this.servant.createCommand(servercmd.music.save, async (msg, kwargs, argv) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let saveName = argv.join(' ');
|
|
|
|
let saveName = argv.join(' ');
|
|
|
|
let row = await this.db.get('SELECT COUNT(*) count FROM playlists WHERE name = ?', [saveName]);
|
|
|
|
this.db.get('SELECT COUNT(*) count FROM playlists WHERE name = ?', [saveName], (err, row) => {
|
|
|
|
if (!row || row.count === 0)
|
|
|
|
if (err) {
|
|
|
|
await this.db.run('INSERT INTO playlists (name, url) VALUES (?, ?)', [saveName, kwargs.url]);
|
|
|
|
logger.error(err.message);
|
|
|
|
else
|
|
|
|
reject();
|
|
|
|
await this.db.run('UPDATE playlists SET url = ? WHERE name = ?', [kwargs.url, saveName]);
|
|
|
|
}
|
|
|
|
return `Saved song/playlist as ${saveName}`;
|
|
|
|
let cb = (err) => { // defining the callback for usage below
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
resolve(`Saved song/playlist as ${saveName}`);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!row || row.count === 0)
|
|
|
|
|
|
|
|
this.db.run('INSERT INTO playlists (name, url) VALUES (?, ?)', [saveName, kwargs.url], cb);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
this.db.run('UPDATE playlists SET url = ? WHERE name = ?', [kwargs.url, saveName], cb);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// saved command - prints out saved playlists
|
|
|
|
// saved command - prints out saved playlists
|
|
|
|
this.servant.createCommand(servercmd.music.saved, (msg) => {
|
|
|
|
this.servant.createCommand(servercmd.music.saved, async (msg) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let response = '';
|
|
|
|
let response = '';
|
|
|
|
let rows = await this.db.all('SELECT name, url FROM playlists');
|
|
|
|
this.db.all('SELECT name, url FROM playlists', (err, rows) => {
|
|
|
|
for (let row of rows)
|
|
|
|
if (err) {
|
|
|
|
response += `[${row.name}](${row.url})\n`;
|
|
|
|
logger.error(err.message);
|
|
|
|
|
|
|
|
reject();
|
|
|
|
if (rows.length === 0)
|
|
|
|
}
|
|
|
|
msg.channel.send(servercmd.music.saved.response.no_saved);
|
|
|
|
for (let row of rows)
|
|
|
|
else
|
|
|
|
response += `[${row.name}](${row.url})\n`;
|
|
|
|
return new Discord.RichEmbed()
|
|
|
|
|
|
|
|
.setTitle('Saved Songs and Playlists')
|
|
|
|
if (rows.length === 0) {
|
|
|
|
.setDescription(response);
|
|
|
|
msg.channel.send(servercmd.music.saved.response.no_saved);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let richEmbed = new Discord.RichEmbed()
|
|
|
|
|
|
|
|
.setTitle('Saved Songs and Playlists')
|
|
|
|
|
|
|
|
.setDescription(response);
|
|
|
|
|
|
|
|
resolve(richEmbed);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param guild
|
|
|
|
|
|
|
|
* @param prefix
|
|
|
|
|
|
|
|
* @returns {GuildHandler}
|
|
|
|
|
|
|
|
* @deprecated use Bot class method instead
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.getHandler = function (guild, prefix) {
|
|
|
|
|
|
|
|
if (!handlers[guild.id])
|
|
|
|
|
|
|
|
handlers[guild.id] = new this.GuildHandler(guild, prefix);
|
|
|
|
|
|
|
|
return handlers[guild.id];
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Destroy all handlers to safely end all sql3-clients.
|
|
|
|
|
|
|
|
* @deprecated automated in Bot class cleanup
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.destroyAll = function () {
|
|
|
|
|
|
|
|
logger.debug('Destroying all handlers...');
|
|
|
|
|
|
|
|
for (let key in Object.keys(handlers))
|
|
|
|
|
|
|
|
if (handlers[key])
|
|
|
|
|
|
|
|
handlers[key].destroy();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|