Public
Script
October 23, 2023
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { discordFetch } from "https://esm.town/v/vtdocs/discordFetch";
export const discordSendDM = async (
botToken: string,
recipientId: string,
content: string,
): Promise<string | undefined> => {
// First, we need a reference to a DM (we create, or get an existing DM)
// https://discord.com/developers/docs/resources/user#create-dm
const DM = await discordFetch(
botToken,
"/users/@me/channels",
"POST",
JSON.stringify({
recipient_id: recipientId,
}),
);
// Then, we send a message via the DM
await discordFetch(
botToken,
`/channels/${DM.id}/messages`,
"POST",
JSON.stringify({
content,
}),
);
return DM.id;
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023