fix(bot): acknowledge /changelog immediately and surface post-defer errors
Defer the reply before the permission check (uncached member fetch + DB lookups) so a slow preamble no longer trips Discord's 3-second window and shows "The application did not respond". Wrap all post-defer work in one try/catch that editReplies an error, and make channel resolution catch fetch errors so an unreachable DISCORD_CHANGELOG_CHANNEL_ID replies clearly instead of hanging the interaction. Also export compareVersions and make the shipped-file test assert newest-first rather than a hardcoded version.
This commit is contained in:
parent
16664b6d4e
commit
6bc7413cd7
3 changed files with 108 additions and 88 deletions
|
|
@ -36,13 +36,21 @@ interface VersionPost {
|
|||
messages: string[];
|
||||
}
|
||||
|
||||
/** Resolve the releases channel, falling back to the configured announce channel. */
|
||||
/**
|
||||
* Resolve the releases channel, falling back to the configured announce channel.
|
||||
* Returns null (never throws) when unset or unreachable, so a bad channel id
|
||||
* surfaces as a clear reply instead of a hung interaction.
|
||||
*/
|
||||
const resolveChannel = async (guild: Guild): Promise<TextBasedChannelFields | null> => {
|
||||
const channelId = botConfig.changelogChannelId ?? botConfig.announceChannelId;
|
||||
if (!channelId) return null;
|
||||
const channel = await guild.channels.fetch(channelId);
|
||||
if (!channel || !("send" in channel)) return null;
|
||||
return channel as TextBasedChannelFields;
|
||||
try {
|
||||
const channel = await guild.channels.fetch(channelId);
|
||||
if (!channel || !("send" in channel)) return null;
|
||||
return channel as TextBasedChannelFields;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -90,91 +98,98 @@ export const changelogCommand: BotCommand = {
|
|||
return;
|
||||
}
|
||||
|
||||
const member = await interaction.guild.members.fetch(interaction.user.id);
|
||||
if (!(await isStaff(member, payload))) {
|
||||
await interaction.reply({
|
||||
content: "You don't have permission to post a changelog.",
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Acknowledge immediately: the permission check (uncached member fetch +
|
||||
// DB lookups) and the file/Discord I/O below can exceed Discord's 3-second
|
||||
// interaction window, which otherwise shows "The application did not respond".
|
||||
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
|
||||
|
||||
const rangeArg = interaction.options.getString("range")?.trim();
|
||||
|
||||
let content: string;
|
||||
try {
|
||||
content = readFileSync(CHANGELOG_PATH, "utf8");
|
||||
const member = await interaction.guild.members.fetch(interaction.user.id);
|
||||
if (!(await isStaff(member, payload))) {
|
||||
await interaction.editReply("You don't have permission to post a changelog.");
|
||||
return;
|
||||
}
|
||||
|
||||
const rangeArg = interaction.options.getString("range")?.trim();
|
||||
|
||||
let content: string;
|
||||
try {
|
||||
content = readFileSync(CHANGELOG_PATH, "utf8");
|
||||
} catch (error) {
|
||||
await interaction.editReply(
|
||||
`Could not read ${path.basename(CHANGELOG_PATH)}: ${
|
||||
error instanceof Error ? error.message : "Unknown error"
|
||||
}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// A range uses "from..to" notation; anything else is a single version.
|
||||
let posts: VersionPost[];
|
||||
let summaryFrom = "";
|
||||
let summaryTo = "";
|
||||
if (rangeArg && rangeArg.includes("..")) {
|
||||
const [fromPart, toPart] = rangeArg.split("..");
|
||||
const from = fromPart?.trim() || undefined;
|
||||
const to = toPart?.trim() || undefined;
|
||||
const batch: ChangelogBatch = resolveChangelogRange(content, from, to);
|
||||
if (!batch.found || batch.versions.length === 0) {
|
||||
await interaction.editReply(
|
||||
from && to
|
||||
? `No changelog entries found between ${from} and ${to}.`
|
||||
: from
|
||||
? `Could not find version ${from}.`
|
||||
: `Could not find version ${to}.`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
posts = batch.versions.map((version, i) => ({ version, messages: batch.messages[i] }));
|
||||
summaryFrom = batch.versions[0];
|
||||
summaryTo = batch.versions[batch.versions.length - 1];
|
||||
} else {
|
||||
const single: ResolvedChangelog = resolveChangelog(content, rangeArg ?? undefined);
|
||||
if (!single.found || single.messages.length === 0) {
|
||||
await interaction.editReply(
|
||||
rangeArg
|
||||
? `No changelog entry found for version ${rangeArg}.`
|
||||
: "The changelog file has no entries to post.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
posts = [{ version: single.version ?? "", messages: single.messages }];
|
||||
summaryFrom = single.version ?? "";
|
||||
summaryTo = summaryFrom;
|
||||
}
|
||||
|
||||
const channel = await resolveChannel(interaction.guild);
|
||||
if (!channel) {
|
||||
await interaction.editReply(
|
||||
"The releases channel isn't configured or isn't reachable (set DISCORD_CHANGELOG_CHANNEL_ID, or DISCORD_ANNOUNCE_CHANNEL_ID as a fallback).",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (posts.length > WIDE_RANGE_THRESHOLD) {
|
||||
await interaction.editReply(
|
||||
`⚠️ Posting ${posts.length} changelog(s) (${summaryFrom} → ${summaryTo}). This may take a moment.`,
|
||||
);
|
||||
}
|
||||
|
||||
let totalMessages = 0;
|
||||
for (const post of posts) {
|
||||
totalMessages += await postVersion(channel, post.version, post.messages);
|
||||
}
|
||||
|
||||
await interaction.editReply(
|
||||
posts.length > 1
|
||||
? `Posted ${posts.length} changelog(s) for ${summaryFrom} → ${summaryTo} (${totalMessages} messages).`
|
||||
: `Posted **Changelog v${summaryFrom}**.`,
|
||||
);
|
||||
} catch (error) {
|
||||
await interaction.editReply(
|
||||
`Could not read ${path.basename(CHANGELOG_PATH)}: ${
|
||||
error instanceof Error ? error.message : "Unknown error"
|
||||
}`,
|
||||
);
|
||||
return;
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
payload.logger.error(`[Bot] /changelog failed: ${message}`);
|
||||
// The interaction is deferred, so a follow-up is always permitted.
|
||||
await interaction.editReply(`Changelog post failed: ${message}`).catch(() => {});
|
||||
}
|
||||
|
||||
// A range uses "from..to" notation; anything else is a single version.
|
||||
let posts: VersionPost[];
|
||||
let summaryFrom = "";
|
||||
let summaryTo = "";
|
||||
if (rangeArg && rangeArg.includes("..")) {
|
||||
const [fromPart, toPart] = rangeArg.split("..");
|
||||
const from = fromPart?.trim() || undefined;
|
||||
const to = toPart?.trim() || undefined;
|
||||
const batch: ChangelogBatch = resolveChangelogRange(content, from, to);
|
||||
if (!batch.found || batch.versions.length === 0) {
|
||||
await interaction.editReply(
|
||||
from && to
|
||||
? `No changelog entries found between ${from} and ${to}.`
|
||||
: from
|
||||
? `Could not find version ${from}.`
|
||||
: `Could not find version ${to}.`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
posts = batch.versions.map((version, i) => ({ version, messages: batch.messages[i] }));
|
||||
summaryFrom = batch.versions[0];
|
||||
summaryTo = batch.versions[batch.versions.length - 1];
|
||||
} else {
|
||||
const single: ResolvedChangelog = resolveChangelog(content, rangeArg ?? undefined);
|
||||
if (!single.found || single.messages.length === 0) {
|
||||
await interaction.editReply(
|
||||
rangeArg
|
||||
? `No changelog entry found for version ${rangeArg}.`
|
||||
: "The changelog file has no entries to post.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
posts = [{ version: single.version ?? "", messages: single.messages }];
|
||||
summaryFrom = single.version ?? "";
|
||||
summaryTo = summaryFrom;
|
||||
}
|
||||
|
||||
const channel = await resolveChannel(interaction.guild);
|
||||
if (!channel) {
|
||||
await interaction.editReply(
|
||||
"The releases channel isn't configured (set DISCORD_CHANGELOG_CHANNEL_ID, or DISCORD_ANNOUNCE_CHANNEL_ID as a fallback).",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (posts.length > WIDE_RANGE_THRESHOLD) {
|
||||
await interaction.editReply(
|
||||
`⚠️ Posting ${posts.length} changelog(s) (${summaryFrom} → ${summaryTo}). This may take a moment.`,
|
||||
);
|
||||
}
|
||||
|
||||
let totalMessages = 0;
|
||||
for (const post of posts) {
|
||||
totalMessages += await postVersion(channel, post.version, post.messages);
|
||||
}
|
||||
|
||||
await interaction.editReply(
|
||||
posts.length > 1
|
||||
? `Posted ${posts.length} changelog(s) for ${summaryFrom} → ${summaryTo} (${totalMessages} messages).`
|
||||
: `Posted **Changelog v${summaryFrom}**.`,
|
||||
);
|
||||
},
|
||||
};
|
||||
|
|
|
|||
|
|
@ -207,7 +207,7 @@ export interface ChangelogBatch {
|
|||
}
|
||||
|
||||
/** Numeric major.minor.patch comparison (CHANGELOG versions carry no prerelease). */
|
||||
const compareVersions = (a: string, b: string): number => {
|
||||
export const compareVersions = (a: string, b: string): number => {
|
||||
const parts = (v: string) => v.split(".").map((n) => Number.parseInt(n, 10) || 0);
|
||||
const pa = parts(a);
|
||||
const pb = parts(b);
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import path from "node:path";
|
|||
import { fileURLToPath } from "node:url";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
compareVersions,
|
||||
formatEntryDiscord,
|
||||
parseChangelog,
|
||||
resolveChangelog,
|
||||
|
|
@ -172,8 +173,12 @@ describe("real CHANGELOG.md", () => {
|
|||
const content = readFileSync(CHANGELOG_PATH, "utf8");
|
||||
const entries = parseChangelog(content);
|
||||
expect(entries.length).toBeGreaterThan(10);
|
||||
// The file is newest-first.
|
||||
expect(entries[0].version).toBe("0.8.0");
|
||||
// The file is newest-first: the first entry is the highest version.
|
||||
const highest = entries.reduce(
|
||||
(max, entry) => (compareVersions(entry.version, max) > 0 ? entry.version : max),
|
||||
entries[0].version,
|
||||
);
|
||||
expect(entries[0].version).toBe(highest);
|
||||
|
||||
const v040 = resolveChangelog(content, "0.4.0");
|
||||
expect(v040.found).toBe(true);
|
||||
|
|
|
|||
Loading…
Reference in a new issue