import { discordBotRequest } from './bot.ts' import { inputRecord, requireLiveMutation, snowflake } from './validation.ts' type EditedMessage = { id?: string channel_id?: string content?: string edited_timestamp?: string | null } /** * Edit a bot-authored Discord message in place. * * Only messages posted by the same bot can be edited. Pass `dryRun: true` to * preview, or `confirm: true` to send the PATCH. * * @param params.secretName - Bot-token secret; default `discordBotToken`. */ export default async function editMessage(params: Record = {}) { const input = inputRecord(params) const channelId = snowflake(input, 'channelId', ['channel_id']) const messageId = snowflake(input, 'messageId', ['message_id']) const content = typeof input.content === 'string' ? input.content : null const embeds = Array.isArray(input.embeds) ? (input.embeds as unknown[]).slice(0, 10) : null if (content == null && embeds == null) { throw new Error('content or embeds is required.') } const body: Record = { allowed_mentions: { replied_user: false } } if (content != null) body.content = content if (embeds != null) body.embeds = embeds const path = '/channels/' + channelId + '/messages/' + messageId if (requireLiveMutation(input, 'edit-message') === 'dryRun') { return { dryRun: true, channelId, messageId, path, body } } const response = (await discordBotRequest(path, input, { method: 'PATCH', body: JSON.stringify(body), })) as EditedMessage return { id: response.id ?? messageId, channelId: response.channel_id || channelId, content: response.content ?? content, editedTimestamp: response.edited_timestamp ?? null, } }