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

115 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-07-11 22:33:01 +02:00
const { Setting } = require('../../../../interfaces/');
class StickyRoleSetting extends Setting {
constructor(client) {
super(client, {
name: 'stickyrole',
module: 'utility',
aliases: [
'sticky',
'stickyroles'
2020-07-11 22:33:01 +02:00
],
usage: '<value>',
examples: [
'stickyrole on',
'stickyrole add moderator member'
2020-07-11 22:33:01 +02:00
],
guarded: false,
resolve: 'GUILD',
default: {
stickyrole: {
roles: []
2020-07-11 22:33:01 +02:00
}
},
custom: true,
premium: 1
});
}
async handle(message, params) {
const { guild } = message;
const setting = guild._settings[this.index];
const { resolver } = this.client;
const langParams = {};
let index = null;
const response = await resolver.resolveMethod(params, null, setting.roles, {
resolver: resolver.resolveRoles.bind(resolver),
guild
});
if (!response) return {
error: true,
msg: message.format('ERR_INVALID_METHOD', { method: params[0] })
};
const { method } = response;
if (method === 'on') {
setting.roles = guild.roles.cache.map((r) => r.id);
index = 'S_STICKYROLE_TOGGLE';
langParams.toggle = message.format('ON_OFF_TOGGLE', { toggle: true }, true);
} else if (method === 'off') {
setting.roles = [];
2020-07-11 22:33:01 +02:00
index = 'S_STICKYROLE_TOGGLE';
langParams.toggle = message.format('ON_OFF_TOGGLE', { toggle: false }, true);
} else if (method === 'add') {
const roles = response.resolved;
setting.roles = response.result;
index = 'S_STICKYROLE_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;
index = 'S_STICKYROLE_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 === 'set') {
const roles = response.resolved;
setting.roles = roles.map((r) => r.id);
index = 'S_STICKYROLE_SET';
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_STICKYROLE_LIST';
langParams.list = response.resolved.map((r) => r.name).join(', ');
2020-07-11 22:33:01 +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] || this.default[this.index];
const roles = setting.roles ? await this.client.resolver.resolveRoles(setting.roles) : null;
2020-07-11 22:33:01 +02:00
return [
{
name: '》 Roles',
value: roles ? roles.map((r) => r.name) : 'N/A',
2020-07-11 22:33:01 +02:00
inline: false
}
];
}
}
module.exports = StickyRoleSetting;