#Crash because of undefined _sharedAddOptionMethod

7 messages · Page 1 of 1 (latest)

opaque plinth
#

I created a CommandConstructor but when adding options to it, it always gives me following error:

WizRepoBot\node_modules\@discordjs\builders\dist\index.js:1335
    return this._sharedAddOptionMethod(input, SlashCommandStringOption);
                ^

TypeError: Cannot read properties of undefined (reading '_sharedAddOptionMethod')
    at addStringOption (D:\Phill030\WizRepoBot\node_modules\@discordjs\builders\dist\index.js:1335:17)
    at new Command (file:///D:/Phill030/WizRepoBot/dist/constructor/Command.js:1:883)
    at Function.Create (file:///D:/Phill030/WizRepoBot/dist/constructor/Command.js:1:483)
    at file:///D:/Phill030/WizRepoBot/dist/commands/ref.js:1:77
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async Function.init (file:///D:/Phill030/WizRepoBot/dist/index.js:1:338)

Constructor:

grave forge
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

opaque plinth
#
export type OptionType =
  | "Attachment"
  | "Boolean"
  | "Channel"
  | "Integer"
  | "Mentionable"
  | "Number"
  | "Role"
  | "String"
  | "Subcommand"
  | "SubcommandGroup"
  | "User";

const optionTypeMap: { [key in OptionType]: (o: SlashCommandBuilder) => any } =
  {
    Attachment: (o) => o.addAttachmentOption,
    Boolean: (o) => o.addBooleanOption,
    Channel: (o) => o.addChannelOption,
    Integer: (o) => o.addIntegerOption,
    Mentionable: (o) => o.addMentionableOption,
    Number: (o) => o.addNumberOption,
    Role: (o) => o.addRoleOption,
    String: (o) => o.addStringOption,
    Subcommand: (o) => o.addSubcommand,
    SubcommandGroup: (o) => o.addSubcommandGroup,
    User: (o) => o.addUserOption,
  };

export interface Option {
  type: OptionType;
  name: string;
  description: string;
  required: boolean;
  choices?: Array<{ name: string; value: string }>;
}

  private constructor(
    name: string,
    description: string,
    options: Array<Option>,
    executeFunc: (interaction: Interaction) => void
  ) {
    name = name.toLowerCase().replaceAll(" ", "");

    const cmd = new SlashCommandBuilder()
      .setName(name)
      .setDescription(description);

    if (options && options.length >= 1) {
      for (let i = 0; i < options.length; i++) {
        const opt = options[i];
        opt.name = opt.name.toLowerCase().replaceAll(" ", "");

        const optionBuilder = optionTypeMap[opt.type](cmd);
        optionBuilder((o: SlashCommandBuilder) =>
          o.setName(opt.name).setDescription(opt.description)
        );

        if (opt.choices) {
          optionBuilder.addChoices(...opt.choices);
        }
      }
    }

    this.execute = executeFunc;
    this.data = cmd;
  }
cunning bane
#

you unbind the this context by putting the methods as function references into your optionTypeMap like that...

opaque plinth
#

I have a brainlag rn can you give me a hint to fix it xd

cunning bane
#

One way would be to call .bind(o) on all of them in the map