Skip to content

Commit

Permalink
fix(GuildAuditLog): Remove Promises in constructor (#7089)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed Dec 14, 2021
1 parent 2ce244b commit 9cf44d1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
30 changes: 16 additions & 14 deletions src/structures/GuildAuditLogs.js
Expand Up @@ -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');

Expand Down Expand Up @@ -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 =
Expand Down
28 changes: 26 additions & 2 deletions src/structures/Invite.js
Expand Up @@ -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}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion typings/index.d.ts
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -4356,7 +4358,7 @@ export interface GuildAuditLogsEntryTargetField<TActionType extends GuildAuditLo
USER: User | null;
GUILD: Guild;
WEBHOOK: Webhook;
INVITE: Invite | { [x: string]: unknown };
INVITE: Invite;
MESSAGE: TActionType extends 'MESSAGE_BULK_DELETE' ? Guild | { id: Snowflake } : User;
INTEGRATION: Integration;
CHANNEL: GuildChannel | { id: Snowflake; [x: string]: unknown };
Expand Down

0 comments on commit 9cf44d1

Please sign in to comment.