Edits history of script submission #22309 for ' Create a Discord forum channel thread (discord)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    // Create a thread in a Discord forum channel with an opening message.
    // Returns the new thread ID, which can be used as channelId in subsequent
    // "send message to channel" calls (e.g., script 8859) to post replies in
    // that thread.
    
    type DiscordBotConfiguration = {
      // Bot token, sent as `Authorization: Bot <token>`. Preferred field.
      bot_token?: string;
      // Ed25519 key for verifying interactions. Legacy: older resources stored the
      // bot token here, so we fall back to it for backward compatibility.
      public_key?: string;
      application_id: string;
    };
    
    export async function main(
      config: DiscordBotConfiguration,
      forum_channel_id: string,
      title: string,
      content: string,
    ): Promise<{ thread_id: string }> {
      const trimmedTitle = title.length > 100 ? title.slice(0, 97) + "..." : title;
      const truncatedContent = content.slice(0, 2000);
    
      const url = `https://discord.com/api/v10/channels/${forum_channel_id}/threads`;
      const res = await fetch(url, {
        method: "POST",
        headers: {
          authorization: `Bot ${config.bot_token ?? config.public_key}`,
          "content-type": "application/json",
        },
        body: JSON.stringify({
          name: trimmedTitle,
          message: { content: truncatedContent },
        }),
      });
    
      if (!res.ok) {
        throw new Error(
          `Discord forum-thread create failed: ${res.status} ${await res.text().catch(() => "")}`,
        );
      }
    
      const data = (await res.json()) as { id: string };
      return { thread_id: data.id };
    }
    

    Submitted by hugo989 8 days ago

  • bun
    // Create a thread in a Discord forum channel with an opening message.
    // Returns the new thread ID, which can be used as channelId in subsequent
    // "send message to channel" calls (e.g., script 8859) to post replies in
    // that thread.
    
    type DiscordBotConfiguration = {
      // Per the existing Windmill Hub convention for discord_bot_configuration
      // (matches script 8859), the bot token is stored in the `public_key` field.
      public_key: string;
      application_id: string;
    };
    
    export async function main(
      config: DiscordBotConfiguration,
      forum_channel_id: string,
      title: string,
      content: string,
    ): Promise<{ thread_id: string }> {
      const trimmedTitle = title.length > 100 ? title.slice(0, 97) + "..." : title;
      const truncatedContent = content.slice(0, 2000);
    
      const url = `https://discord.com/api/v10/channels/${forum_channel_id}/threads`;
      const res = await fetch(url, {
        method: "POST",
        headers: {
          authorization: `Bot ${config.public_key}`,
          "content-type": "application/json",
        },
        body: JSON.stringify({
          name: trimmedTitle,
          message: { content: truncatedContent },
        }),
      });
    
      if (!res.ok) {
        throw new Error(
          `Discord forum-thread create failed: ${res.status} ${await res.text().catch(() => "")}`,
        );
      }
    
      const data = (await res.json()) as { id: string };
      return { thread_id: data.id };
    }
    

    Submitted by hugo989 19 days ago