|
| 1 | +import { Client } from '../client/client.ts' |
| 2 | +import type { AllMessageOptions } from '../managers/channels.ts' |
| 3 | +import { |
| 4 | + CreateThreadInForumPayload, |
| 5 | + GuildForumChannelPayload, |
| 6 | + GuildForumSortOrderTypes, |
| 7 | + GuildForumTagPayload, |
| 8 | + ModifyGuildForumChannelOption, |
| 9 | + ModifyGuildForumChannelPayload, |
| 10 | + ThreadChannelPayload |
| 11 | +} from '../types/channel.ts' |
| 12 | +import { CHANNEL } from '../types/endpoint.ts' |
| 13 | +import { transformComponent } from '../utils/components.ts' |
| 14 | +import { Embed } from './embed.ts' |
| 15 | +import { Emoji } from './emoji.ts' |
| 16 | +import { Guild } from './guild.ts' |
| 17 | +import { GuildThreadAvailableChannel } from './guildThreadAvailableChannel.ts' |
| 18 | +import { Message } from './message.ts' |
| 19 | +import { ThreadChannel } from './threadChannel.ts' |
| 20 | + |
| 21 | +export interface CreateThreadInForumOptions { |
| 22 | + /** 2-100 character channel name */ |
| 23 | + name: string |
| 24 | + /** duration in minutes to automatically archive the thread after recent activity, can be set to: 60, 1440, 4320, 10080 */ |
| 25 | + autoArchiveDuration?: number |
| 26 | + slowmode?: number | null |
| 27 | + message: string | AllMessageOptions |
| 28 | + appliedTags?: string[] | GuildForumTag[] |
| 29 | +} |
| 30 | + |
| 31 | +export class GuildForumTag { |
| 32 | + id!: string |
| 33 | + name!: string |
| 34 | + moderated!: boolean |
| 35 | + emojiID!: string |
| 36 | + emojiName!: string | null |
| 37 | + |
| 38 | + constructor(data: GuildForumTagPayload) { |
| 39 | + this.readFromData(data) |
| 40 | + } |
| 41 | + |
| 42 | + readFromData(data: GuildForumTagPayload): void { |
| 43 | + this.id = data.id ?? this.id |
| 44 | + this.name = data.name ?? this.name |
| 45 | + this.moderated = data.moderated ?? this.moderated |
| 46 | + this.emojiID = data.emoji_id ?? this.emojiID |
| 47 | + this.emojiName = data.emoji_name ?? this.emojiName |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export class GuildForumChannel extends GuildThreadAvailableChannel { |
| 52 | + availableTags!: GuildForumTag[] |
| 53 | + defaultReactionEmoji!: Emoji |
| 54 | + defaultSortOrder!: GuildForumSortOrderTypes |
| 55 | + |
| 56 | + constructor(client: Client, data: GuildForumChannelPayload, guild: Guild) { |
| 57 | + super(client, data, guild) |
| 58 | + this.readFromData(data) |
| 59 | + } |
| 60 | + |
| 61 | + readFromData(data: GuildForumChannelPayload): void { |
| 62 | + super.readFromData(data) |
| 63 | + this.availableTags = |
| 64 | + data.available_tags?.map((tag) => new GuildForumTag(tag)) ?? |
| 65 | + this.availableTags |
| 66 | + this.defaultReactionEmoji = |
| 67 | + data.default_reaction_emoji !== null |
| 68 | + ? new Emoji(this.client, { |
| 69 | + id: data.default_reaction_emoji.emoji_id, |
| 70 | + name: data.default_reaction_emoji.emoji_name |
| 71 | + }) |
| 72 | + : this.defaultReactionEmoji |
| 73 | + this.defaultSortOrder = data.default_sort_order ?? this.defaultSortOrder |
| 74 | + } |
| 75 | + |
| 76 | + async edit( |
| 77 | + options?: ModifyGuildForumChannelOption |
| 78 | + ): Promise<GuildForumChannel> { |
| 79 | + if (options?.defaultReactionEmoji !== undefined) { |
| 80 | + if (options.defaultReactionEmoji instanceof Emoji) { |
| 81 | + options.defaultReactionEmoji = { |
| 82 | + emoji_id: options.defaultReactionEmoji.id, |
| 83 | + emoji_name: options.defaultReactionEmoji.name |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + if (options?.availableTags !== undefined) { |
| 88 | + options.availableTags = options.availableTags?.map((tag) => { |
| 89 | + if (tag instanceof GuildForumTag) { |
| 90 | + return { |
| 91 | + id: tag.id, |
| 92 | + name: tag.name, |
| 93 | + moderated: tag.moderated, |
| 94 | + emoji_id: tag.emojiID, |
| 95 | + emoji_name: tag.emojiName |
| 96 | + } |
| 97 | + } |
| 98 | + return tag |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + const body: ModifyGuildForumChannelPayload = { |
| 103 | + name: options?.name, |
| 104 | + position: options?.position, |
| 105 | + permission_overwrites: options?.permissionOverwrites, |
| 106 | + parent_id: options?.parentID, |
| 107 | + nsfw: options?.nsfw, |
| 108 | + topic: options?.topic, |
| 109 | + rate_limit_per_user: options?.slowmode, |
| 110 | + default_auto_archive_duration: options?.defaultAutoArchiveDuration, |
| 111 | + default_thread_rate_limit_per_user: options?.defaultThreadSlowmode, |
| 112 | + default_sort_order: options?.defaultSortOrder, |
| 113 | + default_reaction_emoji: options?.defaultReactionEmoji, |
| 114 | + available_tags: options?.availableTags |
| 115 | + } |
| 116 | + |
| 117 | + const resp = await this.client.rest.patch(CHANNEL(this.id), body) |
| 118 | + |
| 119 | + return new GuildForumChannel(this.client, resp, this.guild) |
| 120 | + } |
| 121 | + |
| 122 | + override async startThread( |
| 123 | + options: CreateThreadInForumOptions, |
| 124 | + message?: string | AllMessageOptions | Message |
| 125 | + ): Promise<ThreadChannel> { |
| 126 | + if (options.message !== undefined) { |
| 127 | + message = options.message |
| 128 | + } |
| 129 | + if (message instanceof Message) { |
| 130 | + message = { |
| 131 | + content: message.content, |
| 132 | + embeds: message.embeds.map((embed) => new Embed(embed)), |
| 133 | + components: message.components |
| 134 | + } |
| 135 | + } else if (message instanceof Embed) { |
| 136 | + message = { |
| 137 | + embed: message |
| 138 | + } |
| 139 | + } else if (Array.isArray(message)) { |
| 140 | + message = { |
| 141 | + embeds: message |
| 142 | + } |
| 143 | + } else if (typeof message === 'string') { |
| 144 | + message = { |
| 145 | + content: message |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + const messageObject = { |
| 150 | + content: message?.content, |
| 151 | + embed: message?.embed, |
| 152 | + embeds: message?.embeds, |
| 153 | + file: message?.file, |
| 154 | + files: message?.files, |
| 155 | + allowed_mentions: message?.allowedMentions, |
| 156 | + components: |
| 157 | + message?.components !== undefined |
| 158 | + ? typeof message.components === 'function' |
| 159 | + ? message.components() |
| 160 | + : transformComponent(message.components) |
| 161 | + : undefined |
| 162 | + } |
| 163 | + |
| 164 | + if ( |
| 165 | + messageObject.content === undefined && |
| 166 | + messageObject.embed === undefined |
| 167 | + ) { |
| 168 | + messageObject.content = '' |
| 169 | + } |
| 170 | + |
| 171 | + const body: CreateThreadInForumPayload = { |
| 172 | + name: options.name, |
| 173 | + auto_archive_duration: options.autoArchiveDuration, |
| 174 | + rate_limit_per_user: options.slowmode, |
| 175 | + message: messageObject, |
| 176 | + applied_tags: options.appliedTags?.map((tag) => { |
| 177 | + if (tag instanceof GuildForumTag) { |
| 178 | + return tag.id |
| 179 | + } |
| 180 | + return tag |
| 181 | + }) |
| 182 | + } |
| 183 | + |
| 184 | + const resp: ThreadChannelPayload = await this.client.rest.api.channels[ |
| 185 | + this.id |
| 186 | + ].threads.post(body) |
| 187 | + const thread = new ThreadChannel(this.client, resp, this.guild) |
| 188 | + this.threads.set(thread.id, resp) |
| 189 | + return thread |
| 190 | + } |
| 191 | +} |
0 commit comments