|
| 1 | +/** |
| 2 | + * Posts a release announcement to a Discord channel. |
| 3 | + * |
| 4 | + * Usage: `bun .scripts/release/announce.ts <version> [--live]` |
| 5 | + * |
| 6 | + * Without `--live`, role mentions are shown but don't ping anyone, and the message isn't |
| 7 | + * crossposted. Used for previews in a private test channel. |
| 8 | + * |
| 9 | + * Env: `DISCORD_BOT_TOKEN`, `DISCORD_CHANNEL_ID`, and `DISCORD_ROLE_PRERELEASE`, |
| 10 | + * `DISCORD_ROLE_MINOR`, `DISCORD_ROLE_PATCH`, `DISCORD_ROLE_BREAKING`. |
| 11 | + */ |
| 12 | +import { readFileSync } from 'node:fs' |
| 13 | +import { |
| 14 | + type Changelog, |
| 15 | + type ReleasePing, |
| 16 | + getChangelogEntry, |
| 17 | + getReleasePings, |
| 18 | + getReleaseUrl, |
| 19 | + renderReleaseNotes, |
| 20 | +} from './releaseNotes' |
| 21 | + |
| 22 | +const DISCORD_API = 'https://discord.com/api/v10' |
| 23 | +const USER_AGENT = 'DiscordBot (https://github.com/Animated-Java/animated-java, 1.0)' |
| 24 | +const TEMPLATE_PATH = './.scripts/plugins/releaseNoteTemplates/discord_release_notes_template' |
| 25 | +const CHANGELOG_PATH = './src/pluginPackage/changelog.json' |
| 26 | +const IS_COMPONENTS_V2 = 1 << 15 |
| 27 | +// Shared by every text display in a Components V2 message. |
| 28 | +const MAX_TEXT_LENGTH = 4000 |
| 29 | +const ACCENT_COLOR = 0x00aced |
| 30 | +const BREAKING_LABEL = |
| 31 | + '<:BreakingEmoji0:1432852678903463976><:BreakingEmoji1:1432852680404893818><:BreakingEmoji2:1432852682091270144><:BreakingEmoji3:1432852683534110790>' |
| 32 | +const ROLE_ENV_VARS: Record<ReleasePing, string> = { |
| 33 | + prerelease: 'DISCORD_ROLE_PRERELEASE', |
| 34 | + minor: 'DISCORD_ROLE_MINOR', |
| 35 | + patch: 'DISCORD_ROLE_PATCH', |
| 36 | + breaking: 'DISCORD_ROLE_BREAKING', |
| 37 | +} |
| 38 | + |
| 39 | +function requireEnv(name: string) { |
| 40 | + const value = process.env[name] |
| 41 | + if (!value) throw new Error(`Missing environment variable ${name}`) |
| 42 | + return value |
| 43 | +} |
| 44 | + |
| 45 | +async function discordPost(path: string, body?: unknown) { |
| 46 | + const response = await fetch(DISCORD_API + path, { |
| 47 | + method: 'POST', |
| 48 | + headers: { |
| 49 | + Authorization: `Bot ${requireEnv('DISCORD_BOT_TOKEN')}`, |
| 50 | + 'Content-Type': 'application/json', |
| 51 | + 'User-Agent': USER_AGENT, |
| 52 | + }, |
| 53 | + body: body === undefined ? undefined : JSON.stringify(body), |
| 54 | + }) |
| 55 | + if (!response.ok) { |
| 56 | + throw new Error(`POST ${path} failed: ${response.status} ${await response.text()}`) |
| 57 | + } |
| 58 | + return (await response.json()) as { id: string } |
| 59 | +} |
| 60 | + |
| 61 | +async function main() { |
| 62 | + const [version, flag] = process.argv.slice(2) |
| 63 | + if (!version || (flag !== undefined && flag !== '--live')) { |
| 64 | + throw new Error('Usage: bun .scripts/release/announce.ts <version> [--live]') |
| 65 | + } |
| 66 | + const live = flag === '--live' |
| 67 | + const channelId = requireEnv('DISCORD_CHANNEL_ID') |
| 68 | + |
| 69 | + const changelog = JSON.parse(readFileSync(CHANGELOG_PATH, 'utf-8')) as Changelog |
| 70 | + const entry = getChangelogEntry(changelog, version) |
| 71 | + const roleIds = getReleasePings(version, entry).map(ping => requireEnv(ROLE_ENV_VARS[ping])) |
| 72 | + const notes = renderReleaseNotes(readFileSync(TEMPLATE_PATH, 'utf-8'), version, entry, { |
| 73 | + vars: { pings: roleIds.map(id => `<@&${id}>`).join(' ') }, |
| 74 | + maxLength: MAX_TEXT_LENGTH, |
| 75 | + breakingLabel: BREAKING_LABEL, |
| 76 | + }) |
| 77 | + |
| 78 | + const message = await discordPost(`/channels/${channelId}/messages`, { |
| 79 | + flags: IS_COMPONENTS_V2, |
| 80 | + allowed_mentions: { parse: [], roles: live ? roleIds : [] }, |
| 81 | + components: [ |
| 82 | + { |
| 83 | + type: 17, // Container |
| 84 | + accent_color: ACCENT_COLOR, |
| 85 | + components: [ |
| 86 | + { type: 10, content: notes }, // Text Display |
| 87 | + { |
| 88 | + type: 1, // Action Row |
| 89 | + components: [ |
| 90 | + { |
| 91 | + type: 2, |
| 92 | + style: 5, |
| 93 | + label: 'View on GitHub', |
| 94 | + url: getReleaseUrl(version), |
| 95 | + }, |
| 96 | + ], |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + ], |
| 101 | + }) |
| 102 | + console.log(`Posted v${version} announcement (message ${message.id}) to channel ${channelId}`) |
| 103 | + |
| 104 | + if (live) { |
| 105 | + await discordPost(`/channels/${channelId}/messages/${message.id}/crosspost`) |
| 106 | + console.log('Crossposted to following servers') |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +await main() |
0 commit comments