-
-
Notifications
You must be signed in to change notification settings - Fork 37
feat(MessageButton): builder and associated interfaces #6
base: main
Are you sure you want to change the base?
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.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public customID?: string; | |
public customId?: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can always break this for v13, depends on when you want to release it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, merged, we can move forward with that change.
*/ | ||
export class MessageButton { | ||
/** | ||
* The text to be displayed on this button |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy paste error from label
* @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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public setCustomID(customID: string): this { | |
public setCustomId(customId: string): this { |
} | ||
|
||
/** | ||
* Sets the custom ID of this button |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
customID: 'discord.js', | |
customId: 'discord.js', |
/** | ||
* https://discord.com/developers/docs/interactions/message-components | ||
*/ | ||
export interface APIBaseComponent { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is optional intended?
* @returns {APIButtonComponent} The raw data of this button | ||
*/ | ||
public toJSON() { | ||
ow(this.style, ow.number); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we support strings for builders? Or should we stick to just enum values? CC @discordjs/the-big-3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather just go enum only.
The MessageButton builder, in TypeScript
ow
as 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