I have an issue while making a lib to extend Discord.js lib

37 views Asked by At

my library is thought to be able to do whatever we want on a message before sending it.

Here is an example (the wanted one):

require("discord-prepare");

client.on("messageCreate", (msg) => {
  msg.channel.prepare("helloworld", {
    content: "Hello World!",
    embeds: [
      {
        title: "Title 1",
        description: "Description",
      },
    ],
  });
});

client.on("prepareCreate", (prepare) => {
  let newOptions = prepare.options;

  if (prepare.prepareId === "helloworld") {
    newOptions.embeds[0].description = "Description 1";
    prepare.send(newOptions);
  }
});

My index.d.ts

import Discord from "discord.js";
declare module "discord.js" {
  interface TextChannel {
    prepare: PrepareFunction;
  }
  interface ChatInputCommandInteraction {
    prepare: PrepareFunction;
  }
  type PrepareFunction = (
    prepareId: string,
    options: string & MessagePayload & MessageCreateOptions
  ) => void;
  class Prepare {
    constructor(
      prepareId: string,
      options: MessageCreateOptions & MessagePayload & string,
      channelId: string
    );
    prepareId: string;
    options: MessageCreateOptions & MessagePayload & string;
    channelId: string | null;
    channel: TextChannel | ChatInputCommandInteraction;
    send: (
      options: MessageCreateOptions & MessagePayload & string
    ) => Promise<Message | Discord.InteractionResponse>;
    edit?: (
      options: MessageCreateOptions & MessagePayload & string
    ) => Promise<Message | Discord.InteractionResponse>;
  }
  interface ClientEvents {
    prepareCreate: [prepare: Prepare];
  }
}
//# sourceMappingURL=index.d.ts.map

I have extended the declaration of the Discord.js library, but now I have a problem to do all the work to make function my library. Here is my index.js:

const Discord = require("discord.js");

Discord.TextChannel.prototype.prepare = (prepareId, options) => {
  Discord.Client.prototype.emit("prepareCreate", {
    channel: Discord.TextChannel.prototype,
    channelId: Discord.TextChannel.prototype.id,
    options,
    prepareId: prepareId,
    async send(opts2) {
      return await Discord.TextChannel.prototype.send(opts2);
    },
  });
};

Discord.ChatInputCommandInteraction.prototype.prepare = (
  prepareId,
  options
) => {
  Discord.Client.prototype.emit("prepareCreate", {
    channel: Discord.ChatInputCommandInteraction.prototype,
    channelId: Discord.ChatInputCommandInteraction.prototype.channel
      ? Discord.ChatInputCommandInteraction.prototype.channel.id
      : null,
    options,
    prepareId: prepareId,
    async send(opts2) {
      return await Discord.TextChannel.prototype.send(opts2);
    },
  });
};

//# sourceMappingURL=index.d.ts.map

So... I know I'm making some errors while coding this, so I would like help, maybe I wrongly use prototypes to get and set properties for 1 instance.

Thank you for your help!

0

There are 0 answers