0
Send message to Discord channel using Discord Bot
One script reply has been approved by the moderators Verified

Send message to Discord channel using apps: https://discord.com/developers/applications

Created by henri186 166 days ago Viewed 364 times
0
Submitted by henri186 Bun
Verified 166 days ago
1
type DiscordBotConfiguration = {
2
  public_key: string,
3
  application_id: string
4
};
5

6
// Note: you can also send messages to channels using Discord webhooks: https://hub.windmill.dev/scripts/discord/1288/
7

8
import { RequestInit } from 'node-fetch';
9

10
export async function main(
11
  config: DiscordBotConfiguration,
12
  channelId: string,
13
  message: string
14
): Promise<any> {
15
  const url = `https://discord.com/api/v9/channels/${channelId}/messages`;
16

17
  const payload = {
18
    content: message
19
  };
20

21
  const requestOptions: RequestInit = {
22
    method: 'POST',
23
    headers: {
24
      'Content-Type': 'application/json',
25
      'Authorization': `Bot ${config.public_key}`
26
    },
27
    body: JSON.stringify(payload)
28
  };
29

30
  const response = await fetch(url, requestOptions);
31

32
  return await response.json();
33
}