Chunk long final answers in Discord editMessage instead of failing
A final answer over 2000 chars edited straight into the tracked progress message was rejected by Discord (content[BASE_TYPE_MAX_LENGTH]), leaving a failed edit + stale progress stub before the send fallback. editMessage now edits the tracked message with the first 2000-char chunk and sends the remainder as follow-up messages, so long finals land cleanly in order. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -675,6 +675,21 @@ export class DiscordChannel implements Channel {
|
||||
categoryPosition: channel.parent?.position ?? 999,
|
||||
});
|
||||
}
|
||||
|
||||
for (const jid of dcJids) {
|
||||
if (result.has(jid)) continue;
|
||||
const channelId = jid.replace(/^dc:/, '');
|
||||
const channel = await this.client.channels.fetch(channelId);
|
||||
if (channel && 'position' in channel) {
|
||||
const tc = channel as TextChannel;
|
||||
result.set(jid, {
|
||||
name: tc.name,
|
||||
position: tc.position,
|
||||
category: tc.parent?.name || '',
|
||||
categoryPosition: tc.parent?.position ?? 999,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Fallback: individual fetches
|
||||
for (const jid of dcJids) {
|
||||
@@ -769,7 +784,23 @@ export class DiscordChannel implements Channel {
|
||||
throw new Error(`Discord channel not found or not editable: ${jid}`);
|
||||
}
|
||||
const msg = await (channel as TextChannel).messages.fetch(messageId);
|
||||
await msg.edit(text);
|
||||
// Discord caps a single message (edits included) at 2000 chars. A long
|
||||
// final answer edited straight in is rejected with
|
||||
// content[BASE_TYPE_MAX_LENGTH]; edit the tracked message with the first
|
||||
// chunk and send the remainder as follow-up messages so nothing is
|
||||
// dropped and no failed-edit fallback is needed.
|
||||
const MAX_LENGTH = 2000;
|
||||
if (text.length <= MAX_LENGTH) {
|
||||
await msg.edit(text);
|
||||
} else {
|
||||
await msg.edit(text.slice(0, MAX_LENGTH));
|
||||
for (let i = MAX_LENGTH; i < text.length; i += MAX_LENGTH) {
|
||||
await (channel as TextChannel).send({
|
||||
content: text.slice(i, i + MAX_LENGTH),
|
||||
flags: MessageFlags.SuppressEmbeds,
|
||||
});
|
||||
}
|
||||
}
|
||||
logger.info(
|
||||
{
|
||||
jid,
|
||||
|
||||
Reference in New Issue
Block a user