Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

feat(MessageButton): builder and associated interfaces #6

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

monbrey
Copy link
Member

@monbrey monbrey commented Jul 4, 2021

The MessageButton builder, in TypeScript

  • Builder class for Message Buttons
  • Interfaces and types
  • Some Util functions as static methods that would probably belong better elsewhere
  • Added ow as a dependency to validate input
  • Tests

This is a draft for now for a few reasons:

  • Unsure if it needs to extend a base component class or not
  • It's the first time I've used jest for anything, so will need advise/reviews on the tests
  • I've re-written a lot of types that could just be imported from discord-api-types, but it would require bumping the dependency version to 0.19

Status and versioning classification:
TBD

@codecov-commenter
Copy link

Codecov Report

Merging #6 (2aea829) into main (97cf337) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
src/components/MessageButton.ts 100.00% <100.00%> (ø)
src/messages/formatters.ts 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 97cf337...2aea829. Read the comment docs.

Copy link
Member

@iCrawl iCrawl left a 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public customID?: string;
public customId?: string;

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

@kyranet kyranet Jul 4, 2021

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.

Copy link
Member

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
Copy link
Member

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public setCustomID(customID: string): this {
public setCustomId(customId: string): this {

}

/**
* Sets the custom ID of this button
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
customID: 'discord.js',
customId: 'discord.js',

expect(button.style).toBe(1);
});

test('GIVEN raw data THEN customID is set to custom_id', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(button.customID).toBe('discord.js');
expect(button.customId).toBe('discord.js');

expect(
new MessageButton({
style: 'Primary',
customID: 'discord.js',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
customID: 'discord.js',
customId: 'discord.js',

/**
* https://discord.com/developers/docs/interactions/message-components
*/
export interface APIBaseComponent {
Copy link
Member

@vladfrangu vladfrangu Jul 5, 2021

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.'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Doesn't ow have something for enums?
  2. Didn't you forget a .not?
  3. 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);
Copy link
Member

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.'));
Copy link
Member

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);
Copy link
Member

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())),
Copy link
Member

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

Copy link
Member

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants