fix(GuildAuditLog): Remove Promises in constructor (#7089)

This commit is contained in:
Jiralite
2021-12-14 18:03:21 +00:00
committed by GitHub
parent 2ce244b502
commit 9cf44d1c0e
3 changed files with 45 additions and 17 deletions

View File

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

View File

@@ -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) {

4
typings/index.d.ts vendored
View File

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