Bubba
Bubba
DIAdiscord.js - Imagine an app
Created by Bubba on 8/2/2023 in #djs-questions
Issue with the implementation of a custom TextInput class
Hello, I'm trying to implement the following class in order to create a TextInputBuilder without having to use setPlaceholder, setCustomId, etc when creating a new object
interface TextInputProps {
placeholder?: string;
customId?: string;
label?: string;
style?: TextInputStyle;
required?: boolean;
minLength?: number;
maxLength?: number;
}

class TextInput {

private text: TextInputBuilder;

constructor(props: TextInputProps) {
this.text = new TextInputBuilder();
this.setParameters(props);
}

public get(): TextInputBuilder {
return this.text;
}

private setParameters(props: TextInputProps) {
const functionsObject = {
'placeholder': this.text.setPlaceholder,
'customId': this.text.setCustomId,
'label': this.text.setLabel,
'style': this.text.setStyle,
'required': this.text.setRequired,
'minLength': this.text.setMinLength,
'maxLength': this.text.setMaxLength,
};

for (const [key, value] of Object.entries(props)) {
if (key && value) {
console.log(`key: ${key}, value: ${value}`);
functionsObject[key](value);
}
}
}
}
interface TextInputProps {
placeholder?: string;
customId?: string;
label?: string;
style?: TextInputStyle;
required?: boolean;
minLength?: number;
maxLength?: number;
}

class TextInput {

private text: TextInputBuilder;

constructor(props: TextInputProps) {
this.text = new TextInputBuilder();
this.setParameters(props);
}

public get(): TextInputBuilder {
return this.text;
}

private setParameters(props: TextInputProps) {
const functionsObject = {
'placeholder': this.text.setPlaceholder,
'customId': this.text.setCustomId,
'label': this.text.setLabel,
'style': this.text.setStyle,
'required': this.text.setRequired,
'minLength': this.text.setMinLength,
'maxLength': this.text.setMaxLength,
};

for (const [key, value] of Object.entries(props)) {
if (key && value) {
console.log(`key: ${key}, value: ${value}`);
functionsObject[key](value);
}
}
}
}
However I'm receiving the following error:
key: placeholder, value: Product name
PROJECT_PATH/node_modules/@discordjs/builders/src/components/textInput/TextInput.ts:107
this.data.placeholder = placeholderValidator.parse(placeholder);
^
TypeError: Cannot set properties of undefined (setting 'placeholder')
key: placeholder, value: Product name
PROJECT_PATH/node_modules/@discordjs/builders/src/components/textInput/TextInput.ts:107
this.data.placeholder = placeholderValidator.parse(placeholder);
^
TypeError: Cannot set properties of undefined (setting 'placeholder')
Any idea?
3 replies