'use strict';

const Discord = require('discord.js');
const tokens = require('./tokens.json');
const prefix = tokens.prefix;
const client = new Discord.Client();

const music = require('./modules/music.js');
const gif = require('./modules/gif.js');
const reminder = require('./modules/reminder.js');
const translator = require('./modules/translate.js');
const chat = require('./modules/wit-wrapper.js');
const chatActions = require('./modules/wit-actions.js');

// flags...
let reminderModuleAvailable = false;
let chatModuleAvailable = false;

client.on('ready', () => {
    console.log('Koe: Ready!');

    reminder.init(tokens.firebase).then(
        () => {
            reminderModuleAvailable = true;
            console.log('Koe: Reminder module ready!');
        },
        (err) => {
            reminderModuleAvailable = false;
            console.log('Koe: Reminder module broke...');
        }
    );

    chat.init(tokens.wit, chatActions).then(
        () => {
            chatModuleAvailable = true;
            console.log('Koe: Chat module ready!');
        },
        (err) => {
            chatModuleAvailable = false;
            console.log('Koe: Chat module broke...', err);
        }
    );
});

client.on('message', msg => {
    let stop = msg.author.bot ||
        (msg.client !== client) ||
        !msg.content.startsWith(prefix);

    if (stop) {
        return;
    }

    let message = msg.content;
    let msgSplit = message.slice(prefix.length).split(' ');
    let cmdName = msgSplit[0].toLowerCase();
    let cmdArgs = msgSplit.slice(1).join(' ');

    if (cmdName === 'help') {

        let instructions = [
            '```xl',
            music[cmdName](prefix),
            '\n',
            gif[cmdName](prefix),
            '\n',
            reminder[cmdName](prefix),
            '\n',
            translator[cmdName](prefix),
            '\n',
            '```'
        ].join('\n');

        msg.channel.sendMessage(instructions);

    } else if (music.hasOwnProperty(cmdName)) {

        music[cmdName](msg, cmdArgs);

    } else if (gif.hasOwnProperty(cmdName)) {

        gif[cmdName](cmdArgs).then(
            url => msg.channel.sendMessage(url)
        ).catch(
            e => console.log('Koe: Gif error >', e)
        );

    } else if (reminderModuleAvailable &&
        reminder.hasOwnProperty(cmdName)) {

        reminder[cmdName](
            cmdArgs,
            msg.author.username,
            msg.author.id
        ).then(
            eventDetails => msg.author.sendMessage(eventDetails)
        ).catch(
            e => console.log('Koe: Reminder error >', e)
        );

    } else if (translator.hasOwnProperty(cmdName)) {

        let srcLang = msgSplit[1];
        let targetLang = msgSplit[2];
        let keywords = msgSplit.slice(3);

        translator[cmdName](srcLang, targetLang, keywords).then(
            response => msg.channel.sendMessage(response)
        ).catch(
            e => console.log('Koe: Translation error >', e)
        );

    } else if (chatModuleAvailable &&
        cmdName.toLowerCase() === 'koe') {

        chat.message(msg.author.id, cmdArgs).then(
            response => msg.channel.sendMessage(response)
        ).catch(
            e => console.log('Koe: Chat error >', e)
        );

    }
});

client.login(tokens.d_token);

@@ -1962,24 +1962,718 @@
 ructions);%0A%0A
+    %7D else if (cmdName === 'sleep') %7B%0A%0A        if (msg.author.id == botConfig.adminID) %7B%0A%0A            msg.channel.sendMessage('Bye ...').then(%0A                () =%3E client.destroy()%0A            ).then(%0A                () =%3E process.exit()%0A            ).catch(%0A                e =%3E %7B%0A                    msg.channes.sendMessage('I can%5C't sleep ...');%0A                    console.log(%60$%7BbotName%7D: can't sleep %3E %60, e);%0A                %7D%0A            );%0A%0A        %7D else %7B%0A%0A            msg.channel.sendMessage('Only master can make me sleep!');%0A            console.log(%60$%7BbotName%7D: unauthorized to sleep%60);%0A        %7D%0A%0A        // TODO:%0A        // Reboot: requires a node module like Forever to work.%0A%0A
     %7D else i

Add sleep command