galactic-bot/structure/client/components/settings/moderation/WordWatcher.js

182 lines
6.0 KiB
JavaScript
Raw Normal View History

2020-11-11 23:58:13 +01:00
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,
langParams = {};
2020-11-11 23:58:13 +01:00
2021-05-10 21:20:29 +02:00
const result = await resolver.resolveMethod(params.map((param) => Util.sanitiseRegex(param)), {
2020-11-11 23:58:13 +01:00
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)) {
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
// })
// };
2020-11-11 23:58:13 +01:00
// index = `S_WORDWATCHER_IGNORE_${result.method.toUpperCase()}`;
// langParams.changes = result.resolved.map((res) => res.name).join('`, `');
2020-11-11 23:58:13 +01:00
} 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('`, `');
2020-11-11 23:58:13 +01:00
} else if (result) {
setting.words = result.result;
2021-05-10 14:45:55 +02:00
index = result.changed.length || result.method === 'reset' ? `S_WORDWATCHER_${result.method.toUpperCase()}` : 'S_NO_CHANGE';
2020-11-11 23:58:13 +01:00
langParams.changes = result.changed.join('**, **');
} else {
2020-11-13 20:14:13 +01:00
const channel = await guild.resolveChannel(method);
2020-11-11 23:58:13 +01:00
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
}
];
}
};