Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GuildMemberManager): options.roles on 'prune' #4838

Merged
merged 1 commit into from
Oct 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/managers/GuildMemberManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,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 @@ -182,13 +185,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