const { Setting } = require('../../../../interfaces/'); class ProtectionSetting extends Setting { constructor(client) { super(client, { name: 'protection', module: 'moderation', aliases: [ ], usage: ' [value..]', examples: [ 'protection type ', 'protection add ', 'protection remove ' ], guarded: false, resolve: 'GUILD', default: { protection: { type: 'position', roles: [] } }, custom: true }); } async handle(message, params) { const { guild } = message; const { resolver } = this.client; const setting = guild._settings[this.index]; let method = params[0].toLowerCase(), index = null; const langParams = {}; if (method === 'type') { if (!params[1]) return message.format('MISSING_ARGS'); const value = params[1].toLowerCase(); if (!['role', 'position'].includes(value)) return message.format('S_PROTECTION_INVALID_ARGS'); setting.type = value; index = 'S_PROTECTION_TYPE'; langParams.type = value; } else { const response = await resolver.resolveMethod(params, null, setting.roles, resolver.resolveRoles.bind(resolver), guild); if (!response) return message.format('INVALID_METHOD', { method }); ({ method } = response); if (method === 'add') { setting.roles = response.result; setting.enabled = true; index = 'S_PROTECTION_ADD'; langParams.changes = response.resolved.filter((r) => response.changed.includes(r.id)).map((r) => r.name).join('`, `'); } else if (method === 'remove') { setting.roles = response.result; setting.enabled = setting.roles.length && true; index = 'S_PROTECTION_REMOVE'; langParams.changes = response.resolved.filter((r) => response.changed.includes(r.id)).map((r) => r.name).join('`, `'); } else if (method === 'set') { setting.roles = response.result; setting.enabled = true; index = 'S_PROTECTION_SET'; langParams.changes = response.resolved.filter((r) => response.changed.includes(r.id)).map((r) => r.name).join('`, `'); } else if (method === 'list') { index = 'S_AUTOROLE_LIST'; langParams.list = response.resolved.map((r) => r.name).join(', '); } } await guild._updateSettings({ [this.index]: setting }); return { error: false, msg: message.format(index, langParams) }; } async fields(guild) { const setting = guild._settings[this.index]; const roles = setting?.roles ? await Promise.all(setting.roles.map(async (role) => guild.resolveRole(role))) : undefined; return [ { name: '》Type', value: setting.type, inline: true }, { name: '》Roles', value: roles?.map((r) => r.name).join(', ') || 'N/A', inline: false } ]; } } module.exports = ProtectionSetting;