mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 12:33:30 +01:00
refactor(invite): make channel and inviter getters (#7278)
This commit is contained in:
@@ -9,8 +9,6 @@ const { Error } = require('../errors');
|
|||||||
const { Endpoints } = require('../util/Constants');
|
const { Endpoints } = require('../util/Constants');
|
||||||
const Permissions = require('../util/Permissions');
|
const Permissions = require('../util/Permissions');
|
||||||
|
|
||||||
// TODO: Convert `inviter` and `channel` in this class to a getter.
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an invitation to a guild channel.
|
* Represents an invitation to a guild channel.
|
||||||
* @extends {Base}
|
* @extends {Base}
|
||||||
@@ -116,20 +114,13 @@ class Invite extends Base {
|
|||||||
* @type {?Snowflake}
|
* @type {?Snowflake}
|
||||||
*/
|
*/
|
||||||
this.inviterId = data.inviter_id;
|
this.inviterId = data.inviter_id;
|
||||||
this.inviter = this.client.users.resolve(data.inviter_id);
|
|
||||||
} else {
|
} else {
|
||||||
this.inviterId ??= null;
|
this.inviterId ??= null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('inviter' in data) {
|
if ('inviter' in data) {
|
||||||
/**
|
this.client.users._add(data.inviter);
|
||||||
* The user who created this invite
|
|
||||||
* @type {?User}
|
|
||||||
*/
|
|
||||||
this.inviter ??= this.client.users._add(data.inviter);
|
|
||||||
this.inviterId = data.inviter.id;
|
this.inviterId = data.inviter.id;
|
||||||
} else {
|
|
||||||
this.inviter ??= null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('target_user' in data) {
|
if ('target_user' in data) {
|
||||||
@@ -165,18 +156,12 @@ class Invite extends Base {
|
|||||||
if ('channel_id' in data) {
|
if ('channel_id' in data) {
|
||||||
/**
|
/**
|
||||||
* The channel's id this invite is for
|
* The channel's id this invite is for
|
||||||
* @type {Snowflake}
|
* @type {?Snowflake}
|
||||||
*/
|
*/
|
||||||
this.channelId = data.channel_id;
|
this.channelId = data.channel_id;
|
||||||
this.channel = this.client.channels.cache.get(data.channel_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('channel' in data) {
|
if (data.channel) {
|
||||||
/**
|
|
||||||
* The channel this invite is for
|
|
||||||
* @type {Channel}
|
|
||||||
*/
|
|
||||||
this.channel ??= this.client.channels._add(data.channel, this.guild, { cache: false });
|
|
||||||
this.channelId ??= data.channel.id;
|
this.channelId ??= data.channel.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,6 +199,15 @@ class Invite extends Base {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The channel this invite is for
|
||||||
|
* @type {Channel}
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
get channel() {
|
||||||
|
return this.client.channels.resolve(this.channelId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The time the invite was created at
|
* The time the invite was created at
|
||||||
* @type {?Date}
|
* @type {?Date}
|
||||||
@@ -232,9 +226,9 @@ class Invite extends Base {
|
|||||||
const guild = this.guild;
|
const guild = this.guild;
|
||||||
if (!guild || !this.client.guilds.cache.has(guild.id)) return false;
|
if (!guild || !this.client.guilds.cache.has(guild.id)) return false;
|
||||||
if (!guild.me) throw new Error('GUILD_UNCACHED_ME');
|
if (!guild.me) throw new Error('GUILD_UNCACHED_ME');
|
||||||
return (
|
return Boolean(
|
||||||
this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS, false) ||
|
this.channel?.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS, false) ||
|
||||||
guild.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD)
|
guild.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,6 +253,15 @@ class Invite extends Base {
|
|||||||
return this.expiresTimestamp && new Date(this.expiresTimestamp);
|
return this.expiresTimestamp && new Date(this.expiresTimestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The user who created this invite
|
||||||
|
* @type {?User}
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
get inviter() {
|
||||||
|
return this.inviterId && this.client.users.resolve(this.inviterId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The URL to the invite
|
* The URL to the invite
|
||||||
* @type {string}
|
* @type {string}
|
||||||
|
|||||||
6
packages/discord.js/typings/index.d.ts
vendored
6
packages/discord.js/typings/index.d.ts
vendored
@@ -1368,8 +1368,8 @@ export class InteractionWebhook extends PartialWebhookMixin() {
|
|||||||
|
|
||||||
export class Invite extends Base {
|
export class Invite extends Base {
|
||||||
private constructor(client: Client, data: RawInviteData);
|
private constructor(client: Client, data: RawInviteData);
|
||||||
public channel: NonThreadGuildBasedChannel | PartialGroupDMChannel;
|
public readonly channel: NonThreadGuildBasedChannel | PartialGroupDMChannel;
|
||||||
public channelId: Snowflake;
|
public channelId: Snowflake | null;
|
||||||
public code: string;
|
public code: string;
|
||||||
public readonly deletable: boolean;
|
public readonly deletable: boolean;
|
||||||
public readonly createdAt: Date | null;
|
public readonly createdAt: Date | null;
|
||||||
@@ -1377,7 +1377,7 @@ export class Invite extends Base {
|
|||||||
public readonly expiresAt: Date | null;
|
public readonly expiresAt: Date | null;
|
||||||
public readonly expiresTimestamp: number | null;
|
public readonly expiresTimestamp: number | null;
|
||||||
public guild: InviteGuild | Guild | null;
|
public guild: InviteGuild | Guild | null;
|
||||||
public inviter: User | null;
|
public readonly inviter: User | null;
|
||||||
public inviterId: Snowflake | null;
|
public inviterId: Snowflake | null;
|
||||||
public maxAge: number | null;
|
public maxAge: number | null;
|
||||||
public maxUses: number | null;
|
public maxUses: number | null;
|
||||||
|
|||||||
Reference in New Issue
Block a user