Depending on the boost-status of the server and user, messages have some character limit that may be as low as 2000. This code will intelligently split (on line feeds or between sentences) your message and send several ones.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
async function respond(channel_id, event_id, content, tts) {
let limit = 2000;
while (content.length > limit) {
let index = getSplitIndex(content, limit);
await respondPaged(channel_id, event_id, content.substring(0, index), tts);
content = content.substring(index + (index < content.length && content[index] === '\n' ? 1 : 0), content.length);
}
return respondPaged(channel_id, event_id, content, tts);
}
function getSplitIndex(string, limit) {
let index = string.substring(0, limit).lastIndexOf('\n\n');
if (index <= 0) index = string.substring(0, limit).lastIndexOf('\n');
if (index <= 0) index = string.substring(0, limit - 1).lastIndexOf('.') + 1;
if (index <= 0) index = string.substring(0, limit - 1).lastIndexOf(',') + 1;
if (index <= 0) index = string.substring(0, limit - 1).lastIndexOf(' ') + 1;
if (index <= 0) index = limit;
return index;
}
async function respondPaged(channel_id, event_id, content, tts) {
return lib.discord.channels['@0.2.0'].messages.create({
channel_id: channel_id,
content: content,
tts: tts,
message_reference: {
message_id: event_id
}
});
}