/** * @copyright nhcarrigan * @license Naomi's Public License * @author Naomi Carrigan */ /* eslint-disable @typescript-eslint/naming-convention -- we are making raw API calls. */ import type { AnnouncementType } from "../interfaces/announcementType.js"; const channelIds: Record = { community: "1386105484313886820", company: "1422472775695728661", products: "1386105452881776661", }; const roleIds: Record = { community: "1386107941224054895", /** * Note that this is not a role ID, but the server ID. * Company announcements ping everyone. */ company: "1354624415861833870", products: "1386107909699666121", }; /** * Forwards an announcement to our Discord server. * @param title - The title of the announcement. * @param content - The main body of the announcement. * @param type - Whether the announcement is for a product or community. * @returns A message indicating the success or failure of the operation. */ export const announceOnDiscord = async( title: string, content: string, type: AnnouncementType, ): Promise => { const messageRequest = await fetch( `https://discord.com/api/v10/channels/${channelIds[type]}/messages`, { body: JSON.stringify({ allowed_mentions: { parse: [ "users", "roles" ] }, content: `# ${title}\n\n${content}\n-# <@&${roleIds[type]}>`, }), headers: { "Authorization": `Bot ${process.env.DISCORD_TOKEN ?? ""}`, "Content-Type": "application/json", }, method: "POST", }, ); if (messageRequest.status !== 200) { return `Failed to send message to Discord. Status: ${messageRequest.status.toString()} ${messageRequest.statusText}`; } // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- fetch does not accept generics. const message = await messageRequest.json() as { id?: string }; if (message.id === undefined) { return `Failed to parse message ID, cannot crosspost. ${JSON.stringify(message)}`; } const crosspostRequest = await fetch( `https://discord.com/api/v10/channels/${channelIds[type]}/messages/${message.id}/crosspost`, { headers: { "Authorization": `Bot ${process.env.DISCORD_TOKEN ?? ""}`, "Content-Type": "application/json", }, method: "POST", }, ); if (!crosspostRequest.ok) { return `Failed to crosspost message to Discord. Status: ${crosspostRequest.status.toString()} ${crosspostRequest.statusText}`; } return "Successfully sent and published message to Discord."; };