mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-21 14:04:38 +02:00
Local splitmessage
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
const util = require('util');
|
const util = require('util');
|
||||||
const discord = require('discord.js');
|
const discord = require('discord.js');
|
||||||
const tags = require('common-tags');
|
const tags = require('common-tags');
|
||||||
const { escapeRegex } = require('../../util/Util');
|
const { escapeRegex, splitMessage } = require('../../util/Util');
|
||||||
const Command = require('../../framework/Command');
|
const Command = require('../../framework/Command');
|
||||||
|
|
||||||
const nl = '!!NL!!';
|
const nl = '!!NL!!';
|
||||||
@@ -88,14 +88,14 @@ module.exports = class EvalCommand extends Command {
|
|||||||
const prepend = `\`\`\`javascript\n${prependPart}\n`;
|
const prepend = `\`\`\`javascript\n${prependPart}\n`;
|
||||||
const append = `\n${appendPart}\n\`\`\``;
|
const append = `\n${appendPart}\n\`\`\``;
|
||||||
if (input) {
|
if (input) {
|
||||||
return discord.splitMessage(tags.stripIndents`
|
return splitMessage(tags.stripIndents`
|
||||||
*Executed in ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ''}${hrDiff[1] / 1000000}ms.*
|
*Executed in ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ''}${hrDiff[1] / 1000000}ms.*
|
||||||
\`\`\`javascript
|
\`\`\`javascript
|
||||||
${inspected}
|
${inspected}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
`, { maxLength: 1900, prepend, append });
|
`, { maxLength: 1900, prepend, append });
|
||||||
} else {
|
} else {
|
||||||
return discord.splitMessage(tags.stripIndents`
|
return splitMessage(tags.stripIndents`
|
||||||
*Callback executed after ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ''}${hrDiff[1] / 1000000}ms.*
|
*Callback executed after ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ''}${hrDiff[1] / 1000000}ms.*
|
||||||
\`\`\`javascript
|
\`\`\`javascript
|
||||||
${inspected}
|
${inspected}
|
||||||
|
|||||||
@@ -408,4 +408,33 @@ module.exports = class Util {
|
|||||||
if (spoilers !== 0 && (spoilers && (spoilers % 2))) clean += '||';
|
if (spoilers !== 0 && (spoilers && (spoilers % 2))) clean += '||';
|
||||||
return clean;
|
return clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static splitMessage(text, { maxLength = 2000, 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);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user