const Subiex = require('../bot.js')
const bot = Subiex.bot
const config = Subiex.config
const commands = Subiex.commands

bot.on('message', (message) => {
    let cmd = null
    let cmdType = null
    if (message.author.id === config.botID) {
        //  This is the bot speaking
    } else if (message.author.bot) {
        //  This is the bot speaking
    } else if (message.content.indexOf(config.prefix, 0) !== 0) {
        // This is not a command (no prefix)
    } else {
        let userCommand = message.content.split(' ')[0].replace('!', '').toLowerCase()
        for (let loopCmdType in commands) {
            for (let loopCmd in commands[loopCmdType]) {
                if (userCommand.valueOf() === (loopCmd).valueOf()) {
                    message.content = message.content.replace(config.prefix + userCommand, '')
                    cmd = loopCmd
                    cmdType = loopCmdType
                    break
                } else {
                    let aliases = commands[loopCmdType][loopCmd].aliases
                    for (let i = 0; i < aliases.length; i++) {
                        let alias = aliases[i]
                        if (userCommand.valueOf() === alias.valueOf()) {
                            message.content = message.content.replace(config.prefix + userCommand, '')
                            cmd = loopCmd
                            cmdType = loopCmdType
                            break
                        }
                    }
                }
            }
        }
        message.content = message.content.indexOf(' ') === 0 ? message.content.substring(1) : message.content
        if (cmd !== null) {
            if (cmdType === 'master') {
                if (!(config.masters).includes(message.author.id)) {
                    message.reply("You don't have enough permissions to execute this command.")
                    return
                }
            } else if (cmdType === 'moderator') {
                let accepted = ['Subiex Manager', 'Administrator']
                let isMod
                let roles = message.guild.member(message.author.id).roles.array()
                for (let i = 0; i < roles.length; i++) {
                    if (accepted.includes(roles[i].name)) {
                        isMod = true
                        break
                    }
                }
                if (!isMod) {
                    message.reply("You don't have enough permissions to execute this command.")
                    return
                }
            } else if (cmdType === 'dm') {
                if (message.channel.type === 'dm') {
                    console.log(message.author.username + ' - ' + message.content)
                } else {
                    return
                }
            }
            try {
                var result = commands[cmdType][cmd].process(message, bot)
            } catch (error) {
                console.log('An Error occured: ' + error.stack)
            }
            if (result) {
                if (cmdType === 'dm') {
                    message.author.send(result)
                } else {
                    message.reply(result)
                }
            }
        }
    }
})
@@ -2028,16 +2028,31 @@
 strator'
+, 'Team Member'
 %5D%0A      

Add permission for role 'Team Member'