galactic-bot/structure/client/components/settings/moderation/VoiceLogs.js
2020-08-04 12:35:28 +03:00

97 lines
2.9 KiB
JavaScript

const { Setting } = require('../../../../interfaces/');
class VoiceLogSettings extends Setting {
constructor(client) {
super(client, {
name: 'voiceLog',
module: 'moderation',
aliases: [
'vcLog',
'vcLogs',
'voiceLogs'
],
tags: ['log', 'logs', 'logging'],
usage: '<value>',
resolve: 'GUILD',
examples: [
'voicelog reset',
'voicelog on|off',
'voicelog #channel'
],
default: {
voiceLog: {
channel: 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 setting = message.guild._settings[this.index] || this.default[this.index];
const { guild } = message;
if (this.client.resolver.resolveBoolean(method) === false) {
setting.channel = null;
index = 'S_VOICELOG_TOGGLE'
changes = message.format('ON_OFF_TOGGLE', { toggle: false }, true);
} else {
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 = [];
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(', ') })
};
index = 'S_VOICELOG_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];
return [
{
name: '》Channel',
value: await guild.resolveChannel(setting?.channel) || '`N/A`',
inline: true
}
];
}
}
module.exports = VoiceLogSettings;