2020-05-21 12:47:58 +02:00
const { Setting } = require ( '../../../../interfaces/' ) ;
2020-06-02 12:09:28 +02:00
const emojis = require ( '../../../../../util/emojis.json' ) ;
2020-05-21 12:47:58 +02:00
const maxCharacters = 98 ;
2020-06-02 12:09:28 +02:00
const { stripIndents } = require ( 'common-tags' ) ;
2020-05-21 12:47:58 +02:00
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' ,
'createMutedRole'
] , //people really be stupid like that
2020-05-21 12:47:58 +02:00
arguments : [
{
name : 'create' ,
2020-06-02 12:09:28 +02:00
description : "A_CREATE_MUTE_SETTINGS" ,
2020-05-21 12:47:58 +02:00
type : 'BOOLEAN' ,
2020-06-02 12:09:28 +02:00
types : [ 'VERBAL' ] ,
2020-05-21 12:47:58 +02:00
default : true
} ,
{
name : 'type' ,
2020-06-02 12:09:28 +02:00
description : "A_TYPE_MUTE_SETTINGS" ,
type : 'BOOLEAN' ,
types : [ 'VERBAL' ] ,
default : true
} ,
{
name : 'text' ,
description : "A_TEXT_MUTE_SETTINGS" ,
2020-05-21 12:47:58 +02:00
type : 'BOOLEAN' ,
2020-06-02 12:09:28 +02:00
types : [ 'FLAG' ] ,
default : true
} ,
{
name : 'voice' ,
description : "A_VOICE_MUTE_SETTINGS" ,
type : 'BOOLEAN' ,
types : [ 'FLAG' ] ,
2020-05-21 12:47:58 +02:00
default : true
}
] ,
2020-05-24 10:54:33 +02:00
usage : '[type|role|create] [muted-role|mute-type]' ,
2020-05-21 12:47:58 +02:00
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 ;
2020-05-24 00:11:42 +02:00
if ( [ 'mutetype' , 'mutedtype' ] . includes ( message . _settingCaller ) || parsedArguments . type ) {
2020-05-21 12:47:58 +02:00
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
} ;
}
2020-06-02 12:09:28 +02:00
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 ) ) {
2020-05-21 12:47:58 +02:00
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-02 12:09:28 +02:00
const foundRole = await message . guild . resolveRole ( args . join ( ' ' ) , true ) ;
2020-05-21 12:47:58 +02:00
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 ;
}
2020-06-02 12:09:28 +02:00
const channels = message . guild . channels . cache ;
2020-05-21 12:47:58 +02:00
for ( const channel of channels . values ( ) ) {
2020-06-02 12:09:28 +02:00
const configuration = channel . type === 'text'
? { permissions : { SEND _MESSAGES : false , ADD _REACTIONS : false } , bitwise : 0x800 }
: { permissions : { CONNECT : false } , bitwise : 0x100000 } ;
2020-05-21 12:47:58 +02:00
try {
2020-06-02 12:09:28 +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 ) ;
}
}
}
2020-05-21 12:47:58 +02:00
} catch ( err ) { } //eslint-disable-line no-empty
}
updatedPermissions = true ;
} else {
const search = args . join ( ' ' ) ;
2020-06-02 12:09:28 +02:00
role = await this . client . resolver . resolveRole ( search , true , message . guild ) ;
2020-05-21 12:47:58 +02:00
}
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-02 12:09:28 +02:00
console . log ( issues ) ;
2020-05-21 12:47:58 +02:00
return {
2020-06-02 12:09:28 +02:00
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' ) } ) : '' } ` ,
2020-05-21 12:47:58 +02:00
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 ) {
return [
{
2020-05-22 22:13:47 +02:00
name : '》Muted Role' ,
value : guild . _settings [ this . index ] ? . role ? ` <@& ${ guild . _settings [ this . index ] . role } > ` : '`N/A`' ,
2020-05-21 12:47:58 +02:00
inline : true
} ,
{
2020-05-22 22:13:47 +02:00
name : '》Mute Type' ,
value : ` \` ${ guild . _settings [ this . index ] ? . type || 0 } \` ` ,
2020-05-21 12:47:58 +02:00
inline : true
}
] ;
}
}
module . exports = MuteSetting ;