galactic-bot/structure/client/components/settings/moderation/WordWatcher.js
2020-11-12 00:58:13 +02:00

174 lines
5.5 KiB
JavaScript

const { Setting } = require('../../../../interfaces/');
const { Util } = require('../../../../../util');
module.exports = class WordWatcher extends Setting {
constructor(client) {
super(client, {
name: 'wordWatcher',
module: 'moderation',
aliases: [],
resolve: 'GUILD',
usage: '<option> <method> <value..>',
examples: [
'wordfilter off'
],
default: {
wordWatcher: {
channel: null,
words: [],
ignore: [],
bypass: []
}
}
});
}
async handle(message, params) {
const { resolver } = this.client;
const { guild } = message;
const setting = guild._settings[this.index];
let index = null;
const langParams = {};
let result = await resolver.resolveMethod(params.map((param) => Util.sanitiseRegex(param)), {
existing: setting.words,
allowedMethods: ['add', 'set', 'remove', 'reset', 'off']
});
const method = params.shift().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)) {
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)) {
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 (result) {
setting.words = result.result;
index = result.changed.length ? `S_WORDWATCHER_${result.method.toUpperCase()}` : 'S_NO_CHANGE';
langParams.changes = result.changed.join('**, **');
} else {
const channel = await guild.resolveChannel(params[0]);
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;
}
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
}
];
}
};