galactic-bot/structure/client/components/settings/moderation/ModerationLog.js
2020-06-19 21:29:54 +03:00

130 lines
4.4 KiB
JavaScript

const { Setting } = require('../../../../interfaces/');
const CONSTANTS = {
INFRACTIONS: ['note', 'warn', 'mute', 'unmute', 'lockdown', 'unlockdown', 'kick', 'ban', 'unban', 'vcmute', 'vcunmute', 'vckick', 'vcban', 'vcunban', 'prune', 'slowmode', 'dehoist', 'addrole', 'removerole', 'nickname']
};
class ModerationLogsSetting extends Setting {
constructor(client) {
super(client, {
name: 'moderationLog',
module: 'moderation',
aliases: [
'moderationLogs',
'modLog',
'modLogs'
],
usage: '<method|value> [value..]',
resolve: 'GUILD',
examples: [
'modlog <add|remove|list> <infraction-type..>',
'modlog #moderation-log',
'modlog reset',
'modlog off'
],
default: {
moderationLog: {
channel: null,
infractions: ['warn','mute','unmute','lockdown','unlockdown','kick','ban','unban','vcmute','vcunmute','vckick','vcban','vcunban']
}
}
});
this.client = client;
}
async handle(message, params) {
const setting = message.guild._settings.moderationLog;
const response = await this.resolveMethod(params, CONSTANTS.INFRACTIONS, setting.infractions);
if (response) {
console.log(response);
if (params.length < 2 && response.method !== 'list') return {
msg: message.format('MISSING_ARGS'),
error: true
}
let index;
if (response.method === 'add') {
setting.infractions = response.result;
index = 'S_MODERATIONLOG_ADD';
} else if (response.method === 'remove') {
setting.infractions = response.result;
index = 'S_MODERATIONLOG_REMOVE';
} else if (response.method === 'list') {
return {
msg: message.format('S_MODERATIONLOG_LIST', { list: setting.infractions.join('`, `') })
}
} else if (response.method === 'off') {
setting.channel = null;
index = 'S_MODERATIONLOG_OFF'
} else {
return {
msg: message.format('ERR_INVALID_METHOD', {
method: response.method
}),
error: true
}
}
if(response.changed.length) await message.guild._updateSettings({ [this.index]: setting });
return { msg: message.format(index, { list: response.changed.length ? response.changed.join('`, `') : 'N/A' }) };
} else {
let [channel] = params;
channel = await message.guild.resolveChannel(channel);
if (!channel) return {
msg: message.format('S_MODERATIONLOG_CHANNEL404', { val: params[0] }),
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 = [];
if(!perms.has('SEND_MESSAGES')) missingPerms.push('SEND_MESSAGES');
if(!perms.has('VIEW_CHANNEL')) missingPerms.push('VIEW_CHANNEL');
if(missingPerms.length) return {
error: true,
msg: message.format('ERR_CHANNEL_PERMS', { channel: channel.name, perms: missingPerms.join(', ') })
};
setting.channel = channel.id;
await message.guild._updateSettings({ [this.index]: setting }); return {
msg: message.format('S_MODERATIONLOG_CHANNEL_SUCCESS', { channel: channel.name }),
error: false
};
}
}
async fields(guild) {
const setting = guild._settings[this.index];
return [
{
name: '》Channel',
value: await guild.resolveChannel(setting?.channel) || '`N/A`',
inline: true
},
{
name: '》Logged Infractions',
value: setting?.infractions.map(i=>`\`${i}\``).join(', ') || '`N/A`',
inline: true
}
];
}
}
module.exports = ModerationLogsSetting;