mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
refactor(GuildAuditLogsEntry): abstract reduce logic into a new function (#9845)
This commit is contained in:
@@ -83,6 +83,20 @@ const Targets = {
|
||||
* @typedef {string} AuditLogTargetType
|
||||
*/
|
||||
|
||||
/**
|
||||
* Contructs an object of known properties for a structure from an array of changes.
|
||||
* @param {AuditLogChange[]} changes The array of changes
|
||||
* @param {Object} [initialData={}] The initial data passed to the function
|
||||
* @returns {Object}
|
||||
* @ignore
|
||||
*/
|
||||
function changesReduce(changes, initialData = {}) {
|
||||
return changes.reduce((accumulator, change) => {
|
||||
accumulator[change.key] = change.new ?? change.old;
|
||||
return accumulator;
|
||||
}, initialData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Audit logs entry.
|
||||
*/
|
||||
@@ -268,10 +282,7 @@ class GuildAuditLogsEntry {
|
||||
*/
|
||||
this.target = null;
|
||||
if (targetType === Targets.Unknown) {
|
||||
this.target = this.changes.reduce((o, c) => {
|
||||
o[c.key] = c.new ?? c.old;
|
||||
return o;
|
||||
}, {});
|
||||
this.target = changesReduce(this.changes);
|
||||
this.target.id = data.target_id;
|
||||
// MemberDisconnect and similar types do not provide a target_id.
|
||||
} else if (targetType === Targets.User && data.target_id) {
|
||||
@@ -285,33 +296,16 @@ class GuildAuditLogsEntry {
|
||||
logs?.webhooks.get(data.target_id) ??
|
||||
new Webhook(
|
||||
guild.client,
|
||||
this.changes.reduce(
|
||||
(o, c) => {
|
||||
o[c.key] = c.new ?? c.old;
|
||||
return o;
|
||||
},
|
||||
{
|
||||
id: data.target_id,
|
||||
guild_id: guild.id,
|
||||
},
|
||||
),
|
||||
changesReduce(this.changes, {
|
||||
id: data.target_id,
|
||||
guild_id: guild.id,
|
||||
}),
|
||||
);
|
||||
} else if (targetType === Targets.Invite) {
|
||||
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 },
|
||||
),
|
||||
);
|
||||
this.target = guild.invites.cache.get(change) ?? new Invite(guild.client, changesReduce(this.changes, { guild }));
|
||||
} else if (targetType === Targets.Message) {
|
||||
// Discord sends a channel id for the MessageBulkDelete action type.
|
||||
this.target =
|
||||
@@ -321,70 +315,28 @@ class GuildAuditLogsEntry {
|
||||
} else if (targetType === Targets.Integration) {
|
||||
this.target =
|
||||
logs?.integrations.get(data.target_id) ??
|
||||
new Integration(
|
||||
guild.client,
|
||||
this.changes.reduce(
|
||||
(o, c) => {
|
||||
o[c.key] = c.new ?? c.old;
|
||||
return o;
|
||||
},
|
||||
{ id: data.target_id },
|
||||
),
|
||||
guild,
|
||||
);
|
||||
new Integration(guild.client, changesReduce(this.changes, { id: data.target_id }), guild);
|
||||
} else if (targetType === Targets.Channel || targetType === Targets.Thread) {
|
||||
this.target =
|
||||
guild.channels.cache.get(data.target_id) ??
|
||||
this.changes.reduce(
|
||||
(o, c) => {
|
||||
o[c.key] = c.new ?? c.old;
|
||||
return o;
|
||||
},
|
||||
{ id: data.target_id },
|
||||
);
|
||||
this.target = guild.channels.cache.get(data.target_id) ?? changesReduce(this.changes, { id: data.target_id });
|
||||
} else if (targetType === Targets.StageInstance) {
|
||||
this.target =
|
||||
guild.stageInstances.cache.get(data.target_id) ??
|
||||
new StageInstance(
|
||||
guild.client,
|
||||
this.changes.reduce(
|
||||
(o, c) => {
|
||||
o[c.key] = c.new ?? c.old;
|
||||
return o;
|
||||
},
|
||||
{
|
||||
id: data.target_id,
|
||||
channel_id: data.options?.channel_id,
|
||||
guild_id: guild.id,
|
||||
},
|
||||
),
|
||||
changesReduce(this.changes, {
|
||||
id: data.target_id,
|
||||
channel_id: data.options?.channel_id,
|
||||
guild_id: guild.id,
|
||||
}),
|
||||
);
|
||||
} else if (targetType === Targets.Sticker) {
|
||||
this.target =
|
||||
guild.stickers.cache.get(data.target_id) ??
|
||||
new Sticker(
|
||||
guild.client,
|
||||
this.changes.reduce(
|
||||
(o, c) => {
|
||||
o[c.key] = c.new ?? c.old;
|
||||
return o;
|
||||
},
|
||||
{ id: data.target_id },
|
||||
),
|
||||
);
|
||||
new Sticker(guild.client, changesReduce(this.changes, { id: data.target_id }));
|
||||
} else if (targetType === Targets.GuildScheduledEvent) {
|
||||
this.target =
|
||||
guild.scheduledEvents.cache.get(data.target_id) ??
|
||||
new GuildScheduledEvent(
|
||||
guild.client,
|
||||
this.changes.reduce(
|
||||
(o, c) => {
|
||||
o[c.key] = c.new ?? c.old;
|
||||
return o;
|
||||
},
|
||||
{ id: data.target_id, guild_id: guild.id },
|
||||
),
|
||||
);
|
||||
new GuildScheduledEvent(guild.client, changesReduce(this.changes, { id: data.target_id, guild_id: guild.id }));
|
||||
} else if (targetType === Targets.ApplicationCommand) {
|
||||
this.target = logs?.applicationCommands.get(data.target_id) ?? { id: data.target_id };
|
||||
} else if (targetType === Targets.AutoModeration) {
|
||||
@@ -392,13 +344,7 @@ class GuildAuditLogsEntry {
|
||||
guild.autoModerationRules.cache.get(data.target_id) ??
|
||||
new AutoModerationRule(
|
||||
guild.client,
|
||||
this.changes.reduce(
|
||||
(o, c) => {
|
||||
o[c.key] = c.new ?? c.old;
|
||||
return o;
|
||||
},
|
||||
{ id: data.target_id, guild_id: guild.id },
|
||||
),
|
||||
changesReduce(this.changes, { id: data.target_id, guild_id: guild.id }),
|
||||
guild,
|
||||
);
|
||||
} else if (data.target_id) {
|
||||
|
||||
Reference in New Issue
Block a user