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

186 lines
6.1 KiB
JavaScript

const { Setting } = require('../../../../interfaces/');
const maxCharacters = 98;
class MuteSetting extends Setting {
constructor(client) {
super(client, {
name: 'mute',
module: 'moderation',
aliases: [
'muted',
'muteType',
'mutedType',
'muteRole',
'mutedRole',
'createMute'
],
arguments: [
{
name: 'create',
type: 'BOOLEAN',
types: ['VERBAL', 'FLAG'],
default: true
},
{
name: 'type',
type: 'BOOLEAN',
types: ['VERBAL', 'FLAG'],
default: true
}
],
usage: "[test] <what>",
examples: [
'muterole Muted',
'mutetype 1',
'createmute galacticbot-mute'
],
resolve: 'GUILD',
default: {
mute: {
role: null,
type: 0
}
}
});
this.client = client;
}
async handle(message, args) {
const { params, parsedArguments } = await this._parseArguments(args, message.guild);
args = params;
if(['mutetype', 'mutedtype'].includes(this._caller) || parsedArguments.type) {
const num = args[0].toLowerCase() === 'type' ? args[1] || 0 : args[0];
const number = parseInt(num);
if(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
}
});
return {
msg: `${message.format('S_MUTE_TYPESUCCESS', { type: number })} ${message.format('S_MUTE_TYPESWITCH', { type: number }, true)}`,
error: false
};
}
let role = null;
let updatedPermissions = false;
let created = false;
if(parsedArguments.create || this._caller === 'createmute') {
const missing = message.channel.permissionsFor(message.guild.me).missing('MANAGE_ROLES');
if(missing.length > 0) return {
msg: message.format('S_MUTE_ROLEMISSINGPERMISSION'),
error: true
};
const foundRole = await this.client.resolver.resolveRole(args.join(' '), message.guild);
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) {
role = await this._createRole(message, args);
if(role.error) return role;
created = true;
} else {
role = foundRole;
}
} else {
role = await this._createRole(message, args);
if(role.error) return role;
created = true;
}
const channels = message.guild.channels.cache.filter((c) => c.type === 'text');
for(const channel of channels.values()) {
try {
await channel.createOverwrite(role, {
SEND_MESSAGES: false,
ADD_REACTIONS: false
}, super.reason(message.author));
} catch(err) {} //eslint-disable-line no-empty
}
updatedPermissions = true;
} else {
const search = args.join(' ');
role = await this.client.resolver.resolveRole(search, 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
}
});
return {
msg: `${message.format('S_MUTE_ROLESUCCESS', { role: role.name, type: created ? 'created' : 'set' })} ${updatedPermissions ? message.format('S_MUTE_GENERATEDPERMISSIONS') : message.format('S_MUTE_UNGENERATEDPERMISSIONS')}`,
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;
}
data(guild) {
return `**Muted Role:** ${guild._settings[this.index].role ? `<@&${guild._settings[this.index].role}>` : 'N/A'}\n**Mute Type:** \`${guild._setting[this.index].type}\``;
}
fields(guild) {
return [
{
name: "Muted Role",
value: guild._settings[this.index].role ? `<@&${guild._settings[this.index].role}>` : '`N/A`',
inline: true
},
{
name: "Mute Type",
value: `\`${guild._settings[this.index].type}\``,
inline: true
}
];
}
}
module.exports = MuteSetting;