feat(MessageButton): builder and associated interfaces#6
feat(MessageButton): builder and associated interfaces#6monbrey wants to merge 1 commit intodiscordjs:mainfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## main #6 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 1 2 +1
Lines 12 61 +49
Branches 4 19 +15
=========================================
+ Hits 12 61 +49
Continue to review full report at Codecov.
|
iCrawl
left a comment
There was a problem hiding this comment.
Depending on how we decide for v13 or not, ignore the customId changes 👍
| * The text to be displayed on this button | ||
| * @type {?string} | ||
| */ | ||
| public customID?: string; |
There was a problem hiding this comment.
| public customID?: string; | |
| public customId?: string; |
There was a problem hiding this comment.
We'll not use this in v13 anyway, right? cc @discordjs/the-big-3
Otherwise we'll have to change this once we go to the rewrite, which is a bit iffy I feel.
There was a problem hiding this comment.
We can always break this for v13, depends on when you want to release it.
There was a problem hiding this comment.
I opened discordjs/discord.js#6036 a few hours ago, so depending on whether we go for ID or Id, depends the resolution of that PR for v13.
There was a problem hiding this comment.
Yup, merged, we can move forward with that change.
| */ | ||
| export class MessageButton { | ||
| /** | ||
| * The text to be displayed on this button |
| * @param {string} customID A unique string to be sent in the interaction when clicked | ||
| * @returns {MessageButton} | ||
| */ | ||
| public setCustomID(customID: string): this { |
There was a problem hiding this comment.
| public setCustomID(customID: string): this { | |
| public setCustomId(customId: string): this { |
| } | ||
|
|
||
| /** | ||
| * Sets the custom ID of this button |
There was a problem hiding this comment.
| * Sets the custom ID of this button | |
| * Sets the custom id of this button |
|
|
||
| /** | ||
| * Sets the custom ID of this button | ||
| * @param {string} customID A unique string to be sent in the interaction when clicked |
There was a problem hiding this comment.
| * @param {string} customID A unique string to be sent in the interaction when clicked | |
| * @param customId A unique string to be sent in the interaction when clicked |
In TS we don't need to document param types.
| label: 'discord.js', | ||
| style: 'Primary', | ||
| }); | ||
| expect(button.customID).toBe('discord.js'); |
There was a problem hiding this comment.
| expect(button.customID).toBe('discord.js'); | |
| expect(button.customId).toBe('discord.js'); |
| describe('constructor', () => { | ||
| test('GIVEN data THEN class properties are set', () => { | ||
| const button = new MessageButton({ | ||
| customID: 'discord.js', |
There was a problem hiding this comment.
| customID: 'discord.js', | |
| customId: 'discord.js', |
| expect(button.style).toBe(1); | ||
| }); | ||
|
|
||
| test('GIVEN raw data THEN customID is set to custom_id', () => { |
There was a problem hiding this comment.
| test('GIVEN raw data THEN customID is set to custom_id', () => { | |
| test('GIVEN raw data THEN customId is set to custom_id', () => { |
| custom_id: 'discord.js', | ||
| style: 2, | ||
| }); | ||
| expect(button.customID).toBe('discord.js'); |
There was a problem hiding this comment.
| expect(button.customID).toBe('discord.js'); | |
| expect(button.customId).toBe('discord.js'); |
| expect( | ||
| new MessageButton({ | ||
| style: 'Primary', | ||
| customID: 'discord.js', |
There was a problem hiding this comment.
| customID: 'discord.js', | |
| customId: 'discord.js', |
| /** | ||
| * https://discord.com/developers/docs/interactions/message-components | ||
| */ | ||
| export interface APIBaseComponent { |
There was a problem hiding this comment.
Why are these exported?
Second thought, I know why, i'll fix this today
| */ | ||
| public setCustomID(customID: string): this { | ||
| ow(customID, ow.string.nonEmpty.maxLength(100)); | ||
| ow(this.style, ow.optional.number.inRange(1, 4).message('Cannot set customID on a button that is Link style.')); |
There was a problem hiding this comment.
- Doesn't ow have something for enums?
- Didn't you forget a
.not? - Won't this always match, since you basically make ow validate that style is in the range of 1-4. Did you mean
.equals(Styles.Link)?
| * @returns {MessageButton} | ||
| */ | ||
| public setURL(url: string) { | ||
| ow(url, ow.string.nonEmpty); |
There was a problem hiding this comment.
Pretty sure there's a hyperlink option
| */ | ||
| public setURL(url: string) { | ||
| ow(url, ow.string.nonEmpty); | ||
| ow(this.style, ow.optional.number.equal(5).message('Cannot set URL on a button that is not Link style.')); |
| * @returns {APIButtonComponent} The raw data of this button | ||
| */ | ||
| public toJSON() { | ||
| ow(this.style, ow.number); |
There was a problem hiding this comment.
Might as well validate it's not any number, but valid numbers, no? Unless we want to allow... forwards-compatibilty with new types
| style, | ||
| ow.any( | ||
| ow.number.inRange(1, 5), | ||
| ow.string.is((value) => ['primary', 'secondary', 'success', 'danger', 'link'].includes(value.toLowerCase())), |
There was a problem hiding this comment.
Should we support strings for builders? Or should we stick to just enum values? CC @discordjs/the-big-3
The MessageButton builder, in TypeScript
owas a dependency to validate inputThis is a draft for now for a few reasons:
discord-api-types, but it would require bumping the dependency version to 0.19Status and versioning classification:
TBD