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

feat(GuildBanManager): Add deleteMessageSeconds #8575

Merged
merged 1 commit into from Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/managers/GuildBanManager.js
@@ -1,11 +1,14 @@
'use strict';

const process = require('node:process');
const { Collection } = require('@discordjs/collection');
const CachedManager = require('./CachedManager');
const { TypeError, Error } = require('../errors');
const GuildBan = require('../structures/GuildBan');
const { GuildMember } = require('../structures/GuildMember');

let deprecationEmittedForDays = false;

/**
* Manages API methods for GuildBans and stores their cache.
* @extends {CachedManager}
Expand Down Expand Up @@ -126,6 +129,9 @@ class GuildBanManager extends CachedManager {
* Options used to ban a user from a guild.
* @typedef {Object} BanOptions
* @property {number} [days=0] Number of days of messages to delete, must be between 0 and 7, inclusive
* <warn>This property is deprecated. Use `deleteMessageSeconds` instead.</warn>
* @property {number} [deleteMessageSeconds] Number of seconds of messages to delete,
* must be between 0 and 604800 (7 days), inclusive
* @property {string} [reason] The reason for the ban
*/

Expand All @@ -142,15 +148,30 @@ class GuildBanManager extends CachedManager {
* .then(banInfo => console.log(`Banned ${banInfo.user?.tag ?? banInfo.tag ?? banInfo}`))
* .catch(console.error);
*/
async create(user, options = { days: 0 }) {
async create(user, options = {}) {
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
const id = this.client.users.resolveId(user);
if (!id) throw new Error('BAN_RESOLVE_ID', true);

if (typeof options.days !== 'undefined' && !deprecationEmittedForDays) {
process.emitWarning(
'The days option for GuildBanManager#create() is deprecated. Use the deleteMessageSeconds option instead.',
'DeprecationWarning',
);

deprecationEmittedForDays = true;
}

await this.client.api
.guilds(this.guild.id)
.bans(id)
.put({
data: { delete_message_days: options.days },
data: {
delete_message_seconds:
vladfrangu marked this conversation as resolved.
Show resolved Hide resolved
typeof options.deleteMessageSeconds !== 'undefined'
? options.deleteMessageSeconds
: (options.days ?? 0) * 24 * 60 * 60,
},
reason: options.reason,
});
if (user instanceof GuildMember) return user;
Expand Down
2 changes: 1 addition & 1 deletion src/managers/GuildMemberManager.js
Expand Up @@ -379,7 +379,7 @@ class GuildMemberManager extends CachedManager {
* .then(banInfo => console.log(`Banned ${banInfo.user?.tag ?? banInfo.tag ?? banInfo}`))
* .catch(console.error);
*/
ban(user, options = { days: 0 }) {
ban(user, options) {
return this.guild.bans.create(user, options);
}

Expand Down
4 changes: 2 additions & 2 deletions src/structures/GuildMember.js
Expand Up @@ -378,8 +378,8 @@ class GuildMember extends Base {
* @param {BanOptions} [options] Options for the ban
* @returns {Promise<GuildMember>}
* @example
* // ban a guild member
* guildMember.ban({ days: 7, reason: 'They deserved it' })
* // Ban a guild member, deleting a week's worth of messages
* guildMember.ban({ deleteMessageSeconds: 60 * 60 * 24 * 7, reason: 'They deserved it' })
* .then(console.log)
* .catch(console.error);
*/
Expand Down
2 changes: 2 additions & 0 deletions typings/index.d.ts
Expand Up @@ -4034,7 +4034,9 @@ export interface AwaitReactionsOptions extends ReactionCollectorOptions {
}

export interface BanOptions {
/** @deprecated Use {@link deleteMessageSeconds} instead. */
days?: number;
deleteMessageSeconds?: number;
reason?: string;
}

Expand Down