more descriptive djs adapter methods
This commit is contained in:
@@ -24,12 +24,46 @@ import type { ReacordInstance } from "./instance"
|
|||||||
import type { ReacordConfig } from "./reacord"
|
import type { ReacordConfig } from "./reacord"
|
||||||
import { Reacord } 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
|
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
|
ephemeral?: boolean
|
||||||
|
/** Whether to use text-to-speech. */
|
||||||
|
tts?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -54,17 +88,80 @@ export class ReacordDiscordJs extends Reacord {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a message to a channel. Alternatively replies to message event.
|
* 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
|
* @see https://reacord.mapleleaf.dev/guides/sending-messages
|
||||||
*/
|
*/
|
||||||
override send(
|
public createChannelMessage(
|
||||||
channelId: string,
|
target: Discord.Channel,
|
||||||
initialContent?: React.ReactNode,
|
options: CreateChannelMessageOptions = {},
|
||||||
options?: SendOptions,
|
content?: React.ReactNode,
|
||||||
): ReacordInstance {
|
): ReacordInstance {
|
||||||
return this.createInstance(
|
return this.createInstance(
|
||||||
this.createChannelRenderer(channelId, options),
|
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
|
||||||
|
*/
|
||||||
|
public send(
|
||||||
|
event: string | Discord.Message,
|
||||||
|
initialContent?: React.ReactNode,
|
||||||
|
options: LegacyCreateChannelMessageOptions = {},
|
||||||
|
): ReacordInstance {
|
||||||
|
return this.createInstance(
|
||||||
|
this.createMessageReplyRenderer(event, options),
|
||||||
initialContent,
|
initialContent,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -72,12 +169,13 @@ export class ReacordDiscordJs extends Reacord {
|
|||||||
/**
|
/**
|
||||||
* Sends a message as a reply to a command interaction.
|
* Sends a message as a reply to a command interaction.
|
||||||
*
|
*
|
||||||
|
* @deprecated Use reacord.createInteractionReply() instead.
|
||||||
* @see https://reacord.mapleleaf.dev/guides/sending-messages
|
* @see https://reacord.mapleleaf.dev/guides/sending-messages
|
||||||
*/
|
*/
|
||||||
override reply(
|
public reply(
|
||||||
interaction: Discord.CommandInteraction,
|
interaction: Discord.CommandInteraction,
|
||||||
initialContent?: React.ReactNode,
|
initialContent?: React.ReactNode,
|
||||||
options?: ReplyOptions,
|
options: CreateInteractionReplyOptions = {},
|
||||||
): ReacordInstance {
|
): ReacordInstance {
|
||||||
return this.createInstance(
|
return this.createInstance(
|
||||||
this.createInteractionReplyRenderer(interaction, options),
|
this.createInteractionReplyRenderer(interaction, options),
|
||||||
@@ -88,13 +186,14 @@ export class ReacordDiscordJs extends Reacord {
|
|||||||
/**
|
/**
|
||||||
* Sends an ephemeral message as a reply to a command interaction.
|
* 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
|
* @see https://reacord.mapleleaf.dev/guides/sending-messages
|
||||||
*/
|
*/
|
||||||
override ephemeralReply(
|
public ephemeralReply(
|
||||||
interaction: Discord.CommandInteraction,
|
interaction: Discord.CommandInteraction,
|
||||||
initialContent?: React.ReactNode,
|
initialContent?: React.ReactNode,
|
||||||
options?: Omit<ReplyOptions, "ephemeral">,
|
options?: Omit<CreateInteractionReplyOptions, "ephemeral">,
|
||||||
): ReacordInstance {
|
): ReacordInstance {
|
||||||
return this.createInstance(
|
return this.createInstance(
|
||||||
this.createInteractionReplyRenderer(interaction, {
|
this.createInteractionReplyRenderer(interaction, {
|
||||||
@@ -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,
|
event: string | Discord.Message,
|
||||||
opts?: SendOptions,
|
opts: CreateChannelMessageOptions | LegacyCreateChannelMessageOptions,
|
||||||
) {
|
) {
|
||||||
return new ChannelMessageRenderer({
|
return new ChannelMessageRenderer({
|
||||||
send: async (options) => {
|
send: async (options) => {
|
||||||
@@ -124,7 +239,7 @@ export class ReacordDiscordJs extends Reacord {
|
|||||||
raise(`Channel ${channel.id} is not a text channel`)
|
raise(`Channel ${channel.id} is not a text channel`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts?.reply) {
|
if ("reply" in opts && opts.reply) {
|
||||||
if (typeof event === "string") {
|
if (typeof event === "string") {
|
||||||
raise("Cannot send reply with channel ID provided")
|
raise("Cannot send reply with channel ID provided")
|
||||||
}
|
}
|
||||||
@@ -142,7 +257,7 @@ export class ReacordDiscordJs extends Reacord {
|
|||||||
interaction:
|
interaction:
|
||||||
| Discord.CommandInteraction
|
| Discord.CommandInteraction
|
||||||
| Discord.MessageComponentInteraction,
|
| Discord.MessageComponentInteraction,
|
||||||
opts?: ReplyOptions,
|
opts: CreateInteractionReplyOptions,
|
||||||
) {
|
) {
|
||||||
return new InteractionReplyRenderer({
|
return new InteractionReplyRenderer({
|
||||||
type: "command",
|
type: "command",
|
||||||
@@ -150,16 +265,16 @@ export class ReacordDiscordJs extends Reacord {
|
|||||||
reply: async (options) => {
|
reply: async (options) => {
|
||||||
const message = await interaction.reply({
|
const message = await interaction.reply({
|
||||||
...getDiscordMessageOptions(options),
|
...getDiscordMessageOptions(options),
|
||||||
|
...opts,
|
||||||
fetchReply: true,
|
fetchReply: true,
|
||||||
ephemeral: opts?.ephemeral,
|
|
||||||
})
|
})
|
||||||
return createReacordMessage(message)
|
return createReacordMessage(message)
|
||||||
},
|
},
|
||||||
followUp: async (options) => {
|
followUp: async (options) => {
|
||||||
const message = await interaction.followUp({
|
const message = await interaction.followUp({
|
||||||
...getDiscordMessageOptions(options),
|
...getDiscordMessageOptions(options),
|
||||||
|
...opts,
|
||||||
fetchReply: true,
|
fetchReply: true,
|
||||||
ephemeral: opts?.ephemeral,
|
|
||||||
})
|
})
|
||||||
return createReacordMessage(message)
|
return createReacordMessage(message)
|
||||||
},
|
},
|
||||||
@@ -285,7 +400,7 @@ export class ReacordDiscordJs extends Reacord {
|
|||||||
|
|
||||||
reply: (content?: ReactNode) =>
|
reply: (content?: ReactNode) =>
|
||||||
this.createInstance(
|
this.createInstance(
|
||||||
this.createInteractionReplyRenderer(interaction),
|
this.createInteractionReplyRenderer(interaction, {}),
|
||||||
content,
|
content,
|
||||||
),
|
),
|
||||||
|
|
||||||
|
|||||||
@@ -23,9 +23,10 @@ export abstract class Reacord {
|
|||||||
|
|
||||||
constructor(private readonly config: ReacordConfig = {}) {}
|
constructor(private readonly config: ReacordConfig = {}) {}
|
||||||
|
|
||||||
abstract send(...args: unknown[]): ReacordInstance
|
// There's no more need in abstract methods
|
||||||
abstract reply(...args: unknown[]): ReacordInstance
|
// abstract send(...args: unknown[]): ReacordInstance
|
||||||
abstract ephemeralReply(...args: unknown[]): ReacordInstance
|
// abstract reply(...args: unknown[]): ReacordInstance
|
||||||
|
// abstract ephemeralReply(...args: unknown[]): ReacordInstance
|
||||||
|
|
||||||
protected handleComponentInteraction(interaction: ComponentInteraction) {
|
protected handleComponentInteraction(interaction: ComponentInteraction) {
|
||||||
for (const renderer of this.renderers) {
|
for (const renderer of this.renderers) {
|
||||||
|
|||||||
@@ -42,14 +42,14 @@ export class ReacordTester extends Reacord {
|
|||||||
return [...this.messageContainer]
|
return [...this.messageContainer]
|
||||||
}
|
}
|
||||||
|
|
||||||
override send(initialContent?: ReactNode): ReacordInstance {
|
public send(initialContent?: ReactNode): ReacordInstance {
|
||||||
return this.createInstance(
|
return this.createInstance(
|
||||||
new ChannelMessageRenderer(new TestChannel(this.messageContainer)),
|
new ChannelMessageRenderer(new TestChannel(this.messageContainer)),
|
||||||
initialContent,
|
initialContent,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override reply(initialContent?: ReactNode): ReacordInstance {
|
public reply(initialContent?: ReactNode): ReacordInstance {
|
||||||
return this.createInstance(
|
return this.createInstance(
|
||||||
new InteractionReplyRenderer(
|
new InteractionReplyRenderer(
|
||||||
new TestCommandInteraction(this.messageContainer),
|
new TestCommandInteraction(this.messageContainer),
|
||||||
@@ -58,7 +58,7 @@ export class ReacordTester extends Reacord {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override ephemeralReply(initialContent?: ReactNode): ReacordInstance {
|
public ephemeralReply(initialContent?: ReactNode): ReacordInstance {
|
||||||
return this.reply(initialContent)
|
return this.reply(initialContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user