sp00ktober
sp00ktober
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by sp00ktober on 3/15/2024 in #djs-questions
Modals in Node-Red using Discord JS
A little offtopic but its based on discord.js, i hope to find some support here anyways. In node red i try to show a modal with text input in response to a slash command. for this i took the example from the webpage and modified it to fit into node red, here is my code:
// Create the modal
const modal = new discordjsBuilders.ModalBuilder()
.setCustomId('myModal')
.setTitle('My Modal');

// Add components to modal

// Create the text input components
const favoriteColorInput = new discordjsBuilders.TextInputBuilder()
.setCustomId('favoriteColorInput')
// The label is the prompt the user sees for this input
.setLabel("What's your favorite color?")
// Short means only a single line of text
.setStyle(discordApiTypesV9.TextInputStyle.Short);

const hobbiesInput = new discordjsBuilders.TextInputBuilder()
.setCustomId('hobbiesInput')
.setLabel("What's some of your favorite hobbies?")
// Paragraph means multiple lines of text.
.setStyle(discordApiTypesV9.TextInputStyle.Paragraph);

// An action row only holds one text input,
// so you need one action row per text input.
const firstActionRow = new discordjsBuilders.ActionRowBuilder().addComponents(favoriteColorInput);
const secondActionRow = new discordjsBuilders.ActionRowBuilder().addComponents(hobbiesInput);

// Add inputs to the modal
modal.addComponents(firstActionRow, secondActionRow);

// Show the modal to the user
msg.interactionObject.showModal(modal);
// Create the modal
const modal = new discordjsBuilders.ModalBuilder()
.setCustomId('myModal')
.setTitle('My Modal');

// Add components to modal

// Create the text input components
const favoriteColorInput = new discordjsBuilders.TextInputBuilder()
.setCustomId('favoriteColorInput')
// The label is the prompt the user sees for this input
.setLabel("What's your favorite color?")
// Short means only a single line of text
.setStyle(discordApiTypesV9.TextInputStyle.Short);

const hobbiesInput = new discordjsBuilders.TextInputBuilder()
.setCustomId('hobbiesInput')
.setLabel("What's some of your favorite hobbies?")
// Paragraph means multiple lines of text.
.setStyle(discordApiTypesV9.TextInputStyle.Paragraph);

// An action row only holds one text input,
// so you need one action row per text input.
const firstActionRow = new discordjsBuilders.ActionRowBuilder().addComponents(favoriteColorInput);
const secondActionRow = new discordjsBuilders.ActionRowBuilder().addComponents(hobbiesInput);

// Add inputs to the modal
modal.addComponents(firstActionRow, secondActionRow);

// Show the modal to the user
msg.interactionObject.showModal(modal);
Last line produces this error: TypeError: Cannot read properties of undefined (reading 'rest') The msg.interactionObject is a copy of the interaction object from the discord.js examples callbacks. I modified the node red version to not send deferReply() too early (before my own flow code comes), basically here: https://github.com/Markoudstaal/node-red-contrib-discord-advanced/blob/6dedf30e2f88c616ac28992b733dbff37b23796a/discord/discordInteraction.js#L58 still i dont understand the error i get, ideas?
5 replies
CC#
Created by sp00ktober on 3/24/2023 in #help
❔ nested generic interface throws cast exception when added to list
Im working on a mod project and for this i need to handle different updates which are represented by individual classes that all inherit from the same generic interfaces. To accomplish the handling i also wrote a generic interface which can then be implemented by a class that should handle one specific class. In the end i want to have a list of handlers over which i can iterate to process the updates. This is my interface signature
internal interface IComponentUpdateHandler<T, H, C> where T : IComponentMetaclass where H : IComponentUpdate<T> where C : IComponentData<T>
internal interface IComponentUpdateHandler<T, H, C> where T : IComponentMetaclass where H : IComponentUpdate<T> where C : IComponentData<T>
These are the interfaces for the update and data interfaces
public interface IComponentUpdate<C> where C : IComponentMetaclass
public interface IComponentData<C> where C : IComponentMetaclass
public interface IComponentUpdate<C> where C : IComponentMetaclass
public interface IComponentData<C> where C : IComponentMetaclass
This is the signature of one of my handler classes
internal class InventoryModificationState_Handler : IComponentUpdateHandler<InventoryModificationState, InventoryModificationState.Update, InventoryModificationState.Data>
internal class InventoryModificationState_Handler : IComponentUpdateHandler<InventoryModificationState, InventoryModificationState.Update, InventoryModificationState.Data>
These are the signatures of the update and data classes
public class Update : IComponentUpdate<InventoryModificationState>
public class Data : IComponentData<InventoryModificationState>
public class Update : IComponentUpdate<InventoryModificationState>
public class Data : IComponentData<InventoryModificationState>
This is the signature of the base class
public class InventoryModificationState : IComponentMetaclass, IComponentFactory
public class InventoryModificationState : IComponentMetaclass, IComponentFactory
This is how i create the List and fill it with values
private List<IComponentUpdateHandler<IComponentMetaclass, IComponentUpdate<IComponentMetaclass>, IComponentData<IComponentMetaclass>>> handler { get; set; }
private ComponentUpdateManager()
{
handler = new List<IComponentUpdateHandler<IComponentMetaclass, IComponentUpdate<IComponentMetaclass>, IComponentData<IComponentMetaclass>>>
{
new InventoryModificationState_Handler(),
new PlayerCraftingInteractionState_Handler()
};
}
private List<IComponentUpdateHandler<IComponentMetaclass, IComponentUpdate<IComponentMetaclass>, IComponentData<IComponentMetaclass>>> handler { get; set; }
private ComponentUpdateManager()
{
handler = new List<IComponentUpdateHandler<IComponentMetaclass, IComponentUpdate<IComponentMetaclass>, IComponentData<IComponentMetaclass>>>
{
new InventoryModificationState_Handler(),
new PlayerCraftingInteractionState_Handler()
};
}
Im happy for any input as im stuck on this for some hours now 😄
27 replies