|
|
|
@ -24,13 +24,26 @@ exports.GuildHandler = class {
|
|
|
|
|
this.mention = false;
|
|
|
|
|
this.prefix = prefix || config.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('./data', () => {
|
|
|
|
|
utils.dirExistence('./data/gdb', () => {
|
|
|
|
|
this.db = new sqlite3.Database(`./data/gdb/${guild}.db`);
|
|
|
|
|
this.createTables();
|
|
|
|
|
this.db = new sqlite3.Database(`./data/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());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
this.registerMusicCommands();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroy(){
|
|
|
|
@ -67,22 +80,26 @@ exports.GuildHandler = class {
|
|
|
|
|
* @param msg
|
|
|
|
|
*/
|
|
|
|
|
handleMessage(msg) {
|
|
|
|
|
if (this.db) {
|
|
|
|
|
this.db.run(
|
|
|
|
|
'INSERT INTO messages (author, creation_timestamp, author_name, content) values (?, ?, ?, ?)',
|
|
|
|
|
[msg.author.id, msg.createdTimestamp, msg.author.username, msg.content],
|
|
|
|
|
(err) => {
|
|
|
|
|
if (err)
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
let answer = this.servant.parseCommand(msg);
|
|
|
|
|
if (!answer) return;
|
|
|
|
|
if (this.mention) {
|
|
|
|
|
msg.reply(answer);
|
|
|
|
|
if (this.ready) {
|
|
|
|
|
if (this.db) {
|
|
|
|
|
this.db.run(
|
|
|
|
|
'INSERT INTO messages (author, creation_timestamp, author_name, content) values (?, ?, ?, ?)',
|
|
|
|
|
[msg.author.id, msg.createdTimestamp, msg.author.username, msg.content],
|
|
|
|
|
(err) => {
|
|
|
|
|
if (err)
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
let answer = this.servant.parseCommand(msg);
|
|
|
|
|
if (!answer) return;
|
|
|
|
|
if (this.mention) {
|
|
|
|
|
msg.reply(answer);
|
|
|
|
|
} else {
|
|
|
|
|
msg.channel.send(answer);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
msg.channel.send(answer);
|
|
|
|
|
this.msgsQueue.push(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -279,18 +296,22 @@ exports.GuildHandler = class {
|
|
|
|
|
|
|
|
|
|
// saved command - prints out saved playlists
|
|
|
|
|
this.servant.createCommand(servercmd.music.saved, (msg) => {
|
|
|
|
|
let response = '```markdown\nSaved Playlists:\n==\n';
|
|
|
|
|
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.padEnd(10, ' ')}: ${row.url} \n\n`;
|
|
|
|
|
response += `[${row.name}](${row.url})\n`;
|
|
|
|
|
}
|
|
|
|
|
response += '```';
|
|
|
|
|
if (rows.length === 0)
|
|
|
|
|
if (rows.length === 0) {
|
|
|
|
|
msg.channel.send(servercmd.music.saved.response.no_saved);
|
|
|
|
|
else
|
|
|
|
|
msg.channel.send(response);
|
|
|
|
|
} else {
|
|
|
|
|
msg.channel.send('', {embed: {
|
|
|
|
|
"title": "Saved Songs and Playlists",
|
|
|
|
|
"description": response,
|
|
|
|
|
"fields": []
|
|
|
|
|
}});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
@ -302,7 +323,8 @@ exports.GuildHandler = class {
|
|
|
|
|
* @returns {GuildHandler}
|
|
|
|
|
*/
|
|
|
|
|
exports.getHandler = function (guild, prefix) {
|
|
|
|
|
if (!handlers[guild.id]) handlers[guild.id] = new this.GuildHandler(guild, prefix);
|
|
|
|
|
if (!handlers[guild.id])
|
|
|
|
|
handlers[guild.id] = new this.GuildHandler(guild, prefix);
|
|
|
|
|
return handlers[guild.id];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|