<commit_before>/*
 * locales.js - Locale management
 *
 * Contributed by Ovyerus
 */

var exposed;

exports.commands = [
    'locales'
];

exports.locales = {
    desc: 'Manage locales.',
    longDesc: '',
    usage: '[set [guild] <locale>]',
    main(bot, ctx) {
        return new Promise((resolve, reject) => {
            if (ctx.args.length < 2) {
                ctx.msg.channel.createMessage({embed: localeBlock(ctx.settings)}).then(resolve).catch(reject);
            } else if (ctx.args.length === 2 && ctx.args[1] === 'guild') {
                ctx.msg.channel.createMessage({embed: localeBlock(ctx.settings)}).then(resolve).catch(reject);
            } else if (ctx.args.length === 2 && /[a-z]{2}-[A-Z]{2}/i.test(ctx.args[1])) {
                exposed = bot;
                changeLocale(ctx).then(resolve).catch(reject);
            } else if (ctx.args.length === 3 && ctx.args[1] === 'guild' && /[a-z]{2}-[A-Z]{2}/i.test(ctx.args[2])) {
                if (!ctx.msg.member.permission.has('manageGuild')) {
                    ctx.msg.channel.createMessage(localeManager.t('user-noPerm', 'en-UK', {perm: 'Manage guild'})).then(resolve).catch(reject);
                } else {
                    changeLocale(ctx, true).then(resolve).catch(reject);
                }
            } else {
                ctx.msg.channel.createMessage({embed: localeBlock(ctx.settings)}).then(resolve).catch(reject);
            }
        });
    }
};

function localeBlock(settings) {
    let embed = {title: localeManager.t('locales-infoHeader', settings.locale), description: '', fields: []};

    Object.keys(localeManager.locales).forEach(v => embed.description += `\n${v} - **${localeManager.locales[v].locales[v]} [${localeManager.locales[settings.locale].locales[v]}]**`);
    
    embed.description += '\n\u200b';

    embed.fields.push({
        name: localeManager.t('locales-userLocale', settings.locale, {locale: settings.user.locale}),
        value: `${localeManager.t('locales-guildLocale', settings.locale, {locale: settings.guild.locale})}\n\u200b`}
    );

    embed.fields.push({
        name: localeManager.t('exampleUsage', settings.locale),
        value: '`locales set en-UK`\n`locales set guild en-UK`'}
    );

    return embed;
}

function changeLocale(ctx, guild) {
    return new Promise((resolve, reject) => {
        let bot = exposed;
        let real;
        if (!guild) {
            for (let locale in localeManager.locales) {
                if (ctx.args[1].toLowerCase() === locale.toLowerCase()) {
                    real = true;
                    break;
                }
            }

            let userChoice = ctx.args[1].split('-')[0].toLowerCase() + '-' + ctx.args[1].split('-')[1].toUpperCase();
            if (!real) {
                ctx.msg.channel.createMessage(localeManager.t('locales-invalidLocale', ctx.settings.locale, {locale: userChoice})).then(resolve).catch(reject);
            } else {
                bot.setUserSettings(ctx.msg.author.id, {locale: userChoice}).then(() => {
                    return ctx.msg.channel.createMessage(localeManager.t('locales-userLocaleUpdated', ctx.settings.locale, {locale: userChoice}));
                }).then(resolve).catch(reject);
            }
        } else {
            for (let locale in localeManager.locales) {
                if (ctx.args[1].toLowerCase() === locale.toLowerCase()) {
                    real = true;
                    break;
                }
            }

            let userChoice = ctx.args[1].split('-')[0].toLowerCase() + '-' + ctx.args[1].split('-')[1].toUpperCase();
            if (!real) {
                ctx.msg.channel.createMessage(localeManager.t('locales-invalidLocale', ctx.settings.locale, {locale: userChoice})).then(resolve).catch(reject);
            } else {
                bot.setGuildSettings(ctx.msg.channel.guild.id, {locale: userChoice}).then(() => {
                    return ctx.msg.channel.createMessage(localeManager.t('locales-guildLocaleUpdated', ctx.settings.locale, {locale: userChoice}));
                }).then(resolve).catch(reject);
            }
        }
    });
}<commit_msg>Fix issue not allowing guild locale to be set.<commit_after>/*
 * locales.js - Locale management
 *
 * Contributed by Ovyerus
 */

