Skip to content

Commit

Permalink
refactor: removed code and split options (#5918)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
kyranet committed Jun 26, 2021
1 parent 0d0c8f0 commit 985d4d6
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 101 deletions.
54 changes: 1 addition & 53 deletions src/structures/APIMessage.js
Expand Up @@ -104,7 +104,7 @@ class APIMessage {

/**
* Makes the content of this message.
* @returns {?(string|string[])}
* @returns {?string}
*/
makeContent() {
let content;
Expand All @@ -114,27 +114,6 @@ class APIMessage {
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;
}

Expand Down Expand Up @@ -232,37 +211,6 @@ class APIMessage {
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.
* @param {BufferResolvable|Stream|FileOptions|MessageAttachment} fileLike Something that could be resolved to a file
Expand Down
1 change: 0 additions & 1 deletion src/structures/Message.js
Expand Up @@ -554,7 +554,6 @@ class Message extends Base {
* @typedef {Object} MessageEditOptions
* @property {?string} [content] Content to be 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 {MessageFlags} [flags] Which flags to set for the message. Only `SUPPRESS_EMBEDS` can be edited.
* @property {MessageAttachment[]} [attachments] An array of attachments to keep,
Expand Down
4 changes: 0 additions & 4 deletions src/structures/Webhook.js
Expand Up @@ -166,10 +166,6 @@ class Webhook {
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();
return this.client.api
.webhooks(this.id, this.token)
Expand Down
17 changes: 0 additions & 17 deletions src/structures/interfaces/TextBasedChannel.js
Expand Up @@ -62,9 +62,6 @@ class TextBasedChannel {
* @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)
* @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]
* Action rows containing interactive components for the message (buttons, select menus)
*/
Expand Down Expand Up @@ -98,16 +95,6 @@ class TextBasedChannel {
* @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.
* @typedef {Object} ReplyOptions
Expand Down Expand Up @@ -177,10 +164,6 @@ class TextBasedChannel {
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();
return this.client.api.channels[this.id].messages
.post({ data, files })
Expand Down
10 changes: 10 additions & 0 deletions src/util/Util.js
Expand Up @@ -55,6 +55,16 @@ class Util {
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.
* @param {string} text Content to split
Expand Down
29 changes: 7 additions & 22 deletions typings/index.d.ts
Expand Up @@ -228,10 +228,9 @@ declare module 'discord.js' {
): APIMessage;
public static resolveFile(fileLike: BufferResolvable | Stream | FileOptions | MessageAttachment): Promise<unknown>;

public makeContent(): string | string[] | undefined;
public makeContent(): string | undefined;
public resolveData(): this;
public resolveFiles(): Promise<this>;
public split(): APIMessage[];
}

export abstract class Application {
Expand Down Expand Up @@ -1186,12 +1185,7 @@ declare module 'discord.js' {
export class InteractionWebhook extends PartialWebhookMixin() {
constructor(client: Client, id: Snowflake, token: string);
public token: string;
public send(
options: string | APIMessage | (InteractionReplyOptions & { split?: false }),
): Promise<Message | RawMessage>;
public send(
options: APIMessage | (InteractionReplyOptions & { split: true | SplitOptions }),
): Promise<(Message | RawMessage)[]>;
public send(options: string | APIMessage | InteractionReplyOptions): Promise<Message | RawMessage>;
}

export class Invite extends Base {
Expand Down Expand Up @@ -1298,8 +1292,7 @@ declare module 'discord.js' {
public pin(): Promise<Message>;
public react(emoji: EmojiIdentifierResolvable): Promise<MessageReaction>;
public removeAttachments(): Promise<Message>;
public reply(options: string | APIMessage | (ReplyMessageOptions & { split?: false })): Promise<Message>;
public reply(options: APIMessage | (ReplyMessageOptions & { split: true | SplitOptions })): Promise<Message[]>;
public reply(options: string | APIMessage | ReplyMessageOptions): Promise<Message>;
public startThread(
name: string,
autoArchiveDuration: ThreadAutoArchiveDuration,
Expand Down Expand Up @@ -2132,8 +2125,7 @@ declare module 'discord.js' {
options: string | APIMessage | WebhookEditMessageOptions,
): Promise<RawMessage>;
public fetchMessage(message: Snowflake, cache?: boolean): Promise<RawMessage>;
public send(options: string | APIMessage | (WebhookMessageOptions & { split?: false })): Promise<RawMessage>;
public send(options: APIMessage | (WebhookMessageOptions & { split: true | SplitOptions })): Promise<RawMessage[]>;
public send(options: string | APIMessage | WebhookMessageOptions): Promise<RawMessage>;
}

export class WebSocketManager extends EventEmitter {
Expand Down Expand Up @@ -2595,8 +2587,7 @@ declare module 'discord.js' {
interface PartialTextBasedChannelFields {
lastMessageID: Snowflake | null;
readonly lastMessage: Message | null;
send(options: string | APIMessage | (MessageOptions & { split?: false })): Promise<Message>;
send(options: APIMessage | (MessageOptions & { split: true | SplitOptions })): Promise<Message[]>;
send(options: string | APIMessage | MessageOptions): Promise<Message>;
}

interface TextBasedChannelFields extends PartialTextBasedChannelFields {
Expand Down Expand Up @@ -2635,10 +2626,7 @@ declare module 'discord.js' {
options: string | APIMessage | WebhookEditMessageOptions,
): Promise<Message | RawMessage>;
fetchMessage(message: Snowflake | '@original', cache?: boolean): Promise<Message | RawMessage>;
send(
options: APIMessage | (WebhookMessageOptions & { split: true | SplitOptions }),
): Promise<(Message | RawMessage)[]>;
send(options: string | APIMessage | (WebhookMessageOptions & { split?: false })): Promise<Message | RawMessage>;
send(options: string | APIMessage | WebhookMessageOptions): Promise<Message | RawMessage>;
}

interface WebhookFields extends PartialWebhookFields {
Expand Down Expand Up @@ -3652,7 +3640,6 @@ declare module 'discord.js' {
attachments?: MessageAttachment[];
content?: string | null;
embeds?: (MessageEmbed | MessageEmbedOptions)[] | null;
code?: string | boolean;
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];
flags?: BitFieldResolvable<MessageFlagsString, number>;
allowedMentions?: MessageMentionOptions;
Expand Down Expand Up @@ -3753,13 +3740,11 @@ declare module 'discord.js' {
interface MessageOptions {
tts?: boolean;
nonce?: string | number;
content?: string;
content?: string | null;
embeds?: (MessageEmbed | MessageEmbedOptions)[];
components?: MessageActionRow[] | MessageActionRowOptions[] | MessageActionRowComponentResolvable[][];
allowedMentions?: MessageMentionOptions;
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];
code?: string | boolean;
split?: boolean | SplitOptions;
reply?: ReplyOptions;
}

Expand Down
5 changes: 1 addition & 4 deletions typings/index.ts
Expand Up @@ -27,9 +27,8 @@ client.on('messageReactionRemoveAll', async message => {
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 assertIsMessageArray: (m: Promise<Message[]>) => void;

client.on('message', ({ channel }) => {
assertIsMessage(channel.send('string'));
Expand All @@ -42,8 +41,6 @@ client.on('message', ({ channel }) => {
assertIsMessage(channel.send({ embeds: [embed] }));
assertIsMessage(channel.send({ embeds: [embed], files: [attachment] }));

assertIsMessageArray(channel.send({ split: true }));

// @ts-expect-error
channel.send();
// @ts-expect-error
Expand Down

0 comments on commit 985d4d6

Please sign in to comment.