-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
66 lines (59 loc) · 1.92 KB
/
utils.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import 'dotenv/config';
import fetch from 'node-fetch';
import { verifyKey } from 'discord-interactions';
export function VerifyDiscordRequest(clientKey) {
return function (req, res, buf, encoding) {
const signature = req.get('X-Signature-Ed25519');
const timestamp = req.get('X-Signature-Timestamp');
const isValidRequest = verifyKey(buf, signature, timestamp, clientKey);
if (!isValidRequest) {
res.status(401).send('Bad request signature');
throw new Error('Bad request signature');
}
};
};
export async function DiscordRequest(endpoint, options) {
// append endpoint to root API URL
const url = 'https://discord.com/api/v9/' + endpoint;
// Stringify payloads
if (options.body) options.body = JSON.stringify(options.body);
// Use node-fetch to make requests
const res = await fetch(url, {
headers: {
Authorization: `Bot ${process.env.TOKEN}`,
'Content-Type': 'application/json; charset=UTF-8',
},
...options
});
// throw API errors
if (!res.ok) {
const data = await res.json();
console.log(res.status);
throw new Error(JSON.stringify(data));
}
// return original response
return res;
}
export async function getEmailJustSent(id) {
const url = `https://api.buttondown.email/v1/emails/${id}`
const res = await fetch(url, {
headers: {
Authorization: `Token ${process.env.BD_TOKEN}`
}
});
if (!res.ok) {
const data = await res.json();
console.log(res.status);
throw new Error(JSON.stringify(data));
}
return res.json();
};
export async function sendMessageJustSent(subject, issueNumber, url) {
const endpoint = `channels/${process.env.CHANNEL_ID}/messages`;
DiscordRequest(endpoint, {
method: 'POST',
body: {
content: `**${subject}**\nIssue ${issueNumber} of Until It's Not Fun is out now! Read it at ${url}.`
}
})
}