import type { EmbedField , Message , TextChannel } from "eris" ; import { Command } from "../../classes/Command" ; import { itemExists } from "../../utils/itemExists" ; export class UnassignroleCommand extends Command { description = "Removes a role from you that's set to be assignable." ; clientperms = [ "manageRoles" ] ; args = "[role:role]" ; aliases = [ "unassign" , "removerole" , "iamnot" ] ; async run ( msg , _pargs , args ) { const guildconfig = await this . bot . db . getGuildConfig ( msg . channel . guild . id ) ; if ( ! guildconfig ?. assignableRoles ?. length ) { return msg . createEmbed ( `📄 ${ msg . string ( "general.CONFIG_ASSIGNABLEROLES" ) }` , msg . string ( "general.ASSIGN_NOTHINGSET" ) ) ; } guildconfig . assignableRoles = ( await itemExists ( msg . channel . guild , "role" , guildconfig . assignableRoles , this . bot . db , "assignableRoles" ) ) as string [ ] ; if ( ! args . length ) { return msg . createEmbed ( `📄 ${ msg . string ( "general.CONFIG_ASSIGNABLEROLES" ) }` , `${ guildconfig . assignableRoles . map ( ( role ) => `\`${ msg . channel . guild . roles . get ( role ) ?. name || role }\`` ) . join ( "," ) }` ) ; } let roles = await Promise . all ( args . join ( " " ) . split ( /(?:\s{0,},\s{0,})|\s/ ) . map ( async ( arg ) => { const role = this . bot . args . argtypes . role ( arg , msg ) ; if ( ! role ) return { added : false , role : undefined } ; if ( ! msg . member . roles ?. includes ( role . id ) ) return { added : false , role : role , doesntHave : true } ; if ( ! guildconfig . assignableRoles ?. includes ( role . id ) ) return { added : false , role : undefined } ; try { await msg . member . removeRole ( role . id , "Un self-assigned" ) ; return { removed : true , role : role } ; } catch ( err ) { return { removed : false , role : role } ; } } ) ) ; roles = roles . filter ( ( role ) => role . role !== undefined ) ; if ( ! roles . length ) return msg . createEmbed ( msg . string ( "global.ERROR" ) , msg . string ( "general.ASSIGN_NOROLES" ) , "error" ) ; if ( roles . every ( ( r ) => r . doesntHave === true ) ) { return msg . createEmbed ( msg . string ( "global.ERROR" ) , msg . string ( "global.ROLE_DOESNTHAVE" , { amount : roles . length } ) , "error" ) ; } if ( ! roles . length ) return msg . createEmbed ( msg . string ( "global.ERROR" ) , msg . string ( "general.ASSIGN_NOROLES" ) , "error" ) ; const failed = roles . filter ( ( r ) => r . removed === false ) ; const removed = roles . filter ( ( r ) => r . removed === true ) ; const failedField = [ ] ; if ( failed . length ) { failedField . push ( { name : msg . string ( "general.UNASSIGN_FAILED" ) , value : failed . map ( ( f ) => `\`${ f . role . name }\`` ) . join ( ", " ) , } ) ; } msg . channel . createMessage ( { embed : { title : msg . string ( "global.SUCCESS" ) , description : msg . string ( "general.UNASSIGN_UNASSIGNED" , { amount : removed . length , roles : removed . map ( ( r ) => `\`${ r . role . name }\`` ) . join ( ", " ) , } ) , color : msg . convertHex ( "success" ) , fields : failedField , footer : { text : msg . string ( "global.RAN_BY" , { author : msg . tagUser ( msg . author ) } ) , icon_url : msg . author . dynamicAvatarURL ( ) , } , } , } ) ; this . bot . emit ( "roleAssign" , msg . channel . guild , msg . author , null , null , removed . map ( ( r ) => `${ r . role . name }` ) ) ; } }