Skip to content

Commit 13dc03c

Browse files
committed
fix: updating and removing global roles
1 parent 2138d67 commit 13dc03c

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

functions/roles.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,36 @@ const setupRoles = async (client, user, guildId, remove) => {
8989
}
9090
}
9191

92-
const updateRoles = async (client, discordId, guildId, remove = false) => {
92+
const updateRoles = async (client, discordId, guildIds, remove = false) => {
9393
let user, guilds
94+
guildIds = [guildIds].flat().filter(e => e)
9495

9596
if (discordId) {
9697
user = [await User.get(discordId)].flat()
9798
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)
99100
}
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)
101102
}
102103

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)
105105

106106
console.info(`Updating roles for ${guilds.length} guilds, user: ${discordId}, remove: ${remove}`)
107107

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)
111122
}
112123

113124
const logRoleUpdate = (client, member, role, guildDatas, playerElo, action) => {

0 commit comments

Comments
 (0)