-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: modal support * chore: formatting
- Loading branch information
1 parent
9e93027
commit ad9666b
Showing
11 changed files
with
290 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@buape/carbon": minor | ||
--- | ||
|
||
feat: Modal support |
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,64 @@ | ||
import { | ||
Command, | ||
type CommandInteraction, | ||
Modal, | ||
type ModalInteraction, | ||
Row, | ||
TextInput, | ||
TextInputStyle | ||
} from "@buape/carbon" | ||
|
||
export default class ModalCommand extends Command { | ||
name = "modal" | ||
description = "Modal test" | ||
|
||
async run(interaction: CommandInteraction) { | ||
await interaction.showModal(new TestModal()) | ||
} | ||
} | ||
|
||
class TestModal extends Modal { | ||
title = "Test Modal" | ||
customId = "test-modal" | ||
|
||
components = [ | ||
new Row([new TextInputHi()]), | ||
new Row([new TextInputName()]), | ||
new Row([new TextInputAge()]), | ||
new Row([new TextInputColor()]), | ||
new Row([new TextInputHeight()]) | ||
] | ||
|
||
run(interaction: ModalInteraction) { | ||
return interaction.reply({ | ||
content: `Hi ${interaction.values.name}, you are ${interaction.values.age} years old, and your favorite color is ${interaction.values.color}. You are ${interaction.values.height || "not"} tall.` | ||
}) | ||
} | ||
} | ||
|
||
class TextInputHi extends TextInput { | ||
label = "Hi, how are you?" | ||
customId = "hi" | ||
style = TextInputStyle.Paragraph | ||
} | ||
|
||
class TextInputColor extends TextInput { | ||
label = "What is your favorite color?" | ||
customId = "color" | ||
} | ||
|
||
class TextInputAge extends TextInput { | ||
label = "How old are you?" | ||
customId = "age" | ||
} | ||
|
||
class TextInputName extends TextInput { | ||
label = "What is your name?" | ||
customId = "name" | ||
} | ||
|
||
class TextInputHeight extends TextInput { | ||
label = "How tall are you?" | ||
customId = "height" | ||
required = false | ||
} |
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,38 @@ | ||
import type { | ||
APIActionRowComponent, | ||
APIModalInteractionResponseCallbackData, | ||
APITextInputComponent | ||
} from "discord-api-types/v10" | ||
import type { ModalInteraction } from "../internals/ModalInteraction.js" | ||
import type { Row } from "./Row.js" | ||
import type { TextInput } from "./TextInput.js" | ||
|
||
export abstract class Modal { | ||
/** | ||
* The title of the modal | ||
*/ | ||
abstract title: string | ||
|
||
/** | ||
* The components of the modal | ||
*/ | ||
components: Row<TextInput>[] = [] | ||
|
||
/** | ||
* The custom ID of the modal | ||
*/ | ||
abstract customId: string | ||
|
||
abstract run(interaction: ModalInteraction): Promise<void> | ||
|
||
serialize = (): APIModalInteractionResponseCallbackData => { | ||
return { | ||
title: this.title, | ||
custom_id: this.customId, | ||
components: this.components.map( | ||
(row) => | ||
row.serialize() as unknown as APIActionRowComponent<APITextInputComponent> | ||
) | ||
} | ||
} | ||
} |
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,65 @@ | ||
import { | ||
type APITextInputComponent, | ||
ComponentType, | ||
TextInputStyle | ||
} from "discord-api-types/v10" | ||
import { BaseComponent } from "../abstracts/BaseComponent.js" | ||
|
||
export abstract class TextInput extends BaseComponent { | ||
type = ComponentType.TextInput | ||
|
||
/** | ||
* The custom ID of the text input | ||
*/ | ||
abstract customId: string | ||
|
||
/** | ||
* The label of the text input | ||
*/ | ||
abstract label: string | ||
|
||
/** | ||
* The style of the text input | ||
* @default TextInputStyle.Short | ||
*/ | ||
style: TextInputStyle = TextInputStyle.Short | ||
|
||
/** | ||
* The minimum length of the text input | ||
*/ | ||
minLength?: number | ||
|
||
/** | ||
* The maximum length of the text input | ||
*/ | ||
maxLength?: number | ||
|
||
/** | ||
* Whether the text input is required | ||
*/ | ||
required?: boolean | ||
|
||
/** | ||
* The value of the text input | ||
*/ | ||
value?: string | ||
|
||
/** | ||
* The placeholder of the text input | ||
*/ | ||
placeholder?: string | ||
|
||
serialize = (): APITextInputComponent => { | ||
return { | ||
type: ComponentType.TextInput, | ||
custom_id: this.customId, | ||
style: this.style, | ||
label: this.label, | ||
min_length: this.minLength, | ||
max_length: this.maxLength, | ||
required: this.required, | ||
value: this.value, | ||
placeholder: this.placeholder | ||
} | ||
} | ||
} |
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,27 @@ | ||
import type { APIModalSubmitInteraction } from "discord-api-types/v10" | ||
import { Base } from "../abstracts/Base.js" | ||
import type { Modal } from "../classes/Modal.js" | ||
import { ModalInteraction } from "./ModalInteraction.js" | ||
|
||
export class ModalHandler extends Base { | ||
modals: Modal[] = [] | ||
/** | ||
* Register a modal with the handler | ||
* @internal | ||
*/ | ||
registerModal(modal: Modal) { | ||
if (!this.modals.find((x) => x.customId === modal.customId)) { | ||
this.modals.push(modal) | ||
} | ||
} | ||
/** | ||
* Handle an interaction | ||
* @internal | ||
*/ | ||
async handleInteraction(data: APIModalSubmitInteraction) { | ||
const modal = this.modals.find((x) => x.customId === data.data.custom_id) | ||
if (!modal) return false | ||
|
||
modal.run(new ModalInteraction(this.client, data)) | ||
} | ||
} |
Oops, something went wrong.