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',
|
|
|
|
aliases: [
|
|
|
|
'chatLog',
|
|
|
|
'chatLogs',
|
2020-05-24 10:54:33 +02:00
|
|
|
'msgLogs',
|
|
|
|
'messageLogs',
|
|
|
|
'msgLog'
|
2020-05-24 00:11:06 +02:00
|
|
|
],
|
2020-08-04 11:35:28 +02:00
|
|
|
tags: ['log', 'logs', 'logging'],
|
2020-05-24 23:42:17 +02:00
|
|
|
usage: '<method|value> [value..]',
|
2020-05-24 00:11:06 +02:00
|
|
|
resolve: 'GUILD',
|
|
|
|
examples: [
|
2020-05-24 23:42:17 +02:00
|
|
|
'messagelogs roles <add|remove|list> <role>',
|
|
|
|
'messagelogs channels <add|remove|list> <role>',
|
|
|
|
'messagelogs reset',
|
2020-05-25 13:13:34 +02:00
|
|
|
'messagelogs off',
|
2020-05-24 23:42:17 +02:00
|
|
|
'messagelogs attachments <on|off>',
|
|
|
|
'messagelogs #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: [],
|
2020-05-24 23:42:17 +02:00
|
|
|
attachments: false,
|
2020-06-18 16:07:39 +02:00
|
|
|
webhook: null
|
2020-05-24 00:11:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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 23:42:17 +02:00
|
|
|
const setting = message.guild._settings[this.index] || this.default[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
|
|
|
|
2020-05-24 23:42:17 +02:00
|
|
|
if (!args.length) return {
|
|
|
|
msg: message.format('MISSING_ARGS'),
|
|
|
|
error: true
|
|
|
|
};
|
|
|
|
const response = await this.resolveMethod(args, undefined, setting.ignoredRoles, guild.resolveRoles.bind(guild));
|
2020-05-24 00:11:06 +02:00
|
|
|
|
|
|
|
if (response) {
|
|
|
|
|
|
|
|
if (response.method === 'add') {
|
|
|
|
|
2020-05-24 23:42:17 +02:00
|
|
|
const roles = response.resolved;
|
2020-08-17 22:24:43 +02:00
|
|
|
if (!roles.length) return { error: true, msg: message.format('ERR_ROLERESOLVE') }
|
2020-07-23 22:40:20 +02:00
|
|
|
setting.ignoredRoles = [...setting.ignoredRoles, ...roles.filter((r) => !setting.ignoredRoles.includes(r.id)).map((r) => r.id)];
|
2020-05-24 00:11:06 +02:00
|
|
|
|
|
|
|
action = 'GENERAL_ADDED';
|
2020-05-24 10:54:33 +02:00
|
|
|
index = 'S_MESSAGELOG_ROLES';
|
2020-07-23 22:40:20 +02:00
|
|
|
changes = roles.map((r) => r.name);
|
2020-05-24 00:11:06 +02:00
|
|
|
|
|
|
|
} else if (response.method === 'remove') {
|
|
|
|
|
2020-05-24 23:42:17 +02:00
|
|
|
const roles = response.resolved;
|
2020-08-17 22:24:43 +02:00
|
|
|
if (!roles.length) return { error: true, msg: message.format('ERR_ROLERESOLVE') }
|
2020-07-23 22:40:20 +02:00
|
|
|
const _roles = roles.map((r) => r.id);
|
|
|
|
setting.ignoredRoles = setting.ignoredRoles.filter((r) => !_roles.includes(r));
|
2020-05-24 00:11:06 +02:00
|
|
|
|
|
|
|
action = 'GENERAL_REMOVED';
|
2020-05-24 10:54:33 +02:00
|
|
|
index = 'S_MESSAGELOG_ROLES';
|
2020-07-23 22:40:20 +02:00
|
|
|
changes = roles.map((r) => r.name);
|
2020-05-24 00:11:06 +02:00
|
|
|
|
|
|
|
} else if (response.method === 'list') {
|
|
|
|
|
|
|
|
const roles = await guild.resolveRoles(setting.ignoredRoles);
|
|
|
|
return {
|
2020-07-23 22:40:20 +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 {
|
2020-06-09 17:58:54 +02:00
|
|
|
msg: message.format('ERR_INVALID_METHOD', { method }),
|
|
|
|
error: true
|
2020-05-24 00:11:06 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-24 10:54:33 +02:00
|
|
|
} else if (['channels', 'channel', 'ignoredchannels', 'ignoredchannel'].includes(method)) {
|
2020-05-24 00:11:06 +02:00
|
|
|
|
2020-05-24 23:42:17 +02:00
|
|
|
if (!args.length) return {
|
|
|
|
msg: message.format('MISSING_ARGS'),
|
|
|
|
error: true
|
|
|
|
};
|
2020-08-17 22:24:43 +02:00
|
|
|
const response = await this.resolveMethod(args, null, setting.ignoredChannels, guild.resolveChannels.bind(guild));
|
2020-05-24 00:11:06 +02:00
|
|
|
|
|
|
|
if (response) {
|
|
|
|
|
|
|
|
if (response.method === 'add') {
|
|
|
|
|
2020-08-17 22:24:43 +02:00
|
|
|
const channels = response.resolved; //await guild.resolveChannels(response.rest);
|
|
|
|
if (!channels.length) return { error: true, msg: message.format('ERR_CHRESOLVE') }
|
2020-07-23 22:40:20 +02:00
|
|
|
setting.ignoredChannels = [...setting.ignoredChannels, ...channels.filter((c) => !setting.ignoredChannels.includes(c.id)).map((c) => c.id)];
|
2020-05-24 00:11:06 +02:00
|
|
|
|
|
|
|
action = 'GENERAL_ADDED';
|
2020-05-24 10:54:33 +02:00
|
|
|
index = 'S_MESSAGELOG_CHANNELS';
|
2020-07-23 22:40:20 +02:00
|
|
|
changes = channels.map((c) => c.name);
|
2020-05-24 00:11:06 +02:00
|
|
|
|
|
|
|
} else if (response.method === 'remove') {
|
|
|
|
|
2020-08-17 22:24:43 +02:00
|
|
|
const channels = response.resolved; //await guild.resolveChannels(response.rest);
|
|
|
|
if (!channels.length) return { error: true, msg: message.format('ERR_CHRESOLVE') }
|
2020-07-23 22:40:20 +02:00
|
|
|
const _channels = channels.map((c) => c.id);
|
|
|
|
setting.ignoredChannels = setting.ignoredChannels.filter((c) => !_channels.includes(c));
|
2020-05-24 00:11:06 +02:00
|
|
|
|
|
|
|
action = 'GENERAL_REMOVED';
|
2020-05-24 10:54:33 +02:00
|
|
|
index = 'S_MESSAGELOG_CHANNELS';
|
2020-07-23 22:40:20 +02:00
|
|
|
changes = channels.map((c) => c.name);
|
2020-05-24 00:11:06 +02:00
|
|
|
|
|
|
|
} 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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2020-06-19 15:38:56 +02:00
|
|
|
const channels = await guild.resolveChannels(setting.ignoredChannels);
|
2020-05-24 00:11:06 +02:00
|
|
|
return {
|
2020-07-23 22:40:20 +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 {
|
2020-07-11 22:39:05 +02:00
|
|
|
msg: message.format('PREMIUM_REQUIRED', { required: 2, tier: guild.premium }),
|
2020-05-24 00:11:06 +02:00
|
|
|
error: true
|
2020-07-23 22:40:20 +02:00
|
|
|
};
|
2020-05-24 23:42:17 +02:00
|
|
|
if (!args.length) return {
|
|
|
|
msg: message.format('MISSING_ARGS'),
|
|
|
|
error: true
|
|
|
|
};
|
2020-05-24 00:11:06 +02:00
|
|
|
|
|
|
|
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';
|
2020-05-24 23:42:17 +02:00
|
|
|
changes = message.format('ON_OFF_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';
|
2020-05-24 23:42:17 +02:00
|
|
|
changes = message.format('ON_OFF_TOGGLE', { toggle: false }, true);
|
2020-05-24 00:11:06 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-24 23:42:17 +02:00
|
|
|
} else if (this.client.resolver.resolveBoolean(method) === false) {
|
|
|
|
|
2020-07-23 22:40:20 +02:00
|
|
|
index = 'S_MESSAGELOG_TOGGLE';
|
2020-05-24 23:42:17 +02:00
|
|
|
changes = message.format('ON_OFF_TOGGLE', { toggle: false }, true);
|
|
|
|
|
2020-06-18 16:07:39 +02:00
|
|
|
if (setting.webhook) { //Clean up - remove webhook since logs were disabled
|
2020-06-19 15:38:56 +02:00
|
|
|
const channel = await guild.resolveChannel(setting.channel);
|
2020-06-18 16:07:39 +02:00
|
|
|
if (channel) { //Channel might have been deleted
|
2020-08-17 22:24:43 +02:00
|
|
|
const hooks = await channel.fetchWebhooks();
|
|
|
|
const hook = hooks.filter((hook) => hook.id === setting.webhook.id).first();
|
2020-06-18 23:00:32 +02:00
|
|
|
|
2020-08-17 22:24:43 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-06-18 16:07:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setting.channel = null;
|
|
|
|
|
|
|
|
} else { //Set the channel & configure webhook
|
2020-05-24 00:11:06 +02:00
|
|
|
|
2020-06-19 15:38:56 +02:00
|
|
|
const channel = await guild.resolveChannel(method);
|
2020-05-24 00:11:06 +02:00
|
|
|
if (!channel) return {
|
2020-05-24 23:42:17 +02:00
|
|
|
msg: message.format('ERR_CHANNEL_RESOLVE', { resolveable: method }),
|
2020-05-24 00:11:06 +02:00
|
|
|
error: true
|
|
|
|
};
|
2020-06-19 20:29:54 +02:00
|
|
|
if(channel.type !== 'text') return {
|
|
|
|
error: true,
|
|
|
|
msg: message.format('ERR_CHANNEL_TYPE', { type: channel.type })
|
|
|
|
};
|
|
|
|
|
|
|
|
const perms = channel.permissionsFor(guild.me);
|
2020-08-17 22:24:43 +02:00
|
|
|
const missingPerms = perms.missing(['SEND_MESSAGES', 'VIEW_CHANNEL', 'MANAGE_WEBHOOKS']);
|
2020-06-19 20:29:54 +02:00
|
|
|
|
|
|
|
if(missingPerms.length) return {
|
|
|
|
error: true,
|
|
|
|
msg: message.format('ERR_CHANNEL_PERMS', { channel: channel.name, perms: missingPerms.join(', ') })
|
|
|
|
};
|
2020-05-24 00:11:06 +02:00
|
|
|
|
2020-08-17 22:24:43 +02:00
|
|
|
//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 = hooks.filter(hook => hook.id === setting.webhook.id).first();
|
2020-06-18 23:00:32 +02:00
|
|
|
|
2020-08-17 22:24:43 +02:00
|
|
|
if (hook) {
|
|
|
|
guild.updateWebhook(this.index);
|
|
|
|
//hook.delete('Removing old webhook').catch(this.client.logger.error);
|
|
|
|
}
|
|
|
|
}
|
2020-06-18 23:00:32 +02:00
|
|
|
|
2020-08-17 22:24:43 +02:00
|
|
|
//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);
|
2020-06-18 16:07:39 +02:00
|
|
|
|
2020-05-24 23:42:17 +02:00
|
|
|
index = 'S_MESSAGELOG_CHANNEL';
|
|
|
|
changes = channel.name;
|
2020-05-24 00:11:06 +02:00
|
|
|
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) {
|
2020-05-24 23:42:17 +02:00
|
|
|
const setting = guild._settings[this.index];
|
2020-06-19 15:38:56 +02:00
|
|
|
const roles = setting?.ignoredRoles ? await Promise.all(setting.ignoredRoles.map(async (role) => await guild.resolveRole(role))) : undefined;
|
|
|
|
const channels = setting?.ignoredChannels ? await Promise.all(setting.ignoredChannels.map(async (c) => await guild.resolveChannel(c))) : undefined;
|
2020-05-24 00:11:06 +02:00
|
|
|
return [
|
|
|
|
{
|
2020-08-16 09:27:49 +02:00
|
|
|
name: '》 Status',
|
|
|
|
value: guild.format('SETTING_STATUS', { bool: Boolean(setting?.channel) }, true),
|
|
|
|
inline: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: '》 Channel',
|
2020-06-19 15:38:56 +02:00
|
|
|
value: await guild.resolveChannel(setting?.channel) || '`N/A`',
|
2020-05-24 00:11:06 +02:00
|
|
|
inline: true
|
|
|
|
},
|
2020-06-20 22:28:26 +02:00
|
|
|
{
|
2020-08-16 09:27:49 +02:00
|
|
|
name: '》 Attachment Logs',
|
|
|
|
value: guild.format('SETTING_STATUS', { bool: Boolean(setting?.attachments) }, true),
|
2020-06-20 22:28:26 +02:00
|
|
|
inline: true
|
|
|
|
},
|
2020-05-24 00:11:06 +02:00
|
|
|
{
|
2020-08-16 09:27:49 +02:00
|
|
|
name: '》 Ignored Roles',
|
|
|
|
value: roles?.map((r) => r.name).join(', ') || '`N/A`'
|
2020-05-24 00:11:06 +02:00
|
|
|
},
|
|
|
|
{
|
2020-08-16 09:27:49 +02:00
|
|
|
name: '》 Ignored Channels',
|
|
|
|
value: channels?.map((c) => c.name).join(', ') || '`N/A`'
|
2020-05-24 00:11:06 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-08-17 22:24:43 +02:00
|
|
|
async _handleReset(message) {
|
|
|
|
const result = await super._handleReset(message);
|
|
|
|
await message.guild.updateWebhook(this.index);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-24 00:11:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 10:54:33 +02:00
|
|
|
module.exports = MessageLogsSetting;
|