-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: allow discord.js builders to accept camelCase (#7424)
- Loading branch information
1 parent
f495364
commit 94bf727
Showing
10 changed files
with
313 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
const { ActionRow: BuildersActionRow } = require('@discordjs/builders'); | ||
const Components = require('../util/Components'); | ||
|
||
class ActionRow extends BuildersActionRow { | ||
constructor(data) { | ||
// TODO: Simplify when getters PR is merged. | ||
const initData = Components.transformJSON(data); | ||
super({ ...initData, components: initData.components ?? [] }); | ||
} | ||
} | ||
|
||
module.exports = ActionRow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const { ButtonComponent: BuildersButtonComponent } = require('@discordjs/builders'); | ||
const Components = require('../util/Components'); | ||
|
||
class ButtonComponent extends BuildersButtonComponent { | ||
constructor(data) { | ||
super(Components.transformJSON(data)); | ||
} | ||
} | ||
|
||
module.exports = ButtonComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const { Embed: BuildersEmbed } = require('@discordjs/builders'); | ||
const Embeds = require('../util/Embeds'); | ||
|
||
class Embed extends BuildersEmbed { | ||
constructor(data) { | ||
super({ ...Embeds.transformJSON(data) }); | ||
} | ||
} | ||
|
||
module.exports = Embed; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const { SelectMenuComponent: BuildersSelectMenuComponent } = require('@discordjs/builders'); | ||
const Components = require('../util/Components'); | ||
|
||
class SelectMenuComponent extends BuildersSelectMenuComponent { | ||
constructor(data) { | ||
super(Components.transformJSON(data)); | ||
} | ||
} | ||
|
||
module.exports = SelectMenuComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @typedef {Object} BaseComponentData | ||
* @property {ComponentType} type | ||
*/ | ||
|
||
/** | ||
* @typedef {BaseComponentData} ActionRowData | ||
* @property {ComponentData[]} components | ||
*/ | ||
|
||
/** | ||
* @typedef {BaseComponentData} ButtonComponentData | ||
* @property {ButtonStyle} style | ||
* @property {?boolean} disabled | ||
* @property {string} label | ||
* @property {?APIComponentEmoji} emoji | ||
* @property {?string} customId | ||
* @property {?string} url | ||
*/ | ||
|
||
/** | ||
* @typedef {object} SelectMenuComponentOptionData | ||
* @property {string} label | ||
* @property {string} value | ||
* @property {?string} description | ||
* @property {?APIComponentEmoji} emoji | ||
* @property {?boolean} default | ||
*/ | ||
|
||
/** | ||
* @typedef {BaseComponentData} SelectMenuComponentData | ||
* @property {string} customId | ||
* @property {?boolean} disabled | ||
* @property {?number} maxValues | ||
* @property {?number} minValues | ||
* @property {?SelectMenuComponentOptionData[]} options | ||
* @property {?string} placeholder | ||
*/ | ||
|
||
/** | ||
* @typedef {ActionRowData|ButtonComponentData|SelectMenuComponentData} ComponentData | ||
*/ | ||
|
||
class Components extends null { | ||
/** | ||
* Transforms json data into api-compatible json data. | ||
* @param {ComponentData|APIMessageComponent} data The data to transform. | ||
* @returns {APIMessageComponentData} | ||
*/ | ||
static transformJSON(data) { | ||
return { | ||
type: data?.type, | ||
custom_id: data?.customId ?? data?.custom_id, | ||
disabled: data?.disabled, | ||
style: data?.style, | ||
label: data?.label, | ||
emoji: data?.emoji, | ||
url: data?.url, | ||
options: data?.options, | ||
placeholder: data?.placeholder, | ||
min_values: data?.minValues ?? data?.min_values, | ||
max_values: data?.maxValues ?? data?.max_values, | ||
components: data?.components?.map(c => Components.transformJSON(c)), | ||
}; | ||
} | ||
} | ||
|
||
module.exports = Components; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @typedef {Object} EmbedData | ||
* @property {?string} title | ||
* @property {?EmbedType} type | ||
* @property {?string} description | ||
* @property {?string} url | ||
* @property {?string} timestamp | ||
* @property {?number} color | ||
* @property {?EmbedFooterData} footer | ||
* @property {?EmbedImageData} image | ||
* @property {?EmbedImageData} thumbnail | ||
* @property {?EmbedProviderData} provider | ||
* @property {?EmbedAuthorData} author | ||
* @property {?EmbedFieldData[]} fields | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} EmbedFooterData | ||
* @property {string} text | ||
* @property {?string} iconURL | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} EmbedImageData | ||
* @property {?string} url | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} EmbedProviderData | ||
* @property {?string} name | ||
* @property {?string} url | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} EmbedAuthorData | ||
* @property {string} name | ||
* @property {?string} url | ||
* @property {?string} iconURL | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} EmbedFieldData | ||
* @property {string} name | ||
* @property {string} value | ||
* @property {?boolean} inline | ||
*/ | ||
|
||
class Embeds extends null { | ||
/** | ||
* Transforms json data into api-compatible json data. | ||
* @param {EmbedData|APIEmbed} data The data to transform. | ||
* @returns {APIEmbed} | ||
*/ | ||
static transformJSON(data) { | ||
return { | ||
title: data?.title, | ||
type: data?.type, | ||
description: data?.description, | ||
url: data?.url, | ||
timestamp: data?.timestamp, | ||
color: data?.color, | ||
footer: { | ||
test: data?.footer?.text, | ||
icon_url: data?.footer?.iconURL ?? data?.footer?.icon_url, | ||
}, | ||
image: data?.image, | ||
thumbnail: data?.thumbnail, | ||
provider: data?.provider, | ||
author: { | ||
name: data?.author?.name, | ||
text: data?.author?.text, | ||
icon_url: data?.author?.iconURL ?? data?.author?.icon_url, | ||
}, | ||
fields: data?.fields, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = Embeds; |
Oops, something went wrong.