galactic-bot/structure/client/components/settings/moderation/MessageLog.js
2021-05-05 17:46:01 +03:00

284 lines
11 KiB
JavaScript

const { Setting } = require('../../../../interfaces/');
class MessageLogsSetting extends Setting {
constructor(client) {
super(client, {
name: 'messageLog',
module: 'moderation',
clientPermissions: ['MANAGE_WEBHOOKS'],
aliases: [
'chatLog',
'chatLogs',
'msgLogs',
'messageLogs',
'msgLog'
],
tags: ['log', 'logs', 'logging'],
usage: '<method|value> [value..]',
resolve: 'GUILD',
examples: [
'msglog #channel',
'msglog ignorerole <add|remove|list> <role>',
'msglog ignorechannel <add|remove|list> <role>',
'msglog attachments <on|off>',
'msglog off'
],
default: {
messageLog: {
channel: null,
ignoredChannels: [],
ignoredRoles: [],
attachments: false,
webhook: null
}
}
});
}
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 { guild } = message;
const setting = guild._settings[this.index];
if (['bypass', 'roles', 'role', 'ignoredrole', 'ignoredroles', 'ignorerole', 'ignoreroles'].includes(method)) {
if (!args.length) return {
msg: message.format('MISSING_ARGS'),
error: true
};
const response = await this.resolveMethod(args, { existing: setting.ignoredRoles, resolver: guild.resolveRoles.bind(guild) });
if (response) {
if (response.method === 'add') {
const roles = response.resolved;
if (!roles.length) return { error: true, msg: message.format('ERR_ROLERESOLVE') };
setting.ignoredRoles = [...setting.ignoredRoles, ...roles.filter((r) => !setting.ignoredRoles.includes(r.id)).map((r) => r.id)];
action = 'GENERAL_ADDED';
index = 'S_MESSAGELOG_ROLES';
changes = roles.map((r) => `**${r.name}**`);
} else if (response.method === 'remove') {
const roles = response.resolved;
if (!roles.length) return { error: true, msg: message.format('ERR_ROLERESOLVE') };
const _roles = roles.map((r) => r.id);
setting.ignoredRoles = setting.ignoredRoles.filter((r) => !_roles.includes(r));
action = 'GENERAL_REMOVED';
index = 'S_MESSAGELOG_ROLES';
changes = roles.map((r) => `**${r.name}**`);
} else if (response.method === 'list') {
const roles = await guild.resolveRoles(setting.ignoredRoles);
return {
msg: message.format('S_MESSAGELOG_ROLES_LIST', { roles: roles.map((r) => r.name).join(', ') })
};
}
} else {
return {
msg: message.format('ERR_INVALID_METHOD', { method }),
error: true
};
}
} else if (['ignore', 'channels', 'channel', 'ignoredchannels', 'ignoredchannel', 'ignorechannel', 'ignorechannels'].includes(method)) {
if (!args.length) return {
msg: message.format('MISSING_ARGS'),
error: true
};
const response = await this.resolveMethod(args, { existing: setting.ignoredChannels, resolver: guild.resolveChannels.bind(guild) });
if (response) {
if (response.method === 'add') {
const channels = response.resolved; //await guild.resolveChannels(response.rest);
if (!channels.length) return { error: true, msg: message.format('ERR_CHRESOLVE') };
setting.ignoredChannels = [...setting.ignoredChannels, ...channels.filter((c) => !setting.ignoredChannels.includes(c.id)).map((c) => c.id)];
action = 'GENERAL_ADDED';
index = 'S_MESSAGELOG_CHANNELS';
changes = channels.map((c) => `<#${c.id}>`);
} else if (response.method === 'remove') {
const channels = response.resolved; //await guild.resolveChannels(response.rest);
if (!channels.length) return { error: true, msg: message.format('ERR_CHRESOLVE') };
const _channels = channels.map((c) => c.id);
setting.ignoredChannels = setting.ignoredChannels.filter((c) => !_channels.includes(c));
action = 'GENERAL_REMOVED';
index = 'S_MESSAGELOG_CHANNELS';
changes = channels.map((c) => `<#${c.id}>`);
} else if (response.method === 'list') {
return {
msg: message.format('S_MESSAGELOG_LIST')
};
}
} else {
const channels = await guild.resolveChannels(setting.ignoredChannels);
return {
msg: message.format('S_MESSAGELOG_ROLES_LIST', { roles: channels.map((r) => r.name).join(', ') })
};
}
} else if (['attachments', 'images', 'attachment', 'image'].includes(method)) {
if (guild.premium < 2 && !message.author.developer) return {
msg: message.format('PREMIUM_REQUIRED', { required: 2, tier: guild.premium }),
error: true
};
if (!args.length) return {
msg: message.format('MISSING_ARGS'),
error: true
};
const [bool] = args;
const result = this.client.resolver.resolveBoolean(bool);
if (result) {
setting.attachments = true;
index = 'S_MESSAGELOG_ATTACHMENTS';
changes = message.format('ON_OFF_TOGGLE', { toggle: true }, true);
} else {
setting.attachments = false;
index = 'S_MESSAGELOG_ATTACHMENTS';
changes = message.format('ON_OFF_TOGGLE', { toggle: false }, true);
}
} else if (this.client.resolver.resolveBoolean(method) === false) {
index = 'S_MESSAGELOG_TOGGLE';
changes = message.format('ON_OFF_TOGGLE', { toggle: false }, true);
if (setting.webhook) { //Clean up - remove webhook since logs were disabled
const channel = await guild.resolveChannel(setting.channel);
if (channel) { //Channel might have been deleted
//const hooks = await channel.fetchWebhooks();
const hook = await guild.getWebhook(this.index); //hooks.filter((hook) => hook.id === setting.webhook.id).first();
if (hook && !channel.permissionsFor(guild.me).has('MANAGE_WEBHOOKS')) return { error: true, msg: message.format('S_MESSAGELOG_TOGGLE_PERM') }; //index = 'S_MESSAGELOG_TOGGLE_PERM'; //missing perms
else if (hook) {
//await hook.delete('Removing message logging hook, as message logs were disabled.').catch(this.client.logger.error);
//guild.webhooks.delete(setting.webhook);
await guild.updateWebhook(this.index);
}
}
}
setting.channel = null;
} else { //Set the channel & configure webhook
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 = perms.missing(['SEND_MESSAGES', 'VIEW_CHANNEL', 'MANAGE_WEBHOOKS', 'EMBED_LINKS']);
if(missingPerms.length) return {
error: true,
msg: message.format('ERR_CHANNEL_PERMS', { channel: channel.name, perms: missingPerms.join(', ') })
};
//Handle old webhook if one exists
const oldChannel = await guild.resolveChannel(setting.channel);
if (oldChannel && oldChannel.id !== channel.id) {
//const hooks = await oldChannel.fetchWebhooks();
const hook = await guild.getWebhook(this.index); //hooks.filter((hook) => hook.id === setting.webhook.id).first();
if (hook) {
//guild.updateWebhook(this.index);
//hook.delete('Removing old webhook').catch(this.client.logger.error);
await hook.edit({ channel });
}
} else if (!oldChannel) {
//Create new webhook
const hook = await channel.createWebhook('Galactic Bot message logs', { avatar: './util/GBotTest.png', reason: 'Message logs webhook.' });
await guild.updateWebhook(this.index, hook);
}
index = 'S_MESSAGELOG_CHANNEL';
changes = channel.name;
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 setting = guild._settings[this.index];
const roles = setting?.ignoredRoles ? await Promise.all(setting.ignoredRoles.map(async (role) => guild.resolveRole(role))) : undefined;
const channels = setting?.ignoredChannels ? await Promise.all(setting.ignoredChannels.map(async (c) => guild.resolveChannel(c))) : undefined;
return [
{
name: '》 Status',
value: guild.format('SETTING_STATUS', { bool: Boolean(setting?.channel) }, true),
inline: true
},
{
name: '》 Channel',
value: await guild.resolveChannel(setting?.channel) || '`N/A`',
inline: true
},
{
name: '》 Attachment Logs',
value: guild.format('SETTING_STATUS', { bool: Boolean(setting?.attachments) }, true),
inline: true
},
{
name: '》 Ignored Roles',
value: roles?.map((r) => `<@&${r.id}>`).join(' ') || '`N/A`'
},
{
name: '》 Ignored Channels',
value: channels?.map((c) => `<#${c.id}>`).join(' ') || '`N/A`'
}
];
}
async _handleReset(message) {
const result = await super._handleReset(message);
await message.guild.updateWebhook(this.index);
return result;
}
}
module.exports = MessageLogsSetting;