Added Data function

- DataHandler handles Data (wow)
- save to save playlist (server specific)
- saved to get saved playlist
- play with saved name plays the saved playlist
- data is stored in data folder
 - every server has a subfolder
 - entry file to manage all files that should be accessable
- fixed bug where music doesn't play when play was invoked
 -  if this bug appeares again, the skip command could fix it (because it creates a dispatcher if none exists)
pull/9/head
Trivernis 6 years ago
parent 8d53433dea
commit c884211150

@ -1,7 +1,6 @@
const Discord = require("discord.js"), const Discord = require("discord.js"),
fs = require('fs'), fs = require('fs'),
logger = require('./lib/logging').getLogger(), logger = require('./lib/logging').getLogger(),
music = require('./lib/music'),
cmd = require("./lib/cmd"), cmd = require("./lib/cmd"),
guilding = require('./lib/guilding'), guilding = require('./lib/guilding'),
client = new Discord.Client(), client = new Discord.Client(),
@ -10,28 +9,16 @@ const Discord = require("discord.js"),
prefix = args.prefix || '~', prefix = args.prefix || '~',
gamepresence = args.game || 'NieR:Automata'; gamepresence = args.game || 'NieR:Automata';
let savedplaylists = {};
function main() { function main() {
music.setLogger(logger);
cmd.setLogger(logger); cmd.setLogger(logger);
guilding.setLogger(logger);
cmd.init(prefix); cmd.init(prefix);
if (fs.existsSync('./data/savedplaylists.json')) {
savedplaylists = JSON.parse(fs.readFileSync('./data/savedplaylists.json'))
}
registerCommands(); registerCommands();
client.login(authToken).then(()=> { client.login(authToken).then(()=> {
logger.debug("Logged in"); logger.debug("Logged in");
}); });
} }
function savePlaylist(url, name) {
savedplaylists[name] = url;
fs.writeFile('./data/savedplaylists.json',JSON.stringify(savedplaylists), (err) => {
if (err) logger.warn(JSON.stringify(err));
})
}
function registerCommands() { function registerCommands() {
cmd.createGlobalCommand(prefix + 'ping', () => { cmd.createGlobalCommand(prefix + 'ping', () => {
return 'Pong!'; return 'Pong!';
@ -40,15 +27,8 @@ function registerCommands() {
cmd.createGlobalCommand(prefix + 'repeatafterme', (msg, argv, args) => { cmd.createGlobalCommand(prefix + 'repeatafterme', (msg, argv, args) => {
return args.join(' '); return args.join(' ');
},[], "Repeats what you say"); },[], "Repeats what you say");
cmd.createGlobalCommand(prefix + 'save', (msg, argv) => {
savePlaylist(argv['url'], argv['name']);
return `Saved song/playlist as ${argv['name']}`
}, ['url', 'name'], "Saves the YouTube song/playlist with a specific name");
} }
// defining the client's handlers
client.on('ready', () => { client.on('ready', () => {
logger.info(`logged in as ${client.user.tag}!`); logger.info(`logged in as ${client.user.tag}!`);
client.user.setPresence({game: {name: gamepresence, type: "PLAYING"}, status: 'online'}); client.user.setPresence({game: {name: gamepresence, type: "PLAYING"}, status: 'online'});

@ -16,7 +16,7 @@ exports.Servant = class {
let helpstr = "```markdown\n"; let helpstr = "```markdown\n";
helpstr += "Commands\n---\n"; helpstr += "Commands\n---\n";
Object.entries(globCommands).concat(Object.entries(this.commands)).forEach(([key, value]) => { Object.entries(globCommands).concat(Object.entries(this.commands)).forEach(([key, value]) => {
let cmdhelp = `${key} [${value.args.join('] [')}]`.padEnd(25, ' '); let cmdhelp = `${key} [${value.args.join('] [')}]`.replace('[]', '').padEnd(25, ' ');
cmdhelp += value.description || ''; cmdhelp += value.description || '';
helpstr += `\n${cmdhelp}\n`; helpstr += `\n${cmdhelp}\n`;
}); });
@ -116,7 +116,7 @@ exports.init = function(prefix) {
let helpstr = "```markdown\n"; let helpstr = "```markdown\n";
helpstr += "Commands\n---\n"; helpstr += "Commands\n---\n";
Object.entries(globCommands).forEach(([key, value]) => { Object.entries(globCommands).forEach(([key, value]) => {
let cmdhelp = `${key} [${value.args.join('] [')}]`.padEnd(25, ' '); let cmdhelp = `${key} [${value.args.join('] [')}]`.replace('[]', '').padEnd(25, ' ');
cmdhelp += value.description || ''; cmdhelp += value.description || '';
helpstr += `\n${cmdhelp}\n`; helpstr += `\n${cmdhelp}\n`;
}); });

@ -0,0 +1,102 @@
/* Module definition */
const
fs = require('fs'),
path = require('path');
/* Variable Definition */
let logger = require('winston'),
datapath = './data',
entryfile = 'fileentries.json';
fs.exists(datapath, (exist) => {
if (!exist) {
fs.mkdir(datapath, (err) => {
if (err)
logger.warn(JSON.stringify(err));
})
}
});
/* Function Definition */
exports.DataHandler = class {
constructor(name) {
this.workingDir = path.join(datapath, name);
this.fileEntries = {};
this.fileData = {};
fs.exists(this.workingDir, (exists) => {
if (!exists) {
fs.mkdir(this.workingDir, (err) => {
if (err)
logger.error(JSON.stringify(err));
});
}
});
if (fs.existsSync(this.getfp(entryfile))) {
try {
this.fileEntries = this.getJSONSync(entryfile);
} catch (err) {
logger.error(JSON.stringify(err));
}
}
}
addEntry(name, path) {
this.fileEntries.name = path;
this.refreshEntries();
}
getfp(file) {
return path.join(this.workingDir, file);
}
getcont(file, callback) {
fs.readFile(this.getfp, 'utf-8', callback);
}
getJSONSync(file) {
return JSON.parse(fs.readFileSync(this.getfp(file), 'utf-8'));
}
refreshEntries() {
fs.writeFile(this.getfp(entryfile), JSON.stringify(this.fileEntries), (err) => {
if (err)
logger.warn(JSON.stringify(err));
});
}
getData(name) {
try {
if (this.fileData[name])
return this.fileData[name];
else if (this.fileEntries[name]) {
this.fileData[name] = this.getJSONSync(this.fileEntries[name]);
return this.fileData[name];
}
return {};
} catch (err) {
logger.error(JSON.stringify(err));
}
}
setData(name, data) {
this.fileData[name] = data;
if (!this.fileEntries[name]) {
this.fileEntries[name] = name + '.json';
}
this.refreshEntries();
fs.writeFile(this.getfp(this.fileEntries[name]), JSON.stringify(this.fileData[name]), (err) => {
if (err)
logger.warn(JSON.stringify(err));
});
}
};
/**
* Getting the logger
* @param {Object} newLogger
*/
exports.setLogger = function (newLogger) {
logger = newLogger;
};

@ -1,14 +0,0 @@
/* Module definition */
/* Variable Definition */
let logger = require('winston');
/* Function Definition */
// TODO: Class that handles file-data for a server, functions to get/set data for specific server id
/**
* Getting the logger
* @param {Object} newLogger
*/
exports.getLogger = function (newLogger) {
logger = newLogger;
};

@ -1,25 +1,41 @@
const cmd = require('./cmd'), const cmd = require('./cmd'),
music = require('./music'), music = require('./music'),
data = require('./data'),
handlers = {}; handlers = {};
let logger = require('winston'); let logger = require('winston');
exports.setLogger = function(newLogger) { exports.setLogger = function (newLogger) {
logger = newLogger; logger = newLogger;
music.setLogger(logger);
}; };
exports.GuildHandler = class{ exports.GuildHandler = class {
constructor(guild, prefix) { constructor(guild, prefix) {
this.guild = guild; this.guild = guild;
this.dataHandler = new data.DataHandler(guild.name);
this.dj = null; this.dj = null;
this.servant = null; this.servant = null;
this.mention = false; this.mention = false;
this.prefix = prefix || '~'; this.prefix = prefix || '~';
this.data = {
savedplaylists: {}
};
this.registerMusicCommands(); this.registerMusicCommands();
} }
getData(name) {
return this.dataHandler.getData(name);
}
appendData(name, key, value) {
let data = this.getData(name);
data[key] = value;
this.dataHandler.setData(name, data);
}
deleteDataEntry(name, key) {
let data = this.getData(name);
delete data[key];
this.dataHandler.setData(name, data);
}
registerMusicCommands(cmdPrefix) { registerMusicCommands(cmdPrefix) {
let prefix = cmdPrefix || this.prefix; let prefix = cmdPrefix || this.prefix;
this.dj = new music.DJ(); this.dj = new music.DJ();
@ -33,8 +49,8 @@ exports.GuildHandler = class{
if (!url) if (!url)
return 'No url given.'; return 'No url given.';
if (!url.match(/http/g)) { if (!url.match(/http/g)) {
if (this.data.savedplaylists[url]) { if (this.getData('savedplaylists') && this.getData('savedplaylists')[url]) {
url = this.data.savedplaylists[url]; url = this.getData('savedplaylists')[url];
} else { } else {
return 'Not a valid url.'; return 'Not a valid url.';
} }
@ -44,10 +60,10 @@ exports.GuildHandler = class{
this.dj.connect(vc).then(() => { this.dj.connect(vc).then(() => {
this.dj.playYouTube(url); this.dj.playYouTube(url);
}); });
}else { } else {
return this.dj.playYouTube(url); return this.dj.playYouTube(url);
} }
} catch(err) { } catch (err) {
logger.error(err); logger.error(err);
return `${JSON.stringify(err)}`; return `${JSON.stringify(err)}`;
} }
@ -60,15 +76,15 @@ exports.GuildHandler = class{
let url = argv['url']; let url = argv['url'];
if (!url) return 'No url given.'; if (!url) return 'No url given.';
if (!url.match(/http/g)) { if (!url.match(/http/g)) {
if (this.data.savedplaylists[url]) { if (this.getData('savedplaylists') && this.getData('savedplaylists')[url]) {
url = this.data.savedplaylists[url]; url = this.getData('savedplaylists')[url];
} else { } else {
return 'Not a valid url'; return 'Not a valid url';
} }
} }
try { try {
return this.dj.playYouTube(url, true); return this.dj.playYouTube(url, true);
} catch(err) { } catch (err) {
logger.error(err); logger.error(err);
return `${JSON.stringify(err)}`; return `${JSON.stringify(err)}`;
} }
@ -78,8 +94,7 @@ exports.GuildHandler = class{
this.createCommand(prefix + 'join', (msg) => { this.createCommand(prefix + 'join', (msg) => {
if (msg.member.voiceChannel) { if (msg.member.voiceChannel) {
this.dj.connect(msg.member.voiceChannel); this.dj.connect(msg.member.voiceChannel);
} } else {
else {
msg.reply("You are not connected to a voicechannel."); msg.reply("You are not connected to a voicechannel.");
} }
}, [], "Joins the VC you are in."); }, [], "Joins the VC you are in.");
@ -112,7 +127,7 @@ exports.GuildHandler = class{
this.createCommand(prefix + 'clear', () => { this.createCommand(prefix + 'clear', () => {
this.dj.clear(); this.dj.clear();
return "DJ-Queue cleared"; return "DJ-Queue cleared";
}, [],"Clears the playlist."); }, [], "Clears the playlist.");
// playlist command // playlist command
this.createCommand(prefix + 'playlist', () => { this.createCommand(prefix + 'playlist', () => {
@ -137,6 +152,22 @@ exports.GuildHandler = class{
this.dj.shuffle(); this.dj.shuffle();
return "Randomized the order of the queue." return "Randomized the order of the queue."
}, [], "Shuffles the playlist."); }, [], "Shuffles the playlist.");
// saves playlists
this.createCommand(prefix + 'save', (msg, argv) => {
this.appendData('savedplaylists', argv.name, argv.url);
return `Saved song/playlist as ${argv['name']}`
}, ['url', 'name'], "Saves the YouTube song/playlist with a specific name");
// saved command - prints out saved playlists
this.createCommand(prefix + 'saved', () => {
let response = '```markdown\nSaved Playlists:\n==\n'
Object.entries(this.getData('savedplaylists')).forEach(([key, value]) => {
response += `${key.padEnd(10, ' ')} ${value} \n\n`;
});
response += '```'
return response;
}, [], "Prints out all saved playlists.");
} }
createCommand(command, call, args, description) { createCommand(command, call, args, description) {
@ -156,7 +187,12 @@ exports.GuildHandler = class{
} }
}; };
exports.getHandler = function(guild, prefix) { /**
* @param guild
* @param prefix
* @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]; return handlers[guild.id];
}; };

@ -123,7 +123,7 @@ exports.DJ = class{
}); });
return; return;
} }
if (!this.playing) { if (!this.playing || !this.disp) {
logger.debug(`Playing ${url}`); logger.debug(`Playing ${url}`);
this.disp = this.conn.playStream(ytdl(url, { this.disp = this.conn.playStream(ytdl(url, {
filter: "audioonly" filter: "audioonly"
@ -232,12 +232,21 @@ exports.DJ = class{
/** /**
* Skips to the next song by ending the current StreamDispatcher and thereby triggering the * Skips to the next song by ending the current StreamDispatcher and thereby triggering the
* end event of the dispatcher that automatically plays the next song. * end event of the dispatcher that automatically plays the next song. If no dispatcher is found
* It tries to play the next song with playYouTube
*/ */
skip () { skip () {
logger.debug("Skipping song"); logger.debug("Skipping song");
if (this.disp !== null) { if (this.disp !== null) {
this.disp.end(); this.disp.end();
} else {
this.playing = false;
if (this.queue.length > 0) {
this.current = this.queue.shift();
this.playYouTube(this.current.url);
} else {
this.stop();
}
} }
} }

Loading…
Cancel
Save