galactic-bot/structure/client/components/settings/utility/Autorole.js

116 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-06-10 16:49:07 +02:00
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
});
}
async handle(message, params) {
2020-07-11 22:33:44 +02:00
const setting = message.guild._settings[this.index];
2020-06-10 16:49:07 +02:00
const { guild } = message;
const { resolver } = this.client;
2020-09-30 18:23:36 +02:00
const response = await resolver.resolveMethod(params, {
existing: setting.roles,
resolver: resolver.resolveRoles.bind(resolver),
guild
});
2020-06-10 16:49:07 +02:00
2020-09-30 18:23:36 +02:00
let index = null;
const langParams = {};
2020-06-10 16:49:07 +02:00
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('`, `');
2020-06-10 16:49:07 +02:00
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('`, `');
2020-06-10 16:49:07 +02:00
langParams.changes = changed.length ? changed : 'N/A';
} else if (method === 'list') {
index = 'S_AUTOROLE_LIST';
langParams.list = response.resolved.map((r) => r.name).join(', ');
2020-06-10 16:49:07 +02:00
} else if (method === 'set') {
const roles = response.resolved;
setting.roles = roles.map((r) => r.id);
2020-06-10 16:49:07 +02:00
setting.enabled = roles.length && true;
index = 'S_AUTOROLE_SET';
langParams.changes = roles.map((r) => r.name).join('`, `');
2020-06-10 16:49:07 +02:00
}
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',
2020-06-10 16:49:07 +02:00
value: setting.enabled,
inline: false
}, {
name: '》 Roles',
value: roles?.map((r) => r.name).join(', ') || 'N/A',
2020-06-10 16:49:07 +02:00
inline: false
}
];
}
}
module.exports = AutoroleSetting;