Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AutoModerationActionExecution): transform action #9111

Merged
merged 4 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { _transformAPIAutoModerationAction } = require('../util/Transformers');

/**
* Represents the structure of an executed action when an {@link AutoModerationRule} is triggered.
*/
Expand All @@ -15,7 +17,7 @@ class AutoModerationActionExecution {
* The action that was executed.
* @type {AutoModerationAction}
*/
this.action = data.action;
this.action = _transformAPIAutoModerationAction(data.action);

/**
* The id of the auto moderation rule this action belongs to.
Expand Down
9 changes: 2 additions & 7 deletions packages/discord.js/src/structures/AutoModerationRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { Collection } = require('@discordjs/collection');
const Base = require('./Base');
const { _transformAPIAutoModerationAction } = require('../util/Transformers');

/**
* Represents an auto moderation rule.
Expand Down Expand Up @@ -101,13 +102,7 @@ class AutoModerationRule extends Base {
* The actions of this auto moderation rule.
* @type {AutoModerationAction[]}
*/
this.actions = data.actions.map(action => ({
type: action.type,
metadata: {
durationSeconds: action.metadata.duration_seconds ?? null,
channelId: action.metadata.channel_id ?? null,
},
}));
this.actions = data.actions.map(action => _transformAPIAutoModerationAction(action));
}

if ('enabled' in data) {
Expand Down
5 changes: 5 additions & 0 deletions packages/discord.js/src/util/APITypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
* @see {@link https://discord-api-types.dev/api/discord-api-types-v10#APIApplicationCommandOption}
*/

/**
* @external APIAutoModerationAction
* @see {@link https://discord-api-types.dev/api/discord-api-types-v10/interface/APIAutoModerationAction}
*/

/**
* @external APIButtonComponent
* @see {@link https://discord-api-types.dev/api/discord-api-types-v10#APIButtonComponent}
Expand Down
18 changes: 17 additions & 1 deletion packages/discord.js/src/util/Transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,20 @@ function toSnakeCase(obj) {
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [snakeCase(key), toSnakeCase(value)]));
}

module.exports = { toSnakeCase };
/**
* Transforms an API auto moderation action object to a camel-cased variant.
* @param {APIAutoModerationAction} autoModerationAction The action to transform
* @returns {AutoModerationAction}
* @ignore
*/
function _transformAPIAutoModerationAction(autoModerationAction) {
return {
type: autoModerationAction.type,
metadata: {
durationSeconds: autoModerationAction.metadata.duration_seconds ?? null,
channelId: autoModerationAction.metadata.channel_id ?? null,
},
};
}

module.exports = { toSnakeCase, _transformAPIAutoModerationAction };