diff --git a/src/structures/GuildAuditLogs.js b/src/structures/GuildAuditLogs.js index b035d422f..364630d7f 100644 --- a/src/structures/GuildAuditLogs.js +++ b/src/structures/GuildAuditLogs.js @@ -2,11 +2,11 @@ const { Collection } = require('@discordjs/collection'); const Integration = require('./Integration'); +const Invite = require('./Invite'); const { StageInstance } = require('./StageInstance'); const { Sticker } = require('./Sticker'); const Webhook = require('./Webhook'); const { OverwriteTypes, PartialTypes } = require('../util/Constants'); -const Permissions = require('../util/Permissions'); const SnowflakeUtil = require('../util/SnowflakeUtil'); const Util = require('../util/Util'); @@ -502,19 +502,21 @@ class GuildAuditLogsEntry { ), ); } else if (targetType === Targets.INVITE) { - this.target = guild.members.fetch(guild.client.user.id).then(async me => { - if (me.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) { - let change = this.changes.find(c => c.key === 'code'); - change = change.new ?? change.old; - const invites = await guild.invites.fetch(); - this.target = invites.find(i => i.code === change) ?? null; - } else { - this.target = this.changes.reduce((o, c) => { - o[c.key] = c.new ?? c.old; - return o; - }, {}); - } - }); + let change = this.changes.find(c => c.key === 'code'); + change = change.new ?? change.old; + + this.target = + guild.invites.cache.get(change) ?? + new Invite( + guild.client, + this.changes.reduce( + (o, c) => { + o[c.key] = c.new ?? c.old; + return o; + }, + { guild }, + ), + ); } else if (targetType === Targets.MESSAGE) { // Discord sends a channel id for the MESSAGE_BULK_DELETE action type. this.target = diff --git a/src/structures/Invite.js b/src/structures/Invite.js index fd380d316..2826f5c81 100644 --- a/src/structures/Invite.js +++ b/src/structures/Invite.js @@ -7,6 +7,8 @@ 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} @@ -106,12 +108,24 @@ class Invite extends Base { this.maxUses ??= null; } + if ('inviter_id' in data) { + /** + * The user's id who created this invite + * @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.inviter ??= this.client.users._add(data.inviter); + this.inviterId = data.inviter.id; } else { this.inviter ??= null; } @@ -154,12 +168,22 @@ class Invite extends Base { this.targetType ??= null; } + if ('channel_id' in data) { + /** + * The channel's id this invite is for + * @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 }); + this.channel ??= this.client.channels._add(data.channel, this.guild, { cache: false }); + this.channelId ??= data.channel.id; } if ('created_at' in data) { diff --git a/typings/index.d.ts b/typings/index.d.ts index b47fcac82..cc8d02d83 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1302,6 +1302,7 @@ export class InteractionWebhook extends PartialWebhookMixin() { export class Invite extends Base { private constructor(client: Client, data: RawInviteData); public channel: GuildChannel | PartialGroupDMChannel; + public channelId: Snowflake; public code: string; public readonly deletable: boolean; public readonly createdAt: Date | null; @@ -1310,6 +1311,7 @@ export class Invite extends Base { public readonly expiresTimestamp: number | null; public guild: InviteGuild | Guild | null; public inviter: User | null; + public inviterId: Snowflake | null; public maxAge: number | null; public maxUses: number | null; public memberCount: number; @@ -4356,7 +4358,7 @@ export interface GuildAuditLogsEntryTargetField