'use strict';

const
    config = require('config'),
    BaseModule = require('./base_module');

class RegionAssignment extends BaseModule {
    constructor(bot, config) {
        super(bot, config);
        this.name = 'Region Assignment';
    }

    assignRegionRole(member, region) {
        const role = member.guild.roles.find('name', config.modules.region_assignment[region].role);
        return member.addRole(role)
            .then(() => `You should have been **assigned** to **${role.name}**. If not, please contact one of the staff members.`);
    }

    removeRegionRole(member, region) {
        const role = member.guild.roles.find('name', config.modules.region_assignment[region].role);
        return member.removeRole(role)
            .then(() => `You should have been **removed** from **${role.name}**. If not, please contact one of the staff members.`);
    }

    cmd_assignEU() {
        return {
            id: config.modules.region_assignment.eu.command_assign,
            channel_type: 'text',
            channels: config.modules.region_assignment.channels,
            on_command: message => {
                if (!config.modules.region_assignment.eu.enabled) {
                    return "I'm sorry, but this region is currently disabled.";
                }
                return this.assignRegionRole(message.member, 'eu');
            }
        };
    }

    cmd_assignNA() {
        return {
            id: config.modules.region_assignment.na.command_assign,
            channel_type: 'text',
            channels: config.modules.region_assignment.channels,
            on_command: message => {
                if (!config.modules.region_assignment.na.enabled) {
                    return "I'm sorry, but this region is currently disabled.";
                }
                return this.assignRegionRole(message.member, 'na');
            }
        };
    }

    cmd_assignCN() {
        return {
            id: config.modules.region_assignment.cn.command_assign,
            channel_type: 'text',
            channels: config.modules.region_assignment.channels,
            on_command: message => {
                if (!config.modules.region_assignment.cn.enabled) {
                    return "I'm sorry, but this region is currently disabled.";
                }
                return this.assignRegionRole(message.member, 'cn');
            }
        };
    }

    cmd_removeEU() {
        return {
            id: config.modules.region_assignment.eu.command_remove,
            channel_type: 'text',
            channels: config.modules.region_assignment.channels,
            on_command: message => {
                if (!config.modules.region_assignment.eu.enabled) {
                    return "I'm sorry, but this region is currently disabled.";
                }
                return this.removeRegionRole(message.member, 'eu');
            }
        };
    }

    cmd_removeNA() {
        return {
            id: config.modules.region_assignment.na.command_remove,
            channel_type: 'text',
            channels: config.modules.region_assignment.channels,
            on_command: message => {
                if (!config.modules.region_assignment.na.enabled) {
                    return "I'm sorry, but this region is currently disabled.";
                }
                return this.removeRegionRole(message.member, 'na');
            }
        };
    }

    cmd_removeCN() {
        return {
            id: config.modules.region_assignment.cn.command_remove,
            channel_type: 'text',
            channels: config.modules.region_assignment.channels,
            on_command: message => {
                if (!config.modules.region_assignment.cn.enabled) {
                    return "I'm sorry, but this region is currently disabled.";
                }
                return this.removeRegionRole(message.member, 'cn');
            }
        };
    }
}

module.exports = RegionAssignment;

@@ -249,42 +249,319 @@
     
-assignRegionRole(member, region) %7B
+checkEnabled(region) %7B%0A        return config.modules.region_assignment%5Bregion%5D && config.modules.region_assignment%5Bregion%5D.enabled;%0A    %7D%0A%0A    assignRegionRole(member, region) %7B%0A        if (!this.checkEnabled(region)) %7B%0A            return %60Region $%7Bregion.toUpperCase()%7D is currently not available.%60;%0A        %7D%0A
 %0A   
@@ -864,32 +864,166 @@
 mber, region) %7B%0A
+        if (!this.checkEnabled(region)) %7B%0A            return %60Region $%7Bregion.toUpperCase()%7D is currently not available.%60;%0A        %7D%0A%0A
         const ro
@@ -1532,174 +1532,8 @@
 %3E %7B%0A
-                if (!config.modules.region_assignment.eu.enabled) %7B%0A                    return %22I'm sorry, but this region is currently disabled.%22;%0A                %7D%0A
     
@@ -1874,174 +1874,8 @@
 %3E %7B%0A
-                if (!config.modules.region_assignment.na.enabled) %7B%0A                    return %22I'm sorry, but this region is currently disabled.%22;%0A                %7D%0A
     
@@ -2216,174 +2216,8 @@
 %3E %7B%0A
-                if (!config.modules.region_assignment.cn.enabled) %7B%0A                    return %22I'm sorry, but this region is currently disabled.%22;%0A                %7D%0A
     
@@ -2558,174 +2558,8 @@
 %3E %7B%0A
-                if (!config.modules.region_assignment.eu.enabled) %7B%0A                    return %22I'm sorry, but this region is currently disabled.%22;%0A                %7D%0A
     
@@ -2900,174 +2900,8 @@
 %3E %7B%0A
-                if (!config.modules.region_assignment.na.enabled) %7B%0A                    return %22I'm sorry, but this region is currently disabled.%22;%0A                %7D%0A
     
@@ -3193,32 +3193,32 @@
 nment.channels,%0A
+
             on_c
@@ -3242,174 +3242,8 @@
 %3E %7B%0A
-                if (!config.modules.region_assignment.cn.enabled) %7B%0A                    return %22I'm sorry, but this region is currently disabled.%22;%0A                %7D%0A
     

Generalize region enabled checking