-
-
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.
feat(MessageComponents): clickybois (MessageButton, MessageActionRow,…
… associated Collectors) (#5674) Co-authored-by: Vicente <[email protected]> Co-authored-by: Shubham Parihar <[email protected]> Co-authored-by: SpaceEEC <[email protected]> Co-authored-by: BannerBomb <[email protected]> Co-authored-by: Arechi <[email protected]> Co-authored-by: Vlad Frangu <[email protected]> Co-authored-by: Sugden <[email protected]> Co-authored-by: Antonio Román <[email protected]>
- Loading branch information
1 parent
df9b678
commit cbd7f2b
Showing
19 changed files
with
1,190 additions
and
158 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
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
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,94 @@ | ||
'use strict'; | ||
|
||
const { TypeError } = require('../errors'); | ||
const { MessageComponentTypes, Events } = require('../util/Constants'); | ||
|
||
/** | ||
* Represents an interactive component of a Message. It should not be necessary to construct this directly. | ||
* See {@link MessageComponent} | ||
*/ | ||
class BaseMessageComponent { | ||
/** | ||
* Options for a BaseMessageComponent | ||
* @typedef {Object} BaseMessageComponentOptions | ||
* @property {MessageComponentTypeResolvable} type The type of this component | ||
*/ | ||
|
||
/** | ||
* Data that can be resolved into options for a MessageComponent. This can be: | ||
* * MessageActionRowOptions | ||
* * MessageButtonOptions | ||
* @typedef {MessageActionRowOptions|MessageButtonOptions} MessageComponentOptions | ||
*/ | ||
|
||
/** | ||
* Components that can be sent in a message | ||
* @typedef {MessageActionRow|MessageButton} MessageComponent | ||
*/ | ||
|
||
/** | ||
* Data that can be resolved to a MessageComponentType. This can be: | ||
* * {@link MessageComponentType} | ||
* * string | ||
* * number | ||
* @typedef {string|number|MessageComponentType} MessageComponentTypeResolvable | ||
*/ | ||
|
||
/** | ||
* @param {BaseMessageComponent|BaseMessageComponentOptions} [data={}] The options for this component | ||
*/ | ||
constructor(data) { | ||
/** | ||
* The type of this component | ||
* @type {?MessageComponentType} | ||
*/ | ||
this.type = 'type' in data ? BaseMessageComponent.resolveType(data.type) : null; | ||
} | ||
|
||
/** | ||
* Constructs a MessageComponent based on the type of the incoming data | ||
* @param {MessageComponentOptions} data Data for a MessageComponent | ||
* @param {Client|WebhookClient} [client] Client constructing this component | ||
* @param {boolean} [skipValidation=false] Whether or not to validate the component type | ||
* @returns {?MessageComponent} | ||
* @private | ||
*/ | ||
static create(data, client, skipValidation = false) { | ||
let component; | ||
let type = data.type; | ||
|
||
if (typeof type === 'string') type = MessageComponentTypes[type]; | ||
|
||
switch (type) { | ||
case MessageComponentTypes.ACTION_ROW: { | ||
const MessageActionRow = require('./MessageActionRow'); | ||
component = new MessageActionRow(data); | ||
break; | ||
} | ||
case MessageComponentTypes.BUTTON: { | ||
const MessageButton = require('./MessageButton'); | ||
component = new MessageButton(data); | ||
break; | ||
} | ||
default: | ||
if (client) { | ||
client.emit(Events.DEBUG, `[BaseMessageComponent] Received component with unknown type: ${data.type}`); | ||
} else if (!skipValidation) { | ||
throw new TypeError('INVALID_TYPE', 'data.type', 'valid MessageComponentType'); | ||
} | ||
} | ||
return component; | ||
} | ||
|
||
/** | ||
* Resolves the type of a MessageComponent | ||
* @param {MessageComponentTypeResolvable} type The type to resolve | ||
* @returns {MessageComponentType} | ||
* @private | ||
*/ | ||
static resolveType(type) { | ||
return typeof type === 'string' ? type : MessageComponentTypes[type]; | ||
} | ||
} | ||
|
||
module.exports = BaseMessageComponent; |
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
Oops, something went wrong.