import { discordBotRequest } from './bot.ts' import { boundedInteger, clipText, inputRecord, pickString, requireLiveMutation, snowflake } from './validation.ts' type DiscordThread = { id?: string name?: string parent_id?: string } /** * Create a public thread from an existing Discord message. * * Pass `dryRun: true` to preview, or `confirm: true` to create. Default * archive duration is 1440 minutes. * * @param params.secretName - Bot-token secret; default `discordBotToken`. */ export default async function createThread(params: Record = {}) { const input = inputRecord(params) const channelId = snowflake(input, 'channelId', ['channel_id']) const messageId = snowflake(input, 'messageId', ['message_id']) const threadName = clipText(pickString(input.threadName, input.thread_name, input.name) || 'Kody thread', 90) || 'Kody thread' const autoArchiveDuration = boundedInteger(input.autoArchiveDuration, 1440, 60, 10080, 'autoArchiveDuration') const body = { name: threadName, auto_archive_duration: autoArchiveDuration } if (requireLiveMutation(input, 'create-thread') === 'dryRun') { return { dryRun: true, threadId: 'dry-thread-' + messageId, threadName, parentChannelId: channelId, body, } } const response = (await discordBotRequest( '/channels/' + channelId + '/messages/' + messageId + '/threads', input, { method: 'POST', body: JSON.stringify(body) }, )) as DiscordThread return { id: response.id ?? null, threadId: response.id ?? null, threadName: response.name || threadName, parentChannelId: response.parent_id || channelId, } }