forked from Galactic/galactic-bot
253 lines
9.3 KiB
JavaScript
253 lines
9.3 KiB
JavaScript
const { Setting } = require('../../../../interfaces/');
|
|
|
|
class MessageLogsSetting extends Setting {
|
|
|
|
constructor(client) {
|
|
|
|
super(client, {
|
|
name: 'messageLog',
|
|
module: 'moderation',
|
|
aliases: [
|
|
'chatLog',
|
|
'chatLogs',
|
|
'msgLogs',
|
|
'messageLogs',
|
|
'msgLog'
|
|
],
|
|
usage: '<method|value> [value..]',
|
|
resolve: 'GUILD',
|
|
examples: [
|
|
'messagelogs roles <add|remove|list> <role>',
|
|
'messagelogs channels <add|remove|list> <role>',
|
|
'messagelogs reset',
|
|
'messagelogs off',
|
|
'messagelogs attachments <on|off>',
|
|
'messagelogs #channel'
|
|
],
|
|
default: {
|
|
messageLog: {
|
|
channel: null,
|
|
ignoredChannels: [],
|
|
ignoredRoles: [],
|
|
attachments: false,
|
|
enabled: false,
|
|
webhook: null
|
|
}
|
|
}
|
|
});
|
|
|
|
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[this.index] || this.default[this.index];
|
|
const { guild } = message;
|
|
|
|
if (['roles', 'role', 'ignoredrole', 'ignoredroles'].includes(method)) {
|
|
|
|
if (!args.length) return {
|
|
msg: message.format('MISSING_ARGS'),
|
|
error: true
|
|
};
|
|
const response = await this.resolveMethod(args, undefined, setting.ignoredRoles, guild.resolveRoles.bind(guild));
|
|
|
|
if (response) {
|
|
|
|
if (response.method === 'add') {
|
|
|
|
const roles = response.resolved;
|
|
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;
|
|
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 (['channels', 'channel', 'ignoredchannels', 'ignoredchannel'].includes(method)) {
|
|
|
|
if (!args.length) return {
|
|
msg: message.format('MISSING_ARGS'),
|
|
error: true
|
|
};
|
|
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_MESSAGELOG_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_MESSAGELOG_CHANNELS';
|
|
changes = channels.map(c => c.name);
|
|
|
|
} else if (response.method === 'list') {
|
|
return {
|
|
msg: message.format('S_MESSAGELOG_LIST')
|
|
};
|
|
}
|
|
|
|
changes = response.changed;
|
|
|
|
} else {
|
|
const channels = 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) return {
|
|
msg: message.format('PREMIUM_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 = guild.resolveChannel(setting.channel);
|
|
if (channel) { //Channel might have been deleted
|
|
const hooks = channel.fetchWebhooks();
|
|
const hook = hooks.filter(hook => hook.id === setting.webhook).first();
|
|
if (hook && !channel.permissionsFor(guild.me).has('MANAGE_WEBHOOKS')) index = 'S_MESSAGELOG_TOGGLE_PERM' //missing perms
|
|
else try { //If necessary perms and a hook exists, remove it and uncache it from the main collection
|
|
if (hook) await hook.delete('Removing message logging hook, as message logs were disabled.');
|
|
guild.webhooks.delete(setting.webhook);
|
|
setting.webhook = null;
|
|
} catch (err) {
|
|
this.client.logger.error(err);
|
|
if (err.code === 10015) setting.webhook = null; // webhook is somehow missing prior to the bot deleting it?
|
|
}
|
|
}
|
|
}
|
|
|
|
setting.channel = null;
|
|
|
|
} else { //Set the channel & configure webhook
|
|
|
|
const channel = guild.resolveChannel(method);
|
|
if (!channel) return {
|
|
msg: message.format('ERR_CHANNEL_RESOLVE', { resolveable: method }),
|
|
error: true
|
|
};
|
|
|
|
//Handle old webhook if one exists
|
|
const oldChannel = guild.resolveChannel(setting.channel);
|
|
if(oldChannel )
|
|
if (oldChannel && chatlogs.webhook && oldChannel.id !== channel.id) {
|
|
const hooks = await oldChannel.fetchWebhooks();
|
|
const hook = hooks.filter(hook => hook.id === setting.webhook).first();
|
|
if (hook) {
|
|
guild.webhooks.delete(hook.id);
|
|
hook.delete('Removing old webhook').catch(this.client.logger.error);
|
|
}
|
|
}
|
|
|
|
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;
|
|
return [
|
|
{
|
|
name: '》Channel',
|
|
value: guild.resolveChannel(setting?.channel) || '`N/A`',
|
|
inline: true
|
|
},
|
|
{
|
|
name: '》Ignored Roles',
|
|
value: roles?.map((r) => r.name).join(', ') || '`N/A`',
|
|
inline: false
|
|
},
|
|
{
|
|
name: '》Ignored Channels',
|
|
value: guild._settings[this.index]?.ignoredChannels.map((c) => guild.resolveChannel(c).name).join(', ') || '`N/A`',
|
|
inline: false
|
|
}
|
|
];
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = MessageLogsSetting; |