galactic-bot/structure/client/components/settings/moderation/Mute.js

311 lines
12 KiB
JavaScript
Raw Normal View History

const { Setting } = require('../../../../interfaces/');
const emojis = require('../../../../../util/emojis.json');
const maxCharacters = 98;
const { stripIndents } = require('common-tags');
class MuteSetting extends Setting {
constructor(client) {
super(client, {
name: 'mute',
module: 'moderation',
aliases: [
'muted',
'muteType',
'mutedType',
'muteRole',
'mutedRole',
2020-05-24 23:45:47 +02:00
'createMute',
'createMuteRole',
'createMuted',
2020-06-16 00:15:13 +02:00
'createMutedRole',
'permaMute',
'permanentMute',
'defaultMuteDuration',
],
arguments: [
{
name: 'create',
description: "A_CREATE_MUTE_SETTINGS",
type: 'BOOLEAN',
types: ['VERBAL'],
default: true
},
{
name: 'type',
description: "A_TYPE_MUTE_SETTINGS",
type: 'BOOLEAN',
types: ['VERBAL'],
default: true
},
2020-06-16 00:15:13 +02:00
{
name: 'permanent',
description: "A_PERMANENT_MUTE_SETTINGS",
aliases: ['perm', 'perma'],
type: 'BOOLEAN',
types: ['VERBAL'],
default: true
},
{
name: 'defaultDuration',
description: "A_DEFAULT_MUTE_SETTINGS",
aliases: ['default'],
type: 'BOOLEAN',
types: ['VERBAL'],
default: true
},
{
name: 'text',
description: "A_TEXT_MUTE_SETTINGS",
type: 'BOOLEAN',
types: ['FLAG'],
default: true
},
{
name: 'voice',
description: "A_VOICE_MUTE_SETTINGS",
type: 'BOOLEAN',
types: ['FLAG'],
default: true
}
],
2020-06-16 00:15:13 +02:00
usage: '[type|role|create|permanent|duration] [value]',
examples: [
2020-06-16 00:15:13 +02:00
'createmute Muted',
'mutetype 1',
2020-06-16 00:15:13 +02:00
'permamute true',
'mute duration 1 day'
],
resolve: 'GUILD',
default: {
mute: {
role: null,
2020-06-16 00:15:13 +02:00
type: 0,
defaultDuration: 3600,
permanent: false
}
}
});
}
async handle(message, args) {
const { params, parsedArguments } = await this._parseArguments(args, message.guild);
2020-06-16 00:15:13 +02:00
console.log('wut');
console.log(params);
2020-05-24 00:11:42 +02:00
if(['mutetype', 'mutedtype'].includes(message._settingCaller) || parsedArguments.type) {
2020-06-16 00:15:13 +02:00
if(!params[0]) return { error: true, msg: message.format('S_MUTE_TYPENAN') };
const num = params[0];
const number = parseInt(num);
2020-06-16 00:15:13 +02:00
if(Number.isNaN(number)) return {
msg: message.format('S_MUTE_TYPENAN'),
error: true
};
if(![0, 1, 2].includes(number)) return {
msg: message.format('S_MUTE_TYPEINVALID'),
error: true
};
await message.guild._updateSettings({
[this.index]: {
...message.guild._settings[this.index],
type: number
}
});
2020-06-16 00:15:13 +02:00
return {
msg: `${message.format('S_MUTE_TYPESUCCESS', { type: number })} ${message.format('S_MUTE_TYPESWITCH', { type: number }, true)}`,
error: false
};
2020-06-16 00:15:13 +02:00
} else if (['permamute', 'permanentmute'].includes(message._settingCaller) || parsedArguments.permanent) {
if(!params[0]) return { error: true, msg: message.format('S_MUTE_PERMANENTINVALID') };
const boolean = this.client.resolver.resolveBoolean(params[0]);
if(boolean === null) {
return {
msg: message.format('S_MUTE_PERMANENTINVALID'),
error: true
}
}
await message.guild._updateSettings({
[this.index]: {
...message.guild._settings[this.index],
permanent: boolean
}
});
return {
msg: message.format('S_MUTE_PERMANENTSUCCESS', { boolean }),
error: false
};
} else if (['defaultMuteDuration'].includes(message._settingCaller) || parsedArguments.defaultDuration) {
if(!params.length) return { error: true, msg: message.format('S_MUTE_PERMANENTINVALID') };
const time = this.client.resolver.resolveTime(params.join(' '));
if(!time) {
return {
msg: message.format('S_MUTE_DEFAULTINVALID'),
error: true
}
}
await message.guild._updateSettings({
[this.index]: {
...message.guild._settings[this.index],
defaultDuration: time
}
});
return {
msg: message.format('S_MUTE_DEFAULTSUCCESS', { time }),
error: false
};
}
let role = null,
updatedPermissions = false,
created = false,
issues = [];
2020-05-24 23:45:47 +02:00
if(parsedArguments.create || ['createmute', 'createmuted', 'createmutedrole', 'createmuterole'].includes(message._settingCaller)) {
const missing = message.channel.permissionsFor(message.guild.me).missing('MANAGE_ROLES');
if(missing.length > 0) return {
msg: message.format('S_MUTE_ROLEMISSINGPERMISSION'),
error: true
};
2020-06-16 00:15:13 +02:00
const foundRole = await message.guild.resolveRole(params.join(' '), true);
if(foundRole) {
const prompt = await message.prompt(message.format('S_MUTE_ROLEPROMPT', { name: foundRole.name, id: foundRole.id }), { emoji: 'loading' });
const response = prompt.content.toLowerCase();
const bool = this.client.resolver.resolveBoolean(response);
if(bool === null) return { msg: message.format('S_MUTE_ROLEPROMPTERROR'), error: true };
if(!bool) {
2020-06-16 00:15:13 +02:00
role = await this._createRole(message, params);
if(role.error) return role;
created = true;
} else {
role = foundRole;
}
} else {
2020-06-16 00:15:13 +02:00
role = await this._createRole(message, params);
if(role.error) return role;
created = true;
}
const channels = message.guild.channels.cache;
for(const channel of channels.values()) {
const configuration = channel.type === 'text'
? { permissions: { SEND_MESSAGES: false, ADD_REACTIONS: false }, bitwise: 0x800 }
: { permissions: { CONNECT: false }, bitwise: 0x100000 };
try {
2020-06-16 00:15:13 +02:00
// console.log(channel.type)
if((channel.type === 'text' && !parsedArguments.voice) || (channel.type === 'voice' && !parsedArguments.text) || (!parsedArguments.text && !parsedArguments.voice)) {
if(channel.type === 'category') continue;
await channel.createOverwrite(role, configuration.permissions, super.reason(message.author));
for(const permission of channel.permissionOverwrites.values()) {
if(permission.type !== 'role') continue;
const r = await this.client.resolver.resolveRole(permission.id, true, message.guild);
if((permission.allow & configuration.bitwise) === configuration.bitwise) {
const info = {
role: r.name,
permission: channel.type === 'text' ? 'SEND_MESSAGES' : 'CONNECT',
channel: channel.name,
type: channel.type,
emoji: channel.type === 'text' ? emojis['text-channel'] : emojis['voice-channel']
}
issues.push(info);
}
}
}
} catch(err) {} //eslint-disable-line no-empty
}
updatedPermissions = true;
} else {
2020-06-16 00:15:13 +02:00
const search = params.join(' ');
role = await this.client.resolver.resolveRole(search, true, message.guild);
}
if(!role) return {
msg: message.format('S_MUTE_ROLEMISSING'),
error: true
};
await message.guild._updateSettings({
[this.index]: {
...message.guild._settings[this.index],
role: role.id
}
});
2020-06-16 00:15:13 +02:00
// console.log(issues);
return {
msg: stripIndents`${message.format('S_MUTE_ROLESUCCESS', { role: role.name, type: created ? 'created' : 'set' })} ${updatedPermissions ? message.format('S_MUTE_GENERATEDPERMISSIONS') : message.format('S_MUTE_UNGENERATEDPERMISSIONS')}
${issues.length > 0 ? message.format('S_MUTE_ISSUES', { issues: issues.map(i=>`**\`${i.role}\`:** Has the \`${i.permission}\` permission in **${i.emoji}${i.channel}**.`).join('\n') }) : ''}`,
error: false
};
}
async _createRole(message, args) {
let role = null;
let name = args.join(' ') || 'Muted';
if(name.length > maxCharacters) name = name.slice(0, maxCharacters);
try {
role = await message.guild.roles.create({
data: {
name
},
reason: super.reason(message.author)
});
} catch(error) {
return {
msg: message.format('S_MUTE_ROLECREATEERROR'),
error: true
};
}
return role;
}
fields(guild) {
2020-06-16 00:15:13 +02:00
const setting = guild._settings[this.index];
return [
{
2020-05-22 22:13:47 +02:00
name: '》Muted Role',
2020-06-16 00:15:13 +02:00
value: setting?.role ? `<@&${guild._settings[this.index].role}>` : '`N/A`',
inline: true
},
{
2020-05-22 22:13:47 +02:00
name: '》Mute Type',
2020-06-16 00:15:13 +02:00
value: `\`${setting?.type || 0}\``,
inline: true
2020-06-16 00:15:13 +02:00
},
{ name: '\u200b', value: '\u200b', inline: true }, //zero width spaces, not necessary, but could make things look nicer.
{
name: '》Permanent Mutes',
value: `\`${setting?.permanent || 'false'}\``,
inline: true
},
{
name: '》Default Duration',
value: `\`${setting.defaultDuration ? `${setting.defaultDuration} seconds` : 'N/A'}\``,
inline: true
},
{ name: '\u200b', value: '\u200b', inline: true } //zero width spaces, not necessary, but could make things look nicer.
];
}
}
module.exports = MuteSetting;