autorole setting

This commit is contained in:
Erik 2020-06-10 17:49:07 +03:00
parent 472d3334f9
commit 883a1de725

View File

@ -0,0 +1,115 @@
const { Setting } = require('../../../../interfaces/');
class AutoroleSetting extends Setting {
constructor(client) {
super(client, {
name: 'autorole',
module: 'utility',
aliases: [
],
usage: '<method|value> [value..]',
examples: [
'autorole add role',
'autorole remove role'
],
guarded: false,
resolve: 'GUILD',
default: {
autorole: {
roles: [],
enabled: false
}
},
custom: true
});
this.client = client;
}
async handle(message, params) {
const setting = message.guild._settings[this.index] || this.default[this.index];
const { guild } = message;
const { resolver } = this.client;
const response = await resolver.resolveMethod(params, null, setting.roles, resolver.resolveRoles.bind(resolver), guild);
let index = null,
langParams = {};
if (!response) return {
error: true,
msg: message.format('ERR_INVALID_METHOD', { method: params[0] })
};
const { method } = response;
//console.log(response);
if (method === 'add') {
const roles = response.resolved;
setting.roles = response.result;
setting.enabled = true;
index = 'S_AUTOROLE_ADD';
const changed = roles.filter(r => response.changed.includes(r.id)).map(r => r.name).join('`, `');
langParams.changes = changed.length ? changed : 'N/A';
} else if (method === 'remove') {
const roles = response.resolved;
setting.roles = response.result;
setting.enabled = setting.roles.length && true;
index = 'S_AUTOROLE_REMOVE';
const changed = roles.filter(r => response.changed.includes(r.id)).map(r => r.name).join('`, `');
langParams.changes = changed.length ? changed : 'N/A';
} else if (method === 'list') {
index = 'S_AUTOROLE_LIST';
langParams.list = response.resolved.map(r => r.name).join(', ');
} else if (method === 'set') {
const roles = response.resolved;
setting.roles = roles.map(r => r.id);
setting.enabled = roles.length && true;
index = 'S_AUTOROLE_SET';
langParams.changes = roles.map(r => r.name).join('`, `');
}
await message.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: '》Enabled',
value: setting.enabled,
inline: false
}, {
name: '》Roles',
value: roles?.map(r => r.name).join(', ') || 'N/A',
inline: false
}
];
}
}
module.exports = AutoroleSetting;