mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
feat: GuildBanManager (#5276)
Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com> Co-authored-by: Jan <66554238+vaporox@users.noreply.github.com> Co-authored-by: izexi <43889168+izexi@users.noreply.github.com> Co-authored-by: Shubham Parihar <shubhamparihar391@gmail.com>
This commit is contained in:
@@ -10,6 +10,7 @@ const VoiceRegion = require('./VoiceRegion');
|
||||
const Webhook = require('./Webhook');
|
||||
const { Error, TypeError } = require('../errors');
|
||||
const GuildApplicationCommandManager = require('../managers/GuildApplicationCommandManager');
|
||||
const GuildBanManager = require('../managers/GuildBanManager');
|
||||
const GuildChannelManager = require('../managers/GuildChannelManager');
|
||||
const GuildEmojiManager = require('../managers/GuildEmojiManager');
|
||||
const GuildMemberManager = require('../managers/GuildMemberManager');
|
||||
@@ -61,6 +62,12 @@ class Guild extends Base {
|
||||
*/
|
||||
this.channels = new GuildChannelManager(this);
|
||||
|
||||
/**
|
||||
* A manager of the bans belonging to this guild
|
||||
* @type {GuildBanManager}
|
||||
*/
|
||||
this.bans = new GuildBanManager(this);
|
||||
|
||||
/**
|
||||
* A manager of the roles belonging to this guild
|
||||
* @type {RoleManager}
|
||||
@@ -632,50 +639,6 @@ class Guild extends Base {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* An object containing information about a guild member's ban.
|
||||
* @typedef {Object} BanInfo
|
||||
* @property {User} user User that was banned
|
||||
* @property {?string} reason Reason the user was banned
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fetches information on a banned user from this guild.
|
||||
* @param {UserResolvable} user The User to fetch the ban info of
|
||||
* @returns {Promise<BanInfo>}
|
||||
*/
|
||||
fetchBan(user) {
|
||||
const id = this.client.users.resolveID(user);
|
||||
if (!id) throw new Error('FETCH_BAN_RESOLVE_ID');
|
||||
return this.client.api
|
||||
.guilds(this.id)
|
||||
.bans(id)
|
||||
.get()
|
||||
.then(ban => ({
|
||||
reason: ban.reason,
|
||||
user: this.client.users.add(ban.user),
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a collection of banned users in this guild.
|
||||
* @returns {Promise<Collection<Snowflake, BanInfo>>}
|
||||
*/
|
||||
fetchBans() {
|
||||
return this.client.api
|
||||
.guilds(this.id)
|
||||
.bans.get()
|
||||
.then(bans =>
|
||||
bans.reduce((collection, ban) => {
|
||||
collection.set(ban.user.id, {
|
||||
reason: ban.reason,
|
||||
user: this.client.users.add(ban.user),
|
||||
});
|
||||
return collection;
|
||||
}, new Collection()),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a collection of integrations to this guild.
|
||||
* Resolves with a collection mapping integrations by their ids.
|
||||
|
||||
63
src/structures/GuildBan.js
Normal file
63
src/structures/GuildBan.js
Normal file
@@ -0,0 +1,63 @@
|
||||
'use strict';
|
||||
|
||||
const Base = require('./Base');
|
||||
|
||||
/**
|
||||
* Represents a ban in a guild on Discord.
|
||||
* @extends {Base}
|
||||
*/
|
||||
class GuildBan extends Base {
|
||||
/**
|
||||
* @param {Client} client The instantiating client
|
||||
* @param {Object} data The data for the ban
|
||||
* @param {Guild} guild The guild in which the ban is
|
||||
*/
|
||||
constructor(client, data, guild) {
|
||||
super(client);
|
||||
|
||||
/**
|
||||
* The guild in which the ban is
|
||||
* @type {Guild}
|
||||
*/
|
||||
this.guild = guild;
|
||||
|
||||
this._patch(data);
|
||||
}
|
||||
|
||||
_patch(data) {
|
||||
/**
|
||||
* The user this ban applies to
|
||||
* @type {User}
|
||||
*/
|
||||
this.user = this.client.users.add(data.user, true);
|
||||
|
||||
if ('reason' in data) {
|
||||
/**
|
||||
* The reason for the ban
|
||||
* @type {?string}
|
||||
*/
|
||||
this.reason = data.reason;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this GuildBan is a partial
|
||||
* If the reason is not provided the value is null
|
||||
* @type {boolean}
|
||||
* @readonly
|
||||
*/
|
||||
get partial() {
|
||||
return !('reason' in this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches this GuildBan.
|
||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
||||
* @returns {Promise<GuildBan>}
|
||||
*/
|
||||
fetch(force = false) {
|
||||
return this.guild.bans.fetch({ user: this.user, cache: true, force });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GuildBan;
|
||||
Reference in New Issue
Block a user