refactor(invite): make channel and inviter getters (#7278)

This commit is contained in:
Almeida
2022-01-17 12:51:34 +00:00
committed by GitHub
parent c90e47f904
commit 18b0ed4cbe
2 changed files with 27 additions and 24 deletions

View File

@@ -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}

View File

@@ -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;