all repos — stealth-developers @ 124009fe8d16aefd3401cb00740df5e6d26d6a44

apps/bot/src/lib/channels.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
import type { GuildTextBasedChannel, TextChannel } from "discord.js";

import { ChannelType } from "discord.js";

import { client } from "@/client";

export async function getTextChannel(id: string, mustBeSendable = true): Promise<TextChannel> {
	const channel = await client.channels.fetch(id);
	if (!channel) throw new Error(`Channel not found: ${id}`);
	if (channel.type !== ChannelType.GuildText) throw new Error(`Channel is not text-based: ${id}`);

	if (!mustBeSendable) return channel;
	if (!channel.isSendable()) throw new Error(`Channel is not sendable: ${id}`);

	return channel;
}

export async function getSendableChannel(id: string): Promise<GuildTextBasedChannel> {
	const channel = await client.channels.fetch(id);
	if (!channel) throw new Error(`Channel not found: ${id}`);
	if (!channel.isSendable() && !channel.isTextBased())
		throw new Error(`Channel is not sendable or text-based: ${id}`);
	if (!("guild" in channel)) throw new Error(`Channel is not sendable: ${id}`);

	return channel;
}

export async function getCategory(id: string) {
	const channel = await client.channels.fetch(id);
	if (!channel) throw new Error(`Category not found: ${id}`);
	if (channel.type !== ChannelType.GuildCategory)
		throw new Error(`Channel is not a category: ${id}`);

	return channel;
}