Send a message to discord using webhook

Script discord Verified

by lplit ยท 7/30/2022

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 days ago
1
//native
2

3
type DiscordWebhook = {
4
  webhook_url: string;
5
};
6
export async function main(discord_webhook: DiscordWebhook, message: string) {
7
  const response = await fetch(`${discord_webhook.webhook_url}?wait=true`, {
8
    method: "POST",
9
    headers: {
10
      "Content-Type": "application/json",
11
    },
12
    body: JSON.stringify({ content: message }),
13
  });
14
  if (!response.ok) {
15
    throw new Error(`${response.status} ${await response.text()}`);
16
  }
17
  return await response.json();
18
}
19

Other submissions
  • Submitted by lplit Deno
    Created 396 days ago
    1
    import discordwebhook from "https://deno.land/x/discordwebhook/mod.ts";
    2
    
    
    3
    type DiscordWebhook = {
    4
      webhook_url: string;
    5
    };
    6
    export async function main(discord_webhook: DiscordWebhook, message: string) {
    7
      const webhook = new discordwebhook(discord_webhook.webhook_url);
    8
      const ret = await webhook.createMessage(message);
    9
      return ret;
    10
    }
    11
    
    
  • Submitted by joshuarussell.online513 Bun
    Created 347 days ago
    1
    import discordwebhook from "https://deno.land/x/discordwebhook/mod.ts";
    2
    
    
    3
    type DiscordWebhook = {
    4
      webhook_url: string;
    5
    };
    6
    
    
    7
    export async function main(discord_webhook: DiscordWebhook, message: string) {
    8
      // Normalize legacy Discord webhook URLs to the current domain
    9
      const normalized_url = discord_webhook.webhook_url.replace("discordapp.com", "discord.com");
    10
    
    
    11
      const webhook = new discordwebhook(normalized_url);
    12
      const ret = await webhook.createMessage(message);
    13
      return ret;
    14
    }
    15