Skip to content

Commit

Permalink
refactor: allow emoji strings in button builder
Browse files Browse the repository at this point in the history
  • Loading branch information
suneettipirneni committed Mar 7, 2022
1 parent b162f27 commit a06e07f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/discord.js/src/structures/ButtonComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@

const { ButtonComponent: BuildersButtonComponent } = require('@discordjs/builders');
const Transformers = require('../util/Transformers');
const Util = require('../util/Util');

/**
* Represents a button component
*/
class ButtonComponent extends BuildersButtonComponent {
constructor(data) {
super(Transformers.toSnakeCase(data));
}

/**
* Sets the emoji to display on this button
* @param {ComponentEmojiResolvable} emoji The emoji to display on this button
* @returns {ButtonComponent}
*/
setEmoji(emoji) {
if (typeof emoji === 'string') {
return super.setEmoji(Util.parseEmoji(emoji));
}
return super.setEmoji(emoji);
}
}

module.exports = ButtonComponent;
5 changes: 5 additions & 0 deletions packages/discord.js/src/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@
/**
* @typedef {ActionRowData|ButtonComponentData|SelectMenuComponentData|TextInputComponentData} ComponentData
*/

/**
* Any emoji data that can be used within a button
* @typedef {APIMessageComponentEmoji|string} ComponentEmojiResolvable
*/
3 changes: 3 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,11 @@ export class ButtonInteraction<Cached extends CacheType = CacheType> extends Mes
public inRawGuild(): this is ButtonInteraction<'raw'>;
}

export type ComponentEmojiResolvable = APIMessageComponentEmoji | string;

export class ButtonComponent extends BuilderButtonComponent {
public constructor(data?: ButtonComponentData | (Omit<APIButtonComponent, 'type'> & { type?: ComponentType.Button }));
public override setEmoji(emoji: string | ComponentEmojiResolvable): this;
}

export class SelectMenuComponent extends BuilderSelectMenuComponent {
Expand Down

0 comments on commit a06e07f

Please sign in to comment.