galactic-bot/structure/client/components/settings/moderation/WordWatcher.js
2021-06-23 11:51:34 +03:00

227 lines
7.5 KiB
JavaScript

const { FilterSetting } = require('../../../../interfaces/');
const { Util } = require('../../../../../util');
const emojis = {
WARN: '<:note:744490065354293258>',
MUTE: '<:muted:853709118988353556>',
KICK: '👢',
BAN: '🔨',
SOFTBAN: '🧹',
DELETE: '<:failure:723595130912637018>'
};
module.exports = class WordWatcher extends FilterSetting {
constructor(client) {
super(client, {
name: 'wordWatcher',
module: 'moderation',
aliases: [],
resolve: 'GUILD',
usage: '<option> <method> <value..>',
examples: [
'wordwatcher off'
],
default: {
wordWatcher: {
channel: null,
words: [],
ignore: [],
bypass: [],
actions: []
}
}
});
}
async handle(message, params) {
const { resolver } = this.client;
const { guild } = message;
const setting = guild._settings[this.index];
let index = null,
langParams = {};
// eslint-disable-next-line prefer-const
let [first, ...rest] = params;
rest = rest.map((param) => Util.sanitiseRegex(param, ['?', '\\', '(', ')', '|', '\\[', '\\]', '.', '-']));
const result = await resolver.resolveMethod([first, ...rest], {
existing: setting.words,
allowedMethods: ['add', 'set', 'remove', 'reset', 'off']
});
const method = first.toLowerCase();
// if (method.on || method.off) return {
// error: true,
// msg: message.format('MISSING_ARGS')
// };
if (['off'].includes(result.method)) {
index = 'S_WORDWATCHER_TOGGLE';
langParams.toggle = message.format('ON_OFF_TOGGLE', { toggle: false }, true);
setting.channel = null;
} else if (['ignore', 'channels', 'channelignore'].includes(method)) {
langParams = await this._processChannelIgnore(params, setting, guild, 'S_WORDWATCHER_IGNORE_');
if (langParams.error) return langParams;
({ index } = langParams);
// result = await resolver.resolveMethod(params, {
// existing: setting.ignore,
// resolver: resolver.resolveChannels.bind(resolver),
// guild,
// allowedMethods: ['add', 'set', 'remove', 'reset']
// });
// if (!result) return {
// error: true,
// msg: message.format('ERR_INVALID_METHOD', {
// method
// })
// };
// index = `S_WORDWATCHER_IGNORE_${result.method.toUpperCase()}`;
// langParams.changes = result.resolved.map((res) => res.name).join('`, `');
} else if (['roles', 'roleignore', 'bypass'].includes(method)) {
langParams = await this._processRoleBypass(params, setting, guild, 'S_WORDWATCHER_BYPASS_');
if (langParams.error) return langParams;
({ index } = langParams);
// result = await resolver.resolveMethod(params, {
// existing: setting.bypass,
// resolver: resolver.resolveRoles.bind(resolver),
// guild,
// allowedMethods: ['add', 'set', 'remove', 'reset']
// });
// if (!result) return {
// error: true,
// msg: message.format('ERR_INVALID_METHOD', {
// method
// })
// };
// index = `S_WORDWATCHER_BYPASS_${result.method.toUpperCase()}`;
// langParams.changes = result.resolved.map((res) => res.name).join('`, `');
} else if (['action', 'actions'].includes(method)) {
params.shift();
langParams = await this._action(message, params, setting, { _wordWatcher: true });
({ index } = langParams);
} else if (result) {
setting.words = result.result;
index = result.changed.length || result.method === 'reset' ? `S_WORDWATCHER_${result.method.toUpperCase()}` : 'S_NO_CHANGE';
langParams.changes = result.changed.join('**, **');
} else {
const channel = await guild.resolveChannel(method);
if (!channel) return {
msg: message.format('ERR_CHANNEL_RESOLVE', { resolveable: method }),
error: true
};
if (channel.type !== 'text') return {
error: true,
msg: message.format('ERR_CHANNEL_TYPE', { type: channel.type })
};
const perms = channel.permissionsFor(guild.me);
const missingPerms = [];
if (!perms.has('SEND_MESSAGES')) missingPerms.push('SEND_MESSAGES');
if (!perms.has('VIEW_CHANNEL')) missingPerms.push('VIEW_CHANNEL');
if (missingPerms.length) return {
error: true,
msg: message.format('ERR_CHANNEL_PERMS', { channel: channel.name, perms: missingPerms.join(', ') })
};
index = 'S_WORDWATCHER_CHANNEL';
langParams.channel = channel.id;
setting.channel = channel.id;
}
if (langParams.error) return {
error: true,
msg: langParams.msg
};
await message.guild._updateSettings({ [this.index]: setting });
return {
error: false,
msg: message.format(index, langParams)
};
}
async fields(guild) {
const setting = guild._settings[this.index];
return [
{
name: '》 Status',
value: guild.format('SETTING_STATUS', { bool: Boolean(setting.channel) }, true),
inline: true
},
{
name: '》 Channel',
value: setting?.channel ? `<#${setting.channel}>` : '`N/A`',
inline: true
},
{
name: '\u200b',
value: '\u200b',
inline: true
},
{
name: '》 Ignored channels',
value: setting?.ignore.length ? setting.ignore.map((id) => `<#${id}>`) : '`N/A`',
inline: true
},
{
name: '》 Role bypass',
value: setting?.bypass.length ? setting.bypass.map((id) => `<@&${id}>`) : '`N/A`',
inline: true
},
{
name: '》 Words',
value: setting.words.join(', ') || '`N/A`',
inline: true
}
];
}
async _createTrigger(message, action, actionObject, setting) {
const response = await message.prompt(message.format('S_WORDWATCHER_ACTION_ADD_TRIGGERS'), { time: 60 * 1000 });
if (!response && !response.content) {
if (setting.actions.find((ac) => ac.trigger === 'generic')) return {
error: true,
msg: message.format('S_FILTER_ACTIONS_EXISTING', { trigger: 'generic' })
};
setting.actions.push(actionObject);
actionObject.trigger = 'generic';
return actionObject;
}
if (['cancel', 'abort', 'exit'].includes(action.toLowerCase())) return {
error: true,
msg: message.format('ERR_CANCEL')
};
const emoji = await this.client.resolver.resolveEmoji(response.content, message.guild);
// WAITING FOR BUTTONS
}
};