Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/discord

src/fetch-channel.ts

30 lines · 1.0 KB · TypeScript
import { discordBotRequest } from './bot.ts'
import { inputRecord, snowflake } from './validation.ts'

type DiscordChannel = {
	id?: string
	type?: number
	name?: string
	guild_id?: string
	parent_id?: string
	topic?: string | null
}

/**
 * Fetch one Discord channel or thread by id through the bot-token lane.
 *
 * @param params.secretName - Bot-token secret; default `discordBotToken`.
 */
export default async function fetchChannel(params: Record<string, unknown> = {}) {
	const input = inputRecord(params)
	const channelId = snowflake(input, 'channelId', ['channel_id'])
	const channel = (await discordBotRequest('/channels/' + channelId, input)) as DiscordChannel
	return {
		id: channel.id ?? channelId,
		type: typeof channel.type === 'number' ? channel.type : null,
		name: typeof channel.name === 'string' ? channel.name : null,
		guildId: typeof channel.guild_id === 'string' ? channel.guild_id : null,
		parentId: typeof channel.parent_id === 'string' ? channel.parent_id : null,
		topic: typeof channel.topic === 'string' ? channel.topic : null,
	}
}