Skip to content

Commit

Permalink
more descriptive djs adapter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
domin-mnd committed Oct 21, 2023
1 parent 65d1d68 commit d387f66
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 24 deletions.
151 changes: 133 additions & 18 deletions packages/reacord/library/core/reacord-discord-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,46 @@ import type { ReacordInstance } from "./instance"
import type { ReacordConfig } from "./reacord"
import { Reacord } from "./reacord"

interface SendOptions {
/**
* Options for the channel message.
*
* @see https://reacord.mapleleaf.dev/guides/sending-messages
*/
export interface LegacyCreateChannelMessageOptions
extends CreateChannelMessageOptions {
/**
* Send message as a reply. Requires the use of message event instead of
* channel id provided as argument.
*
* @deprecated Use reacord.createMessageReply()
*/
reply?: boolean
}

interface ReplyOptions {
/**
* Options for the channel message.
*
* @see https://reacord.mapleleaf.dev/guides/sending-messages
*/
export interface CreateChannelMessageOptions {}

/**
* Options for the message reply method.
*
* @see https://reacord.mapleleaf.dev/guides/sending-messages
*/
export interface CreateMessageReplyOptions {}

/**
* Custom options for the interaction reply method.
*
* @see https://reacord.mapleleaf.dev/guides/sending-messages
*/
export interface CreateInteractionReplyOptions {
/** Whether to send interaction reply as _ephemeral_. */
ephemeral?: boolean
/** Whether to use text-to-speech. */
tts?: boolean
}

/**
Expand All @@ -53,31 +87,95 @@ export class ReacordDiscordJs extends Reacord {
})
}

/**
* Sends a message to a channel.
*
* @param {Discord.Channel} target - Discord channel object.
* @param {CreateChannelMessageOptions} [options={}] - Options for the channel
* message. Default is `{}`
* @param {React.ReactNode} [content] - Initial React node content to render.
* @see https://reacord.mapleleaf.dev/guides/sending-messages
*/
public createChannelMessage(
target: Discord.Channel,
options: CreateChannelMessageOptions = {},
content?: React.ReactNode,
): ReacordInstance {
return this.createInstance(
this.createChannelMessageRenderer(target, options),
content,
)
}

/**
* Replies to a message by sending a message.
*
* @param {Discord.Message} message - Discord message event object.
* @param {CreateMessageReplyOptions} [options={}] - Options for the message
* reply method. Default is `{}`
* @param {React.ReactNode} [content] - Initial React node content to render.
* @see https://reacord.mapleleaf.dev/guides/sending-messages
*/
public createMessageReply(
message: Discord.Message,
options: CreateMessageReplyOptions = {},
content?: React.ReactNode,
): ReacordInstance {
return this.createInstance(
this.createMessageReplyRenderer(message, options),
content,
)
}

/**
* Replies to a command interaction by sending a message.
*
* @param {Discord.CommandInteraction} interaction - Discord command
* interaction object.
* @param {CreateInteractionReplyOptions} [options={}] - Custom options for
* the interaction reply method. Default is `{}`
* @param {React.ReactNode} [content] - Initial React node content to render.
* @see https://reacord.mapleleaf.dev/guides/sending-messages
*/
public createInteractionReply(
interaction: Discord.CommandInteraction,
options: CreateInteractionReplyOptions = {},
content?: React.ReactNode,
): ReacordInstance {
return this.createInstance(
this.createInteractionReplyRenderer(interaction, options),
content,
)
}

/**
* Sends a message to a channel. Alternatively replies to message event.
*
* @deprecated Use reacord.createChannelMessage() or
* reacord.createMessageReply() instead.
* @see https://reacord.mapleleaf.dev/guides/sending-messages
*/
override send(
channelId: string,
public send(
event: string | Discord.Message,
initialContent?: React.ReactNode,
options?: SendOptions,
options: LegacyCreateChannelMessageOptions = {},
): ReacordInstance {
return this.createInstance(
this.createChannelRenderer(channelId, options),
this.createMessageReplyRenderer(event, options),
initialContent,
)
}

/**
* Sends a message as a reply to a command interaction.
*
* @deprecated Use reacord.createInteractionReply() instead.
* @see https://reacord.mapleleaf.dev/guides/sending-messages
*/
override reply(
public reply(
interaction: Discord.CommandInteraction,
initialContent?: React.ReactNode,
options?: ReplyOptions,
options: CreateInteractionReplyOptions = {},
): ReacordInstance {
return this.createInstance(
this.createInteractionReplyRenderer(interaction, options),
Expand All @@ -88,13 +186,14 @@ export class ReacordDiscordJs extends Reacord {
/**
* Sends an ephemeral message as a reply to a command interaction.
*
* @deprecated Use reacord.reply(interaction, content, { ephemeral: true })
* @deprecated Use reacord.createInteractionReply(interaction, content, {
* ephemeral: true })
* @see https://reacord.mapleleaf.dev/guides/sending-messages
*/
override ephemeralReply(
public ephemeralReply(
interaction: Discord.CommandInteraction,
initialContent?: React.ReactNode,
options?: Omit<ReplyOptions, "ephemeral">,
options?: Omit<CreateInteractionReplyOptions, "ephemeral">,
): ReacordInstance {
return this.createInstance(
this.createInteractionReplyRenderer(interaction, {
Expand All @@ -105,9 +204,25 @@ export class ReacordDiscordJs extends Reacord {
)
}

private createChannelRenderer(
private createChannelMessageRenderer(
channel: Discord.Channel,
_opts?: CreateMessageReplyOptions,
) {
return new ChannelMessageRenderer({
send: async (options) => {
if (!channel.isTextBased()) {
raise(`Channel ${channel.id} is not a text channel`)
}

const message = await channel.send(getDiscordMessageOptions(options))
return createReacordMessage(message)
},
})
}

private createMessageReplyRenderer(
event: string | Discord.Message,
opts?: SendOptions,
opts: CreateChannelMessageOptions | LegacyCreateChannelMessageOptions,
) {
return new ChannelMessageRenderer({
send: async (options) => {
Expand All @@ -124,7 +239,7 @@ export class ReacordDiscordJs extends Reacord {
raise(`Channel ${channel.id} is not a text channel`)
}

if (opts?.reply) {
if ("reply" in opts && opts.reply) {
if (typeof event === "string") {
raise("Cannot send reply with channel ID provided")
}
Expand All @@ -142,24 +257,24 @@ export class ReacordDiscordJs extends Reacord {
interaction:
| Discord.CommandInteraction
| Discord.MessageComponentInteraction,
opts?: ReplyOptions,
opts: CreateInteractionReplyOptions,
) {
return new InteractionReplyRenderer({
type: "command",
id: interaction.id,
reply: async (options) => {
const message = await interaction.reply({
...getDiscordMessageOptions(options),
...opts,
fetchReply: true,
ephemeral: opts?.ephemeral,
})
return createReacordMessage(message)
},
followUp: async (options) => {
const message = await interaction.followUp({
...getDiscordMessageOptions(options),
...opts,
fetchReply: true,
ephemeral: opts?.ephemeral,
})
return createReacordMessage(message)
},
Expand Down Expand Up @@ -285,7 +400,7 @@ export class ReacordDiscordJs extends Reacord {

reply: (content?: ReactNode) =>
this.createInstance(
this.createInteractionReplyRenderer(interaction),
this.createInteractionReplyRenderer(interaction, {}),
content,
),

Expand Down
7 changes: 4 additions & 3 deletions packages/reacord/library/core/reacord.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ export abstract class Reacord {

constructor(private readonly config: ReacordConfig = {}) {}

abstract send(...args: unknown[]): ReacordInstance
abstract reply(...args: unknown[]): ReacordInstance
abstract ephemeralReply(...args: unknown[]): ReacordInstance
// There's no more need in abstract methods
// abstract send(...args: unknown[]): ReacordInstance
// abstract reply(...args: unknown[]): ReacordInstance
// abstract ephemeralReply(...args: unknown[]): ReacordInstance

protected handleComponentInteraction(interaction: ComponentInteraction) {
for (const renderer of this.renderers) {
Expand Down
6 changes: 3 additions & 3 deletions packages/reacord/test/test-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ export class ReacordTester extends Reacord {
return [...this.messageContainer]
}

override send(initialContent?: ReactNode): ReacordInstance {
public send(initialContent?: ReactNode): ReacordInstance {
return this.createInstance(
new ChannelMessageRenderer(new TestChannel(this.messageContainer)),
initialContent,
)
}

override reply(initialContent?: ReactNode): ReacordInstance {
public reply(initialContent?: ReactNode): ReacordInstance {
return this.createInstance(
new InteractionReplyRenderer(
new TestCommandInteraction(this.messageContainer),
Expand All @@ -58,7 +58,7 @@ export class ReacordTester extends Reacord {
)
}

override ephemeralReply(initialContent?: ReactNode): ReacordInstance {
public ephemeralReply(initialContent?: ReactNode): ReacordInstance {
return this.reply(initialContent)
}

Expand Down

0 comments on commit d387f66

Please sign in to comment.