galactic-bot/structure/client/components/settings/moderation/Chatlogs.js
2020-05-24 01:11:06 +03:00

197 lines
6.5 KiB
JavaScript

const { Setting } = require('../../../../interfaces/');
class Chatlogs extends Setting {
constructor(client) {
super(client, {
name: 'messageLogs',
module: 'moderation',
index: 'chatlogs',
aliases: [
'chatLog',
'chatLogs',
'msgLogs'
],
guarded: true,
resolve: 'GUILD',
examples: [
'chatlogs roles <add|remove|list> <role>',
'chatlogs channels <add|remove|list> <role>',
'chatlogs reset',
'chatlogs attachments <on|off>',
'chatlogs #channel'
],
default: {
chatlogs: {
channel: undefined,
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();
const setting = message.guild._settings.chatlogs || this.default.chatlogs;
const { guild } = message;
if (method === 'roles') {
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';
index = 'S_CHATLOGS_ROLES';
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';
index = 'S_CHATLOGS_ROLES';
changes = roles.map(r => r.name);
} else if (response.method === 'list') {
const roles = await guild.resolveRoles(setting.ignoredRoles);
return {
msg: message.format('S_CHATLOGS_ROLES_LIST', { roles: roles.map(r => r.name).join(', ') })
};
}
} else {
return {
msg: message.format('ERR_INVALID_METHOD', { method })
};
}
} else if (method === 'channels') {
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';
index = 'S_CHATLOGS_CHANNELS';
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';
index = 'S_CHATLOGS_CHANNELS';
changes = channels.map(c => c.name);
} else if (response.method === 'list') {
return {
msg: message.format('S_CHATLOGS_LIST')
};
}
changes = response.changed;
} else {
const channels = guild.resolveChannels(setting.ignoredChannels);
return {
msg: message.format('S_CHATLOGS_ROLES_LIST', { roles: channels.map(r => r.name).join(', ') })
};
}
} else if (method === 'attachments') {
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;
index = 'S_CHATLOGS_ATTACHMENTS';
changes = message.format('S_CHATLOGS_TOGGLE', { toggle: true }, true);
} else {
setting.attachments = false;
index = 'S_CHATLOGS_ATTACHMENTS';
changes = message.format('S_CHATLOGS_TOGGLE', { toggle: false }, true);
}
} else {
const channel = message.guild.resolveChannel(args[0]);
if (!channel) return {
msg: message.format('S_CHATLOGS_CHANNEL404'),
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 [
{
name: 'Channel',
value: guild.resolveChannel(guild._settings[this.index]?.channel) || 'N/A',
inline: true
},
{
name: 'Ignored roles',
value: roles?.map((r) => r.name).join(', ') || 'N/A',
inline: true
},
{
name: 'Ignored channels',
value: guild._settings[this.index]?.ignoredChannels.map((c) => guild.resolveChannel(c).name).join(', ') || 'N/A',
inline: true
}
];
}
}
module.exports = Chatlogs;