<commit_before>/**
 * Created by julia on 07.11.2016.
 */
let Command = require('../../Objects/command');
let PermManager = require('../../modules/permissionManager');
let minimist = require('minimist');
let discordReg = /<?(#|@|@&)[0-9]+>/g;
let Selector = require('../../modules/selector');
class AddPermission extends Command {
    constructor(t) {
        super();
        this.cmd = "ap";
        this.cat = "permission";
        this.needGuild = true;
        this.t = t;
        this.accessLevel = 0;
        this.p = new PermManager();
    }

    run(msg) {
        let messageSplit = msg.content.split(' ').splice(1);
        let args = minimist(messageSplit);
        this.parseArgs(args, (err, args) => {
            if (err) return msg.channel.createMessage(err);
            // console.log(args);
            // msg.channel.sendCode('JSON', JSON.stringify(args));
            if (args.r) {
                return this.role(msg, args);
            }
            if (args.c) {
                return this.channel(msg, args);
            }
            if (args.u) {
                return this.user(msg, args);
            }
            this.guild(msg, args);
        });
    }

    addPermission(msg, perm) {
        // console.log(perm);
        this.p.addPermission(msg.guild.id, perm, (err) => {
            if (err) return msg.channel.createMessage(this.t('generic.error', {lngs: msg.lang}));
            msg.channel.createMessage(`Ok, the ${perm.type} now has the permission \`${perm.cat}.${perm.perm}\`set to ${perm.use}`);
        })
    }

    guild(msg, args) {
        let perm = this.p.createPermission(args.node, "guild", msg.guild.id, args.allow);
        this.addPermission(msg, perm);
    }

    user(msg, args) {
        let user;
        if (discordReg.test(args.u)) {
            user = msg.mentions[0];
            if (user) {
                let perm = this.p.createPermission(args.node, "user", user.id, args.allow);
                this.addPermission(msg, perm);
            } else {
                msg.channel.createMessage('NOPE user');
            }
        } else {
            let regex = new RegExp(`${args.u}.*`, 'gi');
            let users = msg.guild.members.filter(u => {
                return regex.test(u.user.username)
            });
            if (users.length > 1) {
                let collector = new Selector(msg, users, this.t, (err, number) => {
                    if (err) return msg.channel.createMessage(err);
                    user = users[number - 1];
                    let perm = this.p.createPermission(args.node, "user", user.id, args.allow);
                    this.addPermission(msg, perm);
                });
            } else {
                if (users.length === 1) {
                    user = users[0];
                    let perm = this.p.createPermission(args.node, "user", user.id, args.allow);
                    this.addPermission(msg, perm);
                } else {
                    return msg.channel.createMessage(`No user with name ${args.u} found!`);
                }
            }
        }
    }

    role(msg, args) {
        let role;
        if (discordReg.test(args.r)) {
            role = msg.roleMentions[0];
            if (role) {
                let perm = this.p.createPermission(args.node, "role", role, args.allow);
                // msg.channel.createMessage('```json\n' + JSON.stringify(perm) + '```');
            } else {
                msg.channel.createMessage('NOPE Role');
            }
        } else {
            let regex = new RegExp(`${args.r}.*`, 'gi');
            let roles = msg.guild.roles.filter(r => regex.test(r.name));<commit_msg>hotfixed a bug in ap<commit_after>/**
 * Created by julia on 07.11.2016.
 */
let Command = require('../../Objects/command');
let PermManager = require('../../modules/permissionManager');
let minimist = require('minimist');
let discordReg = /<?(#|@|@&)[0-9]+>/g;
let Selector = require('../../modules/selector');
class AddPermission extends Command {
    constructor(t) {
        super();
        this.cmd = "ap";
        this.cat = "permission";
        this.needGuild = true;
        this.t = t;
        this.accessLevel = 0;
        this.p = new PermManager();
    }

    run(msg) {
        let messageSplit = msg.content.split(' ').splice(1);
        let args = minimist(messageSplit);
        this.parseArgs(args, (err, args) => {
            if (err) return msg.channel.createMessage(err);
            // console.log(args);
            // msg.channel.sendCode('JSON', JSON.stringify(args));
            if (args.r) {
                return this.role(msg, args);
            }
            if (args.c) {
                return this.channel(msg, args);
            }
            if (args.u) {
                return this.user(msg, args);
            }
            this.guild(msg, args);
        });
    }

    addPermission(msg, perm) {
        // console.log(perm);
        this.p.addPermission(msg.guild.id, perm, (err) => {
            if (err) return msg.channel.createMessage(this.t('generic.error', {lngs: msg.lang}));
            msg.channel.createMessage(`Ok, the ${perm.type} now has the permission \`${perm.cat}.${perm.perm}\`set to ${perm.use}`);
        })
    }

    guild(msg, args) {
        let perm = this.p.createPermission(args.node, "guild", msg.guild.id, args.allow);
        this.addPermission(msg, perm);
    }

    user(msg, args) {
        let user;
        if (discordReg.test(args.u)) {
            user = msg.mentions[0];
            if (user) {
                let perm = this.p.createPermission(args.node, "user", user.id, args.allow);
                this.addPermission(msg, perm);
            } else {
                msg.channel.createMessage('NOPE user');
            }
        } else {
            let regex = new RegExp(`${args.u}.*`, 'gi');
            let users = msg.guild.members.filter(u => {
                return regex.test(u.user.username)
            });
            if (users.length > 1) {
                let collector = new Selector(msg, users, this.t, (err, number) => {
                    if (err) return msg.channel.createMessage(this.t(err, {lngs: msg.lang}));
                    user = users[number - 1];
                    let perm = this.p.createPermission(args.node, "user", user.id, args.allow);
                    this.addPermission(msg, perm);
                });
            } else {
                if (users.length === 1) {
                    user = users[0];
                    let perm = this.p.createPermission(args.node, "user", user.id, args.allow);
                    this.addPermission(msg, perm);
                } else {
                    return msg.channel.createMessage(`No user with name ${args.u} found!`);
                }
            }
        }
    }

    role(msg, args) {
        let role;
        if (discordReg.test(args.r)) {
            role = msg.roleMentions[0];
            if (role) {
                let perm = this.p.createPermission(args.node, "role", role, args.allow);
                // msg.channel.createMessage('```json\n' + JSON.stringify(perm) + '```');
            } else {
                msg.channel.createMessage('NOPE Role');
            }
        } else {
            let regex = new RegExp(`${args.r}.*`, 'gi');
            let roles = msg.guild.roles.filter(r => regex.test(r.name));
{'fraction_non_alphanumeric': 0.15737349070682405, 'fraction_numerical': 0.004884004884004884, 'mean_word_length': 1.5749214111072303, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4300162', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2149, 'n_words': 441}