var exposed;

exports.commands = [
    'locales'
];

exports.locales = {
    desc: 'Manage locales.',
    longDesc: '',
    usage: '[set [guild] <locale>]',
    main(bot, ctx) {
        return new Promise((resolve, reject) => {
            if (ctx.args.length < 2) {
                ctx.msg.channel.createMessage({embed: localeBlock(ctx.settings)}).then(resolve).catch(reject);
            } else if (ctx.args.length === 2 && ctx.args[1] === 'guild') {
                ctx.msg.channel.createMessage({embed: localeBlock(ctx.settings)}).then(resolve).catch(reject);
            } else if (ctx.args.length === 2 && /[a-z]{2}-[A-Z]{2}/i.test(ctx.args[1])) {
                exposed = bot;
                changeLocale(ctx).then(resolve).catch(reject);
            } else if (ctx.args.length === 3 && ctx.args[1] === 'guild' && /[a-z]{2}-[A-Z]{2}/i.test(ctx.args[2])) {
                if (!ctx.msg.member.permission.has('manageGuild')) {
                    ctx.msg.channel.createMessage(localeManager.t('user-noPerm', 'en-UK', {perm: 'Manage guild'})).then(resolve).catch(reject);
                } else {
                    exposed = bot;
                    changeLocale(ctx, true).then(resolve).catch(reject);
                }
            } else {
                ctx.msg.channel.createMessage({embed: localeBlock(ctx.settings)}).then(resolve).catch(reject);
            }
        });
    }
};

function localeBlock(settings) {
    let embed = {title: localeManager.t('locales-infoHeader', settings.locale), description: '', fields: []};

    Object.keys(localeManager.locales).forEach(v => embed.description += `\n${v} - **${localeManager.locales[v].locales[v]} [${localeManager.locales[settings.locale].locales[v]}]**`);
    
    embed.description += '\n\u200b';

    embed.fields.push({
        name: localeManager.t('locales-userLocale', settings.locale, {locale: settings.user.locale}),
        value: `${localeManager.t('locales-guildLocale', settings.locale, {locale: settings.guild.locale})}\n\u200b`}
    );

    embed.fields.push({
        name: localeManager.t('exampleUsage', settings.locale),
        value: '`locales set en-UK`\n`locales set guild en-UK`'}
    );

    return embed;
}

function changeLocale(ctx, guild) {
    return new Promise((resolve, reject) => {
        let real;
        if (!guild) {
            for (let locale in localeManager.locales) {
                if (ctx.args[1].toLowerCase() === locale.toLowerCase()) {
                    real = true;
                    break;
                }
            }

            let userChoice = ctx.args[1].split('-')[0].toLowerCase() + '-' + ctx.args[1].split('-')[1].toUpperCase();
            if (!real) {
                ctx.msg.channel.createMessage(localeManager.t('locales-invalidLocale', userChoice, {locale: userChoice})).then(resolve).catch(reject);
            } else {
                let bot = exposed;
                bot.setUserSettings(ctx.msg.author.id, {locale: userChoice}).then(() => {
                    return ctx.msg.channel.createMessage(localeManager.t('locales-userLocaleUpdated', userChoice, {locale: userChoice}));
                }).then(resolve).catch(reject);
            }
        } else {
            for (let locale in localeManager.locales) {
                if (ctx.args[2].toLowerCase() === locale.toLowerCase()) {
                    real = true;
                    break;
                }
            }

            let userChoice = ctx.args[2].split('-')[0].toLowerCase() + '-' + ctx.args[2].split('-')[1].toUpperCase();
            if (!real) {
                ctx.msg.channel.createMessage(localeManager.t('locales-invalidLocale', ctx.settings.locale, {locale: userChoice})).then(resolve).catch(reject);
            } else {
                let bot = exposed;
                bot.setGuildSettings(ctx.msg.channel.guild.id, {locale: userChoice}).then(() => {
                    return ctx.msg.channel.createMessage(localeManager.t('locales-guildLocaleUpdated', userChoice, {locale: userChoice}));
                }).then(resolve).catch(reject);
            }
        }
    });
}
{'fraction_non_alphanumeric': 0.1723109945146673, 'fraction_numerical': 0.00667779632721202, 'mean_word_length': 2.18292220113852, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '1984566', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2461, 'n_words': 381}