0

Create a Discord forum channel thread

by
Published 18 days ago

Create a thread in a Discord forum channel with an opening message. Returns the new thread ID. Forum channels in Discord are channel containers where each post is a thread (a 'forum post'). Use this script to start a new forum post programmatically - useful for opening tickets, support threads, bug reports, or any cross-tool bridge that creates a Discord-side thread keyed to an external resource. For messages in the resulting thread (replies), use the existing 'Send message to Discord channel using Discord Bot' script (https://hub.windmill.dev/scripts/discord/8859/) - the thread ID returned here is what you pass as channelId. The configuration object follows the existing 'discord_bot_configuration' resource convention from the Hub. Per that convention, the public_key field is used as the bot token (https://discord.com/developers/applications -> Bot -> Reset Token). For a complete Teams to Discord bridge using this script, see the Windmill blog post: https://www.windmill.dev/blog/teams-discord-bridge

Script discord Verified

The script

Submitted by hugo989 Bun
Verified 7 days ago
1
// Create a thread in a Discord forum channel with an opening message.
2
// Returns the new thread ID, which can be used as channelId in subsequent
3
// "send message to channel" calls (e.g., script 8859) to post replies in
4
// that thread.
5

6
type DiscordBotConfiguration = {
7
  // Bot token, sent as `Authorization: Bot <token>`. Preferred field.
8
  bot_token?: string;
9
  // Ed25519 key for verifying interactions. Legacy: older resources stored the
10
  // bot token here, so we fall back to it for backward compatibility.
11
  public_key?: string;
12
  application_id: string;
13
};
14

15
export async function main(
16
  config: DiscordBotConfiguration,
17
  forum_channel_id: string,
18
  title: string,
19
  content: string,
20
): Promise<{ thread_id: string }> {
21
  const trimmedTitle = title.length > 100 ? title.slice(0, 97) + "..." : title;
22
  const truncatedContent = content.slice(0, 2000);
23

24
  const url = `https://discord.com/api/v10/channels/${forum_channel_id}/threads`;
25
  const res = await fetch(url, {
26
    method: "POST",
27
    headers: {
28
      authorization: `Bot ${config.bot_token ?? config.public_key}`,
29
      "content-type": "application/json",
30
    },
31
    body: JSON.stringify({
32
      name: trimmedTitle,
33
      message: { content: truncatedContent },
34
    }),
35
  });
36

37
  if (!res.ok) {
38
    throw new Error(
39
      `Discord forum-thread create failed: ${res.status} ${await res.text().catch(() => "")}`,
40
    );
41
  }
42

43
  const data = (await res.json()) as { id: string };
44
  return { thread_id: data.id };
45
}
46