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

199 lines
6.7 KiB
JavaScript
Raw Normal View History

2020-05-24 00:11:06 +02:00
const { Setting } = require('../../../../interfaces/');
2020-05-24 10:54:33 +02:00
class MessageLogsSetting extends Setting {
2020-05-24 00:11:06 +02:00
constructor(client) {
super(client, {
2020-05-24 10:54:33 +02:00
name: 'messageLog',
2020-05-24 00:11:06 +02:00
module: 'moderation',
index: 'chatlogs',
aliases: [
'chatLog',
'chatLogs',
2020-05-24 10:54:33 +02:00
'msgLogs',
'messageLogs',
'msgLog'
2020-05-24 00:11:06 +02:00
],
guarded: true,
resolve: 'GUILD',
examples: [
2020-05-24 10:54:33 +02:00
'msglog roles <add|remove|list> <role>',
'msglog channels <add|remove|list> <role>',
'msglog reset',
'msglog attachments <on|off>',
'msglog #channel'
2020-05-24 00:11:06 +02:00
],
default: {
2020-05-24 10:54:33 +02:00
messageLog: {
channel: null,
2020-05-24 00:11:06 +02:00
ignoredChannels: [],
ignoredRoles: [],
attachments: false
}
}
});
this.client = client;
}
async handle(message, params) {
// eslint-disable-next-line init-declarations
let index, changes, action;
// eslint-disable-next-line prefer-const
let [method, ...args] = params;
method = method.toLowerCase();
2020-05-24 10:54:33 +02:00
const setting = message.guild._settings[this.index];
2020-05-24 00:11:06 +02:00
const { guild } = message;
2020-05-24 10:54:33 +02:00
if (['roles', 'role', 'ignoredrole', 'ignoredroles'].includes(method)) {
2020-05-24 00:11:06 +02:00
const response = this.resolveMethod(args);
if (response) {
if (response.method === 'add') {
const roles = await guild.resolveRoles(response.rest);
setting.ignoredRoles = [...setting.ignoredRoles, ...roles.filter(r => !setting.ignoredRoles.includes(r.id)).map(r => r.id)];
action = 'GENERAL_ADDED';
2020-05-24 10:54:33 +02:00
index = 'S_MESSAGELOG_ROLES';
2020-05-24 00:11:06 +02:00
changes = roles.map(r => r.name);
} else if (response.method === 'remove') {
const roles = await guild.resolveRoles(response.rest);
const _roles = roles.map(r => r.id);
setting.ignoredRoles = setting.ignoredRoles.filter(r => !_roles.includes(r));
action = 'GENERAL_REMOVED';
2020-05-24 10:54:33 +02:00
index = 'S_MESSAGELOG_ROLES';
2020-05-24 00:11:06 +02:00
changes = roles.map(r => r.name);
} else if (response.method === 'list') {
const roles = await guild.resolveRoles(setting.ignoredRoles);
return {
2020-05-24 10:54:33 +02:00
msg: message.format('S_MESSAGELOG_ROLES_LIST', { roles: roles.map(r => r.name).join(', ') })
2020-05-24 00:11:06 +02:00
};
}
} else {
return {
msg: message.format('ERR_INVALID_METHOD', { method })
};
}
2020-05-24 10:54:33 +02:00
} else if (['channels', 'channel', 'ignoredchannels', 'ignoredchannel'].includes(method)) {
2020-05-24 00:11:06 +02:00
const response = this.resolveMethod(args);
if (response) {
if (response.method === 'add') {
const channels = guild.resolveChannels(response.rest);
setting.ignoredChannels = [...setting.ignoredChannels, ...channels.filter(c => !setting.ignoredChannels.includes(c.id)).map(c => c.id)];
action = 'GENERAL_ADDED';
2020-05-24 10:54:33 +02:00
index = 'S_MESSAGELOG_CHANNELS';
2020-05-24 00:11:06 +02:00
changes = channels.map(c => c.name);
} else if (response.method === 'remove') {
const channels = guild.resolveChannels(response.rest);
const _channels = channels.map(c => c.id);
setting.ignoredChannels = setting.ignoredChannels.filter(c => !_channels.includes(c));
action = 'GENERAL_REMOVED';
2020-05-24 10:54:33 +02:00
index = 'S_MESSAGELOG_CHANNELS';
2020-05-24 00:11:06 +02:00
changes = channels.map(c => c.name);
} else if (response.method === 'list') {
return {
2020-05-24 10:54:33 +02:00
msg: message.format('S_MESSAGELOG_LIST')
2020-05-24 00:11:06 +02:00
};
}
changes = response.changed;
} else {
const channels = guild.resolveChannels(setting.ignoredChannels);
return {
2020-05-24 10:54:33 +02:00
msg: message.format('S_MESSAGELOG_ROLES_LIST', { roles: channels.map(r => r.name).join(', ') })
2020-05-24 00:11:06 +02:00
};
}
2020-05-24 10:54:33 +02:00
} else if (['attachments', 'images', 'attachment', 'image'].includes(method)) {
2020-05-24 00:11:06 +02:00
if (guild.premium < 2) return {
msg: message.format('PREMIUM_2', { tier: guild.premium }),
error: true
}
const [bool] = args;
const result = this.client.resolver.resolveBoolean(bool);
if (result) {
setting.attachments = true;
2020-05-24 10:54:33 +02:00
index = 'S_MESSAGELOG_ATTACHMENTS';
changes = message.format('S_MESSAGELOG_TOGGLE', { toggle: true }, true);
2020-05-24 00:11:06 +02:00
} else {
setting.attachments = false;
2020-05-24 10:54:33 +02:00
index = 'S_MESSAGELOG_ATTACHMENTS';
changes = message.format('S_MESSAGELOG_TOGGLE', { toggle: false }, true);
2020-05-24 00:11:06 +02:00
}
} else {
const channel = message.guild.resolveChannel(args[0]);
if (!channel) return {
2020-05-24 10:54:33 +02:00
msg: message.format('S_MESSAGELOG_CHANNEL404'),
2020-05-24 00:11:06 +02:00
error: true
};
setting.channel = channel.id;
}
await message.guild._updateSettings({ [this.index]: setting });
return {
msg: message.format(index, { changed: changes instanceof Array ? changes?.join(', ') : changes || undefined, action: message.format(action) })
};
}
async fields(guild) {
const roles = guild._settings[this.index]?.ignoredRoles ? await Promise.all(guild._settings[this.index].ignoredRoles.map(async (role) => guild.resolveRole(role))) : undefined;
return [
{
2020-05-24 10:54:33 +02:00
name: '》Channel',
value: guild.resolveChannel(guild._settings[this.index]?.channel) || '`N/A`',
2020-05-24 00:11:06 +02:00
inline: true
},
{
2020-05-24 10:54:33 +02:00
name: '》Ignored Roles',
value: roles?.map((r) => r.name).join(', ') || '`N/A`',
2020-05-24 00:11:06 +02:00
inline: true
},
{
2020-05-24 10:54:33 +02:00
name: '》Ignored Channels',
value: guild._settings[this.index]?.ignoredChannels.map((c) => guild.resolveChannel(c).name).join(', ') || '`N/A`',
2020-05-24 00:11:06 +02:00
inline: true
}
];
}
}
2020-05-24 10:54:33 +02:00
module.exports = MessageLogsSetting;