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
29
30
31
32
33
34
35
36
37
export async function noteToSelf(
{ body, dmId, userId, token }: { body: object; dmId: string; userId: string; token: string },
) {
if (!dmId) {
console.log("Fetching dmId...", { userId });
const channelResponse = await fetch("https://discord.com/api/users/@me/channels", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bot ${token}`,
},
body: JSON.stringify({ recipient_id: userId }),
});
if (channelResponse.status !== 200) {
return { status: channelResponse.status, dmId: undefined };
}
const { id: channelId } = await channelResponse.json();
dmId = channelId;
}
console.log("sending", body);
const dmResponse = await fetch(`https://discord.com/api/channels/${dmId}/messages`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bot ${token}`,
},
body: JSON.stringify(body),
});
console.log("recieved", dmResponse);
return { status: dmResponse.status, dmId };
}
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!
March 14, 2024