refactor: removed code and split options (#5918)

Co-authored-by: Jan <66554238+vaporox@users.noreply.github.com>

BREAKING CHANGE: Removed `APIMessage#split`
BREAKING CHANGE: Removed `MessageEditOptions#code`
BREAKING CHANGE: Removed `BaseMessageOptions#code`
BREAKING CHANGE: Removed `BaseMessageOptions#split`
This commit is contained in:
Antonio Román
2021-06-27 00:11:40 +02:00
committed by GitHub
parent 0d0c8f07f2
commit 985d4d6a43
7 changed files with 19 additions and 101 deletions

View File

@@ -104,7 +104,7 @@ class APIMessage {
/** /**
* Makes the content of this message. * Makes the content of this message.
* @returns {?(string|string[])} * @returns {?string}
*/ */
makeContent() { makeContent() {
let content; let content;
@@ -114,27 +114,6 @@ class APIMessage {
content = Util.verifyString(this.options.content, RangeError, 'MESSAGE_CONTENT_TYPE', false); content = Util.verifyString(this.options.content, RangeError, 'MESSAGE_CONTENT_TYPE', false);
} }
if (typeof content !== 'string') return content;
const isSplit = typeof this.options.split !== 'undefined' && this.options.split !== false;
const isCode = typeof this.options.code !== 'undefined' && this.options.code !== false;
const splitOptions = isSplit ? { ...this.options.split } : undefined;
if (content) {
if (isCode) {
const codeName = typeof this.options.code === 'string' ? this.options.code : '';
content = `\`\`\`${codeName}\n${Util.cleanCodeBlockContent(content)}\n\`\`\``;
if (isSplit) {
splitOptions.prepend = `${splitOptions.prepend || ''}\`\`\`${codeName}\n`;
splitOptions.append = `\n\`\`\`${splitOptions.append || ''}`;
}
}
if (isSplit) {
content = Util.splitMessage(content, splitOptions);
}
}
return content; return content;
} }
@@ -232,37 +211,6 @@ class APIMessage {
return this; return this;
} }
/**
* Converts this APIMessage into an array of APIMessages for each split content
* @returns {APIMessage[]}
*/
split() {
if (!this.data) this.resolveData();
if (!Array.isArray(this.data.content)) return [this];
const apiMessages = [];
for (let i = 0; i < this.data.content.length; i++) {
let data;
let opt;
if (i === this.data.content.length - 1) {
data = { ...this.data, content: this.data.content[i] };
opt = { ...this.options, content: this.data.content[i] };
} else {
data = { content: this.data.content[i], tts: this.data.tts, allowed_mentions: this.options.allowedMentions };
opt = { content: this.data.content[i], tts: this.data.tts, allowedMentions: this.options.allowedMentions };
}
const apiMessage = new APIMessage(this.target, opt);
apiMessage.data = data;
apiMessages.push(apiMessage);
}
return apiMessages;
}
/** /**
* Resolves a single file into an object sendable to the API. * Resolves a single file into an object sendable to the API.
* @param {BufferResolvable|Stream|FileOptions|MessageAttachment} fileLike Something that could be resolved to a file * @param {BufferResolvable|Stream|FileOptions|MessageAttachment} fileLike Something that could be resolved to a file

View File

@@ -554,7 +554,6 @@ class Message extends Base {
* @typedef {Object} MessageEditOptions * @typedef {Object} MessageEditOptions
* @property {?string} [content] Content to be edited * @property {?string} [content] Content to be edited
* @property {MessageEmbed[]|APIEmbed[]} [embeds] Embeds to be added/edited * @property {MessageEmbed[]|APIEmbed[]} [embeds] Embeds to be added/edited
* @property {string|boolean} [code] Language for optional codeblock formatting to apply
* @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content * @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content
* @property {MessageFlags} [flags] Which flags to set for the message. Only `SUPPRESS_EMBEDS` can be edited. * @property {MessageFlags} [flags] Which flags to set for the message. Only `SUPPRESS_EMBEDS` can be edited.
* @property {MessageAttachment[]} [attachments] An array of attachments to keep, * @property {MessageAttachment[]} [attachments] An array of attachments to keep,

View File

@@ -166,10 +166,6 @@ class Webhook {
apiMessage = APIMessage.create(this, options).resolveData(); apiMessage = APIMessage.create(this, options).resolveData();
} }
if (Array.isArray(apiMessage.data.content)) {
return Promise.all(apiMessage.split().map(this.send.bind(this)));
}
const { data, files } = await apiMessage.resolveFiles(); const { data, files } = await apiMessage.resolveFiles();
return this.client.api return this.client.api
.webhooks(this.id, this.token) .webhooks(this.id, this.token)

View File

@@ -62,9 +62,6 @@ class TextBasedChannel {
* @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content * @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content
* (see [here](https://discord.com/developers/docs/resources/channel#allowed-mentions-object) for more details) * (see [here](https://discord.com/developers/docs/resources/channel#allowed-mentions-object) for more details)
* @property {FileOptions[]|BufferResolvable[]|MessageAttachment[]} [files] Files to send with the message * @property {FileOptions[]|BufferResolvable[]|MessageAttachment[]} [files] Files to send with the message
* @property {string|boolean} [code] Language for optional codeblock formatting to apply
* @property {boolean|SplitOptions} [split=false] Whether or not the message should be split into multiple messages if
* it exceeds the character limit. If an object is provided, these are the options for splitting the message
* @property {MessageActionRow[]|MessageActionRowOptions[]|MessageActionRowComponentResolvable[][]} [components] * @property {MessageActionRow[]|MessageActionRowOptions[]|MessageActionRowComponentResolvable[][]} [components]
* Action rows containing interactive components for the message (buttons, select menus) * Action rows containing interactive components for the message (buttons, select menus)
*/ */
@@ -98,16 +95,6 @@ class TextBasedChannel {
* @property {string} [name='file.jpg'] Filename of the attachment * @property {string} [name='file.jpg'] Filename of the attachment
*/ */
/**
* Options for splitting a message.
* @typedef {Object} SplitOptions
* @property {number} [maxLength=2000] Maximum character length per message piece
* @property {string|string[]|RegExp|RegExp[]} [char='\n'] Character(s) or Regex(s) to split the message with,
* an array can be used to split multiple times
* @property {string} [prepend=''] Text to prepend to every piece except the first
* @property {string} [append=''] Text to append to every piece except the last
*/
/** /**
* Options for sending a message with a reply. * Options for sending a message with a reply.
* @typedef {Object} ReplyOptions * @typedef {Object} ReplyOptions
@@ -177,10 +164,6 @@ class TextBasedChannel {
apiMessage = APIMessage.create(this, options).resolveData(); apiMessage = APIMessage.create(this, options).resolveData();
} }
if (Array.isArray(apiMessage.data.content)) {
return Promise.all(apiMessage.split().map(this.send.bind(this)));
}
const { data, files } = await apiMessage.resolveFiles(); const { data, files } = await apiMessage.resolveFiles();
return this.client.api.channels[this.id].messages return this.client.api.channels[this.id].messages
.post({ data, files }) .post({ data, files })

View File

@@ -55,6 +55,16 @@ class Util {
return out; return out;
} }
/**
* Options for splitting a message.
* @typedef {Object} SplitOptions
* @property {number} [maxLength=2000] Maximum character length per message piece
* @property {string|string[]|RegExp|RegExp[]} [char='\n'] Character(s) or Regex(s) to split the message with,
* an array can be used to split multiple times
* @property {string} [prepend=''] Text to prepend to every piece except the first
* @property {string} [append=''] Text to append to every piece except the last
*/
/** /**
* Splits a string into multiple chunks at a designated character that do not exceed a specific length. * Splits a string into multiple chunks at a designated character that do not exceed a specific length.
* @param {string} text Content to split * @param {string} text Content to split

29
typings/index.d.ts vendored
View File

@@ -228,10 +228,9 @@ declare module 'discord.js' {
): APIMessage; ): APIMessage;
public static resolveFile(fileLike: BufferResolvable | Stream | FileOptions | MessageAttachment): Promise<unknown>; public static resolveFile(fileLike: BufferResolvable | Stream | FileOptions | MessageAttachment): Promise<unknown>;
public makeContent(): string | string[] | undefined; public makeContent(): string | undefined;
public resolveData(): this; public resolveData(): this;
public resolveFiles(): Promise<this>; public resolveFiles(): Promise<this>;
public split(): APIMessage[];
} }
export abstract class Application { export abstract class Application {
@@ -1186,12 +1185,7 @@ declare module 'discord.js' {
export class InteractionWebhook extends PartialWebhookMixin() { export class InteractionWebhook extends PartialWebhookMixin() {
constructor(client: Client, id: Snowflake, token: string); constructor(client: Client, id: Snowflake, token: string);
public token: string; public token: string;
public send( public send(options: string | APIMessage | InteractionReplyOptions): Promise<Message | RawMessage>;
options: string | APIMessage | (InteractionReplyOptions & { split?: false }),
): Promise<Message | RawMessage>;
public send(
options: APIMessage | (InteractionReplyOptions & { split: true | SplitOptions }),
): Promise<(Message | RawMessage)[]>;
} }
export class Invite extends Base { export class Invite extends Base {
@@ -1298,8 +1292,7 @@ declare module 'discord.js' {
public pin(): Promise<Message>; public pin(): Promise<Message>;
public react(emoji: EmojiIdentifierResolvable): Promise<MessageReaction>; public react(emoji: EmojiIdentifierResolvable): Promise<MessageReaction>;
public removeAttachments(): Promise<Message>; public removeAttachments(): Promise<Message>;
public reply(options: string | APIMessage | (ReplyMessageOptions & { split?: false })): Promise<Message>; public reply(options: string | APIMessage | ReplyMessageOptions): Promise<Message>;
public reply(options: APIMessage | (ReplyMessageOptions & { split: true | SplitOptions })): Promise<Message[]>;
public startThread( public startThread(
name: string, name: string,
autoArchiveDuration: ThreadAutoArchiveDuration, autoArchiveDuration: ThreadAutoArchiveDuration,
@@ -2132,8 +2125,7 @@ declare module 'discord.js' {
options: string | APIMessage | WebhookEditMessageOptions, options: string | APIMessage | WebhookEditMessageOptions,
): Promise<RawMessage>; ): Promise<RawMessage>;
public fetchMessage(message: Snowflake, cache?: boolean): Promise<RawMessage>; public fetchMessage(message: Snowflake, cache?: boolean): Promise<RawMessage>;
public send(options: string | APIMessage | (WebhookMessageOptions & { split?: false })): Promise<RawMessage>; public send(options: string | APIMessage | WebhookMessageOptions): Promise<RawMessage>;
public send(options: APIMessage | (WebhookMessageOptions & { split: true | SplitOptions })): Promise<RawMessage[]>;
} }
export class WebSocketManager extends EventEmitter { export class WebSocketManager extends EventEmitter {
@@ -2595,8 +2587,7 @@ declare module 'discord.js' {
interface PartialTextBasedChannelFields { interface PartialTextBasedChannelFields {
lastMessageID: Snowflake | null; lastMessageID: Snowflake | null;
readonly lastMessage: Message | null; readonly lastMessage: Message | null;
send(options: string | APIMessage | (MessageOptions & { split?: false })): Promise<Message>; send(options: string | APIMessage | MessageOptions): Promise<Message>;
send(options: APIMessage | (MessageOptions & { split: true | SplitOptions })): Promise<Message[]>;
} }
interface TextBasedChannelFields extends PartialTextBasedChannelFields { interface TextBasedChannelFields extends PartialTextBasedChannelFields {
@@ -2635,10 +2626,7 @@ declare module 'discord.js' {
options: string | APIMessage | WebhookEditMessageOptions, options: string | APIMessage | WebhookEditMessageOptions,
): Promise<Message | RawMessage>; ): Promise<Message | RawMessage>;
fetchMessage(message: Snowflake | '@original', cache?: boolean): Promise<Message | RawMessage>; fetchMessage(message: Snowflake | '@original', cache?: boolean): Promise<Message | RawMessage>;
send( send(options: string | APIMessage | WebhookMessageOptions): Promise<Message | RawMessage>;
options: APIMessage | (WebhookMessageOptions & { split: true | SplitOptions }),
): Promise<(Message | RawMessage)[]>;
send(options: string | APIMessage | (WebhookMessageOptions & { split?: false })): Promise<Message | RawMessage>;
} }
interface WebhookFields extends PartialWebhookFields { interface WebhookFields extends PartialWebhookFields {
@@ -3652,7 +3640,6 @@ declare module 'discord.js' {
attachments?: MessageAttachment[]; attachments?: MessageAttachment[];
content?: string | null; content?: string | null;
embeds?: (MessageEmbed | MessageEmbedOptions)[] | null; embeds?: (MessageEmbed | MessageEmbedOptions)[] | null;
code?: string | boolean;
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[]; files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];
flags?: BitFieldResolvable<MessageFlagsString, number>; flags?: BitFieldResolvable<MessageFlagsString, number>;
allowedMentions?: MessageMentionOptions; allowedMentions?: MessageMentionOptions;
@@ -3753,13 +3740,11 @@ declare module 'discord.js' {
interface MessageOptions { interface MessageOptions {
tts?: boolean; tts?: boolean;
nonce?: string | number; nonce?: string | number;
content?: string; content?: string | null;
embeds?: (MessageEmbed | MessageEmbedOptions)[]; embeds?: (MessageEmbed | MessageEmbedOptions)[];
components?: MessageActionRow[] | MessageActionRowOptions[] | MessageActionRowComponentResolvable[][]; components?: MessageActionRow[] | MessageActionRowOptions[] | MessageActionRowComponentResolvable[][];
allowedMentions?: MessageMentionOptions; allowedMentions?: MessageMentionOptions;
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[]; files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];
code?: string | boolean;
split?: boolean | SplitOptions;
reply?: ReplyOptions; reply?: ReplyOptions;
} }

View File

@@ -27,9 +27,8 @@ client.on('messageReactionRemoveAll', async message => {
console.log(`messageReactionRemoveAll - content: ${message.content}`); console.log(`messageReactionRemoveAll - content: ${message.content}`);
}); });
// These are to check that stuff is the right type // This is to check that stuff is the right type
declare const assertIsMessage: (m: Promise<Message>) => void; declare const assertIsMessage: (m: Promise<Message>) => void;
declare const assertIsMessageArray: (m: Promise<Message[]>) => void;
client.on('message', ({ channel }) => { client.on('message', ({ channel }) => {
assertIsMessage(channel.send('string')); assertIsMessage(channel.send('string'));
@@ -42,8 +41,6 @@ client.on('message', ({ channel }) => {
assertIsMessage(channel.send({ embeds: [embed] })); assertIsMessage(channel.send({ embeds: [embed] }));
assertIsMessage(channel.send({ embeds: [embed], files: [attachment] })); assertIsMessage(channel.send({ embeds: [embed], files: [attachment] }));
assertIsMessageArray(channel.send({ split: true }));
// @ts-expect-error // @ts-expect-error
channel.send(); channel.send();
// @ts-expect-error // @ts-expect-error