joel bot
This commit is contained in:
50
src/utils/discord.ts
Normal file
50
src/utils/discord.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Discord message utilities
|
||||
*/
|
||||
|
||||
/**
|
||||
* Split a message into chunks that fit within Discord's limit
|
||||
*/
|
||||
export function splitMessage(content: string, maxLength = 1900): string[] {
|
||||
if (content.length <= maxLength) {
|
||||
return [content];
|
||||
}
|
||||
|
||||
const chunks: string[] = [];
|
||||
let remaining = content;
|
||||
|
||||
while (remaining.length > 0) {
|
||||
if (remaining.length <= maxLength) {
|
||||
chunks.push(remaining);
|
||||
break;
|
||||
}
|
||||
|
||||
// Try to split at a natural break point
|
||||
let splitPoint = remaining.lastIndexOf("\n", maxLength);
|
||||
if (splitPoint === -1 || splitPoint < maxLength / 2) {
|
||||
splitPoint = remaining.lastIndexOf(" ", maxLength);
|
||||
}
|
||||
if (splitPoint === -1 || splitPoint < maxLength / 2) {
|
||||
splitPoint = maxLength;
|
||||
}
|
||||
|
||||
chunks.push(remaining.slice(0, splitPoint));
|
||||
remaining = remaining.slice(splitPoint).trimStart();
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape Discord markdown
|
||||
*/
|
||||
export function escapeMarkdown(text: string): string {
|
||||
return text.replace(/([*_`~|\\])/g, "\\$1");
|
||||
}
|
||||
|
||||
/**
|
||||
* Format code block
|
||||
*/
|
||||
export function codeBlock(content: string, language = ""): string {
|
||||
return `\`\`\`${language}\n${content}\n\`\`\``;
|
||||
}
|
||||
Reference in New Issue
Block a user