{"size": 1562, "ext": "js", "max_stars_repo_path": "lib/on-event/ready.js", "max_stars_repo_name": "Richardson-Media-House/kirbi-discord-roles", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/on-event/ready.js", "max_issues_repo_name": "Richardson-Media-House/kirbi-discord-roles", "max_issues_repo_issues_event_min_datetime": "2018-01-03T04:39:02.000Z", "max_issues_repo_issues_event_max_datetime": "2018-01-07T17:58:54.000Z", "max_forks_repo_path": "lib/on-event/ready.js", "max_forks_repo_name": "Richardson-Media-House/kirbi-discord-roles", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 26.9310344828, "max_line_length": 109, "alphanum_fraction": 0.6670934699}
const chalk = require('chalk');

const guildAssignableRolesSchema = require('../../classes/guild-assignable-roles-schema');
const guildDefaultRoleSchema = require('../../classes/guild-default-role-schema');

module.exports = Kirbi => {
	const guildAssignableRoles = Kirbi.Database.model('GuildAssignableRolesSchema', guildAssignableRolesSchema);
	const guildDefaultRoles = Kirbi.Database.model('GuildDefaultRoles', guildDefaultRoleSchema);

	Kirbi.Discord.on('ready', () => {
		if (!Kirbi.Config.discord.roles) {
			Kirbi.Config.discord.roles = [];
		}

		guildAssignableRoles.find((err, guildSettings) => {
			if (err) {
				console.log(chalk.red(`Error: ${err}`));
				return false;
			}

			guildSettings.forEach(settings => {
				const guild = Kirbi.Discord.guilds.find('id', settings.guildId);

				if (guild) {
					if (!Kirbi.Config.discord.roles[settings.guildId]) {
						Kirbi.Config.discord.roles[settings.guildId] = {};
					}

					Kirbi.Config.discord.roles[settings.guildId].assignable = settings.roleIds;
				}
			});

			return true;
		});

		guildDefaultRoles.find((err, guildSettings) => {
			if (err) {
				console.log(chalk.red(`Error: ${err}`));
				return false;
			}

			guildSettings.forEach(settings => {
				const guild = Kirbi.Discord.guilds.find('id', settings.guildId);

				if (guild) {
					if (!Kirbi.Config.discord.roles[settings.guildId]) {
						Kirbi.Config.discord.roles[settings.guildId] = {};
					}

					Kirbi.Config.discord.roles[settings.guildId].default = settings.roleId;
				}
			});

			return true;
		});
	});
};
