mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 20:43:30 +01:00
feat(GuildBanManager): Add deleteMessageSeconds (#8575)
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const process = require('node:process');
|
||||||
const { Collection } = require('@discordjs/collection');
|
const { Collection } = require('@discordjs/collection');
|
||||||
const CachedManager = require('./CachedManager');
|
const CachedManager = require('./CachedManager');
|
||||||
const { TypeError, Error } = require('../errors');
|
const { TypeError, Error } = require('../errors');
|
||||||
const GuildBan = require('../structures/GuildBan');
|
const GuildBan = require('../structures/GuildBan');
|
||||||
const { GuildMember } = require('../structures/GuildMember');
|
const { GuildMember } = require('../structures/GuildMember');
|
||||||
|
|
||||||
|
let deprecationEmittedForDays = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages API methods for GuildBans and stores their cache.
|
* Manages API methods for GuildBans and stores their cache.
|
||||||
* @extends {CachedManager}
|
* @extends {CachedManager}
|
||||||
@@ -126,6 +129,9 @@ class GuildBanManager extends CachedManager {
|
|||||||
* Options used to ban a user from a guild.
|
* Options used to ban a user from a guild.
|
||||||
* @typedef {Object} BanOptions
|
* @typedef {Object} BanOptions
|
||||||
* @property {number} [days=0] Number of days of messages to delete, must be between 0 and 7, inclusive
|
* @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
|
* @property {string} [reason] The reason for the ban
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -142,15 +148,30 @@ class GuildBanManager extends CachedManager {
|
|||||||
* .then(banInfo => console.log(`Banned ${banInfo.user?.tag ?? banInfo.tag ?? banInfo}`))
|
* .then(banInfo => console.log(`Banned ${banInfo.user?.tag ?? banInfo.tag ?? banInfo}`))
|
||||||
* .catch(console.error);
|
* .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);
|
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
|
||||||
const id = this.client.users.resolveId(user);
|
const id = this.client.users.resolveId(user);
|
||||||
if (!id) throw new Error('BAN_RESOLVE_ID', true);
|
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
|
await this.client.api
|
||||||
.guilds(this.guild.id)
|
.guilds(this.guild.id)
|
||||||
.bans(id)
|
.bans(id)
|
||||||
.put({
|
.put({
|
||||||
data: { delete_message_days: options.days },
|
data: {
|
||||||
|
delete_message_seconds:
|
||||||
|
typeof options.deleteMessageSeconds !== 'undefined'
|
||||||
|
? options.deleteMessageSeconds
|
||||||
|
: (options.days ?? 0) * 24 * 60 * 60,
|
||||||
|
},
|
||||||
reason: options.reason,
|
reason: options.reason,
|
||||||
});
|
});
|
||||||
if (user instanceof GuildMember) return user;
|
if (user instanceof GuildMember) return user;
|
||||||
|
|||||||
@@ -379,7 +379,7 @@ class GuildMemberManager extends CachedManager {
|
|||||||
* .then(banInfo => console.log(`Banned ${banInfo.user?.tag ?? banInfo.tag ?? banInfo}`))
|
* .then(banInfo => console.log(`Banned ${banInfo.user?.tag ?? banInfo.tag ?? banInfo}`))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
ban(user, options = { days: 0 }) {
|
ban(user, options) {
|
||||||
return this.guild.bans.create(user, options);
|
return this.guild.bans.create(user, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -378,8 +378,8 @@ class GuildMember extends Base {
|
|||||||
* @param {BanOptions} [options] Options for the ban
|
* @param {BanOptions} [options] Options for the ban
|
||||||
* @returns {Promise<GuildMember>}
|
* @returns {Promise<GuildMember>}
|
||||||
* @example
|
* @example
|
||||||
* // ban a guild member
|
* // Ban a guild member, deleting a week's worth of messages
|
||||||
* guildMember.ban({ days: 7, reason: 'They deserved it' })
|
* guildMember.ban({ deleteMessageSeconds: 60 * 60 * 24 * 7, reason: 'They deserved it' })
|
||||||
* .then(console.log)
|
* .then(console.log)
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
|
|||||||
2
typings/index.d.ts
vendored
2
typings/index.d.ts
vendored
@@ -4034,7 +4034,9 @@ export interface AwaitReactionsOptions extends ReactionCollectorOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface BanOptions {
|
export interface BanOptions {
|
||||||
|
/** @deprecated Use {@link deleteMessageSeconds} instead. */
|
||||||
days?: number;
|
days?: number;
|
||||||
|
deleteMessageSeconds?: number;
|
||||||
reason?: string;
|
reason?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user