Skip to content

Commit

Permalink
refactor(GuildAuditLogsEntry): abstract reduce logic into a new funct…
Browse files Browse the repository at this point in the history
…ion (#9845)
  • Loading branch information
sdanialraza committed Sep 23, 2023
1 parent 1e5c14b commit 19ea0ba
Showing 1 changed file with 30 additions and 84 deletions.
114 changes: 30 additions & 84 deletions packages/discord.js/src/structures/GuildAuditLogsEntry.js
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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) {
Expand All @@ -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 =
Expand All @@ -321,84 +315,36 @@ 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) {
this.target =
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) {
Expand Down

0 comments on commit 19ea0ba

Please sign in to comment.