diff --git a/packages/discord.js/src/structures/Invite.js b/packages/discord.js/src/structures/Invite.js index 9fa3e432f..77c96c726 100644 --- a/packages/discord.js/src/structures/Invite.js +++ b/packages/discord.js/src/structures/Invite.js @@ -9,8 +9,6 @@ const { Error } = require('../errors'); const { Endpoints } = require('../util/Constants'); const Permissions = require('../util/Permissions'); -// TODO: Convert `inviter` and `channel` in this class to a getter. - /** * Represents an invitation to a guild channel. * @extends {Base} @@ -116,20 +114,13 @@ class Invite extends Base { * @type {?Snowflake} */ this.inviterId = data.inviter_id; - this.inviter = this.client.users.resolve(data.inviter_id); } else { this.inviterId ??= null; } if ('inviter' in data) { - /** - * The user who created this invite - * @type {?User} - */ - this.inviter ??= this.client.users._add(data.inviter); + this.client.users._add(data.inviter); this.inviterId = data.inviter.id; - } else { - this.inviter ??= null; } if ('target_user' in data) { @@ -165,18 +156,12 @@ class Invite extends Base { if ('channel_id' in data) { /** * The channel's id this invite is for - * @type {Snowflake} + * @type {?Snowflake} */ this.channelId = data.channel_id; - this.channel = this.client.channels.cache.get(data.channel_id); } - if ('channel' in data) { - /** - * The channel this invite is for - * @type {Channel} - */ - this.channel ??= this.client.channels._add(data.channel, this.guild, { cache: false }); + if (data.channel) { 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 * @type {?Date} @@ -232,9 +226,9 @@ class Invite extends Base { const guild = this.guild; if (!guild || !this.client.guilds.cache.has(guild.id)) return false; if (!guild.me) throw new Error('GUILD_UNCACHED_ME'); - return ( - this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS, false) || - guild.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD) + return Boolean( + this.channel?.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS, false) || + guild.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD), ); } @@ -259,6 +253,15 @@ class Invite extends Base { 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 * @type {string} diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 32ca70672..ff352b3c7 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -1368,8 +1368,8 @@ export class InteractionWebhook extends PartialWebhookMixin() { export class Invite extends Base { private constructor(client: Client, data: RawInviteData); - public channel: NonThreadGuildBasedChannel | PartialGroupDMChannel; - public channelId: Snowflake; + public readonly channel: NonThreadGuildBasedChannel | PartialGroupDMChannel; + public channelId: Snowflake | null; public code: string; public readonly deletable: boolean; public readonly createdAt: Date | null; @@ -1377,7 +1377,7 @@ export class Invite extends Base { public readonly expiresAt: Date | null; public readonly expiresTimestamp: number | null; public guild: InviteGuild | Guild | null; - public inviter: User | null; + public readonly inviter: User | null; public inviterId: Snowflake | null; public maxAge: number | null; public maxUses: number | null;