@@ -89,25 +89,36 @@ const setupRoles = async (client, user, guildId, remove) => {
89
89
}
90
90
}
91
91
92
- const updateRoles = async ( client , discordId , guildId , remove = false ) => {
92
+ const updateRoles = async ( client , discordId , guildIds , remove = false ) => {
93
93
let user , guilds
94
+ guildIds = [ guildIds ] . flat ( ) . filter ( e => e )
94
95
95
96
if ( discordId ) {
96
97
user = [ await User . get ( discordId ) ] . flat ( )
97
98
if ( user . length > 1 ) {
98
- user = user . filter ( e => guildId ? [ guildId ] . flat ( ) . includes ( e . guildId ) : true )
99
+ user = user . filter ( e => guildIds . length ? guildIds . includes ( e . guildId ) : true )
99
100
}
100
- guildId = user . map ( e => e . guildId ) . filter ( ( e , i , a ) => a . indexOf ( e ) === i )
101
+ guilds = user . map ( e => e . guildId ) . filter ( ( e , i , a ) => a . indexOf ( e ) === i ) . filter ( e => e )
101
102
}
102
103
103
- if ( guildId ) guilds = [ guildId ] . flat ( )
104
- else guilds = ( await GuildCustomRole . getAll ( ) ) . map ( e => e . guildId ) . filter ( ( e , i , a ) => a . indexOf ( e ) === i )
104
+ if ( ! guilds . length ) guilds = ( await GuildCustomRole . getAll ( ) ) . map ( e => e . guildId ) . filter ( ( e , i , a ) => a . indexOf ( e ) === i )
105
105
106
106
console . info ( `Updating roles for ${ guilds . length } guilds, user: ${ discordId } , remove: ${ remove } ` )
107
107
108
- Promise . all ( guilds . map ( async guild => await setupRoles ( client , user , guild , remove ) . catch ( console . error ) ) )
109
- . then ( ( ) => { if ( remove ) User . remove ( discordId , guildId ) } )
110
- . catch ( console . error )
108
+ Promise . all ( guilds . map ( async guildId => {
109
+ await setupRoles ( client , user , guildId , remove ) . catch ( console . error )
110
+ return guildId
111
+ } ) ) . then ( ( guildIdsProcessed ) => {
112
+ if ( remove ) {
113
+ if ( ! guildIds . length ) User . remove ( discordId , null , true )
114
+ . then ( ( ) => console . info ( `User ${ discordId } removed from all guilds` ) )
115
+ else {
116
+ guildIdsProcessed . forEach ( guildId => {
117
+ User . remove ( discordId , guildId ) . then ( ( ) => console . info ( `User ${ discordId } removed from guild: ${ guildId } ` ) )
118
+ } )
119
+ }
120
+ }
121
+ } ) . catch ( console . error )
111
122
}
112
123
113
124
const logRoleUpdate = ( client , member , role , guildDatas , playerElo , action ) => {
0 commit comments