Skip to content

Commit

Permalink
fix(GuildMemberManager): options.roles on 'prune' (discordjs#4838)
Browse files Browse the repository at this point in the history
  • Loading branch information
izexi authored and GiorgioBrux committed Oct 17, 2020
1 parent a70771b commit 0444ae7
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/managers/GuildMemberManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,22 @@ class GuildMemberManager extends BaseManager {
* .then(pruned => console.log(`I just pruned ${pruned} people!`))
* .catch(console.error);
*/
prune({ days = 7, dry = false, count = true, roles = [], reason } = {}) {
prune({ days = 7, dry = false, count: compute_prune_count = true, roles = [], reason } = {}) {
if (typeof days !== 'number') throw new TypeError('PRUNE_DAYS_TYPE');

const query = new URLSearchParams();
query.set('days', days);
query.set('compute_prune_count', count);
const query = { days };
const resolvedRoles = [];

for (const role of roles) {
const resolvedRole = this.guild.roles.resolveID(role);
if (!resolvedRole) {
return Promise.reject(new TypeError('INVALID_TYPE', 'roles', 'Array of Roles or Snowflakes', true));
}
query.append('include_roles', role);
resolvedRoles.push(resolvedRole);
}

if (resolvedRoles.length) {
query.include_roles = dry ? resolvedRoles.join(',') : resolvedRoles;
}

const endpoint = this.client.api.guilds(this.guild.id).prune;
Expand All @@ -183,13 +186,12 @@ class GuildMemberManager extends BaseManager {
return endpoint.get({ query, reason }).then(data => data.pruned);
}

const body = [...query.entries()].reduce((acc, [k, v]) => {
if (k === 'include_roles') v = (acc[k] || []).concat(v);
acc[k] = v;
return acc;
}, {});

return endpoint.post({ data: body, reason }).then(data => data.pruned);
return endpoint
.post({
data: { ...query, compute_prune_count },
reason,
})
.then(data => data.pruned);
}

/**
Expand Down

0 comments on commit 0444ae7

Please sign in to comment.