mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
refactor(Util): remove splitting (#7780)
This commit is contained in:
@@ -52,51 +52,6 @@ class Util extends null {
|
||||
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(es) 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
|
||||
* @param {SplitOptions} [options] Options controlling the behavior of the split
|
||||
* @returns {string[]}
|
||||
*/
|
||||
static splitMessage(text, { maxLength = 2_000, char = '\n', prepend = '', append = '' } = {}) {
|
||||
text = Util.verifyString(text);
|
||||
if (text.length <= maxLength) return [text];
|
||||
let splitText = [text];
|
||||
if (Array.isArray(char)) {
|
||||
while (char.length > 0 && splitText.some(elem => elem.length > maxLength)) {
|
||||
const currentChar = char.shift();
|
||||
if (currentChar instanceof RegExp) {
|
||||
splitText = splitText.flatMap(chunk => chunk.match(currentChar));
|
||||
} else {
|
||||
splitText = splitText.flatMap(chunk => chunk.split(currentChar));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
splitText = text.split(char);
|
||||
}
|
||||
if (splitText.some(elem => elem.length > maxLength)) throw new RangeError('SPLIT_MAX_LEN');
|
||||
const messages = [];
|
||||
let msg = '';
|
||||
for (const chunk of splitText) {
|
||||
if (msg && (msg + char + chunk + append).length > maxLength) {
|
||||
messages.push(msg + append);
|
||||
msg = prepend;
|
||||
}
|
||||
msg += (msg && msg !== prepend ? char : '') + chunk;
|
||||
}
|
||||
return messages.concat(msg).filter(m => m);
|
||||
}
|
||||
|
||||
/**
|
||||
* Options used to escape markdown.
|
||||
* @typedef {Object} EscapeMarkdownOptions
|
||||
|
||||
8
packages/discord.js/typings/index.d.ts
vendored
8
packages/discord.js/typings/index.d.ts
vendored
@@ -2558,7 +2558,6 @@ export class Util extends null {
|
||||
route: string,
|
||||
reason?: string,
|
||||
): Promise<{ id: Snowflake; position: number }[]>;
|
||||
public static splitMessage(text: string, options?: SplitOptions): string[];
|
||||
public static resolveAutoArchiveMaxLimit(guild: Guild): number;
|
||||
}
|
||||
|
||||
@@ -5091,13 +5090,6 @@ export interface ShardingManagerOptions {
|
||||
|
||||
export { Snowflake };
|
||||
|
||||
export interface SplitOptions {
|
||||
maxLength?: number;
|
||||
char?: string | string[] | RegExp | RegExp[];
|
||||
prepend?: string;
|
||||
append?: string;
|
||||
}
|
||||
|
||||
export type StageInstanceResolvable = StageInstance | Snowflake;
|
||||
|
||||
export interface StartThreadOptions {
|
||||
|
||||
Reference in New Issue
Block a user