From d0789955166e6e5db65c264fde1c190ef56def83 Mon Sep 17 00:00:00 2001 From: Domin-MND <69919939+domin-mnd@users.noreply.github.com> Date: Wed, 18 Oct 2023 20:59:14 +0300 Subject: [PATCH 1/4] deprecate ephemeralReply in adapters --- packages/reacord/library/core/reacord-discord-js.ts | 1 + packages/reacord/library/core/reacord.tsx | 3 +++ 2 files changed, 4 insertions(+) diff --git a/packages/reacord/library/core/reacord-discord-js.ts b/packages/reacord/library/core/reacord-discord-js.ts index 458ff86..e945696 100644 --- a/packages/reacord/library/core/reacord-discord-js.ts +++ b/packages/reacord/library/core/reacord-discord-js.ts @@ -78,6 +78,7 @@ export class ReacordDiscordJs extends Reacord { /** * Sends an ephemeral message as a reply to a command interaction. * + * @deprecated Use reacord.reply(content, { ephemeral: true }) * @see https://reacord.mapleleaf.dev/guides/sending-messages */ override ephemeralReply( diff --git a/packages/reacord/library/core/reacord.tsx b/packages/reacord/library/core/reacord.tsx index 02eba7e..bcb40ec 100644 --- a/packages/reacord/library/core/reacord.tsx +++ b/packages/reacord/library/core/reacord.tsx @@ -25,6 +25,9 @@ export abstract class Reacord { abstract send(...args: unknown[]): ReacordInstance abstract reply(...args: unknown[]): ReacordInstance + /** + * @deprecated Use reacord.reply(content, { ephemeral: true }) + */ abstract ephemeralReply(...args: unknown[]): ReacordInstance protected handleComponentInteraction(interaction: ComponentInteraction) { From 9e2be6c2e002a5987bbca36a18d75b907166c950 Mon Sep 17 00:00:00 2001 From: Domin-MND <69919939+domin-mnd@users.noreply.github.com> Date: Wed, 18 Oct 2023 21:39:17 +0300 Subject: [PATCH 2/4] add opts argument support --- .../library/core/reacord-discord-js.ts | 38 ++++++++++++++++--- packages/reacord/library/core/reacord.tsx | 3 -- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/packages/reacord/library/core/reacord-discord-js.ts b/packages/reacord/library/core/reacord-discord-js.ts index e945696..07d3a08 100644 --- a/packages/reacord/library/core/reacord-discord-js.ts +++ b/packages/reacord/library/core/reacord-discord-js.ts @@ -24,6 +24,14 @@ import type { ReacordInstance } from "./instance" import type { ReacordConfig } from "./reacord" import { Reacord } from "./reacord" +interface SendOptions { + reply?: boolean +} + +interface ReplyOptions { + ephemeral?: boolean +} + /** * The Reacord adapter for Discord.js. * @@ -78,7 +86,7 @@ export class ReacordDiscordJs extends Reacord { /** * Sends an ephemeral message as a reply to a command interaction. * - * @deprecated Use reacord.reply(content, { ephemeral: true }) + * @deprecated Use reacord.reply(interaction, content, { ephemeral: true }) * @see https://reacord.mapleleaf.dev/guides/sending-messages */ override ephemeralReply( @@ -91,18 +99,33 @@ export class ReacordDiscordJs extends Reacord { ) } - private createChannelRenderer(channelId: string) { + private createChannelRenderer( + event: string | Discord.Message, + opts?: SendOptions, + ) { return new ChannelMessageRenderer({ send: async (options) => { + // Backwards compatible channelId api + // `event` is treated as MessageEvent depending on its type const channel = - this.client.channels.cache.get(channelId) ?? - (await this.client.channels.fetch(channelId)) ?? - raise(`Channel ${channelId} not found`) + typeof event === "string" + ? this.client.channels.cache.get(event) ?? + (await this.client.channels.fetch(event)) ?? + raise(`Channel ${event} not found`) + : event.channel if (!channel.isTextBased()) { - raise(`Channel ${channelId} is not a text channel`) + raise(`Channel ${event} is not a text channel`) } + if (opts?.reply) { + if (typeof event === "string") { + raise("Cannot send reply with channel ID provided") + } + + const message = await event.reply(getDiscordMessageOptions(options)) + return createReacordMessage(message) + } const message = await channel.send(getDiscordMessageOptions(options)) return createReacordMessage(message) }, @@ -113,6 +136,7 @@ export class ReacordDiscordJs extends Reacord { interaction: | Discord.CommandInteraction | Discord.MessageComponentInteraction, + opts?: ReplyOptions, ) { return new InteractionReplyRenderer({ type: "command", @@ -121,6 +145,7 @@ export class ReacordDiscordJs extends Reacord { const message = await interaction.reply({ ...getDiscordMessageOptions(options), fetchReply: true, + ephemeral: opts?.ephemeral, }) return createReacordMessage(message) }, @@ -128,6 +153,7 @@ export class ReacordDiscordJs extends Reacord { const message = await interaction.followUp({ ...getDiscordMessageOptions(options), fetchReply: true, + ephemeral: opts?.ephemeral, }) return createReacordMessage(message) }, diff --git a/packages/reacord/library/core/reacord.tsx b/packages/reacord/library/core/reacord.tsx index bcb40ec..02eba7e 100644 --- a/packages/reacord/library/core/reacord.tsx +++ b/packages/reacord/library/core/reacord.tsx @@ -25,9 +25,6 @@ export abstract class Reacord { abstract send(...args: unknown[]): ReacordInstance abstract reply(...args: unknown[]): ReacordInstance - /** - * @deprecated Use reacord.reply(content, { ephemeral: true }) - */ abstract ephemeralReply(...args: unknown[]): ReacordInstance protected handleComponentInteraction(interaction: ComponentInteraction) { From dfb7562c97e984d3c08b271da0036ca2304722b3 Mon Sep 17 00:00:00 2001 From: Domin-MND <69919939+domin-mnd@users.noreply.github.com> Date: Wed, 18 Oct 2023 21:48:38 +0300 Subject: [PATCH 3/4] use reply renderer for ephermalReply --- .../library/core/reacord-discord-js.ts | 56 +++++-------------- 1 file changed, 13 insertions(+), 43 deletions(-) diff --git a/packages/reacord/library/core/reacord-discord-js.ts b/packages/reacord/library/core/reacord-discord-js.ts index 07d3a08..a48d3b0 100644 --- a/packages/reacord/library/core/reacord-discord-js.ts +++ b/packages/reacord/library/core/reacord-discord-js.ts @@ -54,16 +54,17 @@ export class ReacordDiscordJs extends Reacord { } /** - * Sends a message to a channel. + * Sends a message to a channel. Alternatively replies to message event. * * @see https://reacord.mapleleaf.dev/guides/sending-messages */ override send( channelId: string, initialContent?: React.ReactNode, + options?: SendOptions, ): ReacordInstance { return this.createInstance( - this.createChannelRenderer(channelId), + this.createChannelRenderer(channelId, options), initialContent, ) } @@ -76,9 +77,10 @@ export class ReacordDiscordJs extends Reacord { override reply( interaction: Discord.CommandInteraction, initialContent?: React.ReactNode, + options?: ReplyOptions, ): ReacordInstance { return this.createInstance( - this.createInteractionReplyRenderer(interaction), + this.createInteractionReplyRenderer(interaction, options), initialContent, ) } @@ -92,9 +94,13 @@ export class ReacordDiscordJs extends Reacord { override ephemeralReply( interaction: Discord.CommandInteraction, initialContent?: React.ReactNode, + options?: Omit, ): ReacordInstance { return this.createInstance( - this.createEphemeralInteractionReplyRenderer(interaction), + this.createInteractionReplyRenderer(interaction, { + ...options, + ephemeral: true, + }), initialContent, ) } @@ -160,31 +166,6 @@ export class ReacordDiscordJs extends Reacord { }) } - private createEphemeralInteractionReplyRenderer( - interaction: - | Discord.CommandInteraction - | Discord.MessageComponentInteraction, - ) { - return new InteractionReplyRenderer({ - type: "command", - id: interaction.id, - reply: async (options) => { - await interaction.reply({ - ...getDiscordMessageOptions(options), - ephemeral: true, - }) - return createEphemeralReacordMessage() - }, - followUp: async (options) => { - await interaction.followUp({ - ...getDiscordMessageOptions(options), - ephemeral: true, - }) - return createEphemeralReacordMessage() - }, - }) - } - private createReacordComponentInteraction( interaction: Discord.MessageComponentInteraction, ): ComponentInteraction { @@ -310,7 +291,9 @@ export class ReacordDiscordJs extends Reacord { ephemeralReply: (content: ReactNode) => this.createInstance( - this.createEphemeralInteractionReplyRenderer(interaction), + this.createInteractionReplyRenderer(interaction, { + ephemeral: true, + }), content, ), }, @@ -349,19 +332,6 @@ function createReacordMessage(message: Discord.Message): Message { } } -function createEphemeralReacordMessage(): Message { - return { - edit: () => { - console.warn("Ephemeral messages can't be edited") - return Promise.resolve() - }, - delete: () => { - console.warn("Ephemeral messages can't be deleted") - return Promise.resolve() - }, - } -} - function convertButtonStyleToEnum(style: MessageButtonOptions["style"]) { const styleMap = { primary: Discord.ButtonStyle.Primary, From 65d1d68bb02a8110e5e55fff0ff457863060f621 Mon Sep 17 00:00:00 2001 From: Domin-MND <69919939+domin-mnd@users.noreply.github.com> Date: Thu, 19 Oct 2023 16:37:51 +0300 Subject: [PATCH 4/4] fix id raising --- packages/reacord/library/core/reacord-discord-js.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/reacord/library/core/reacord-discord-js.ts b/packages/reacord/library/core/reacord-discord-js.ts index a48d3b0..6e48763 100644 --- a/packages/reacord/library/core/reacord-discord-js.ts +++ b/packages/reacord/library/core/reacord-discord-js.ts @@ -121,7 +121,7 @@ export class ReacordDiscordJs extends Reacord { : event.channel if (!channel.isTextBased()) { - raise(`Channel ${event} is not a text channel`) + raise(`Channel ${channel.id} is not a text channel`) } if (opts?.reply) {