-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-to-discord.mjs
executable file
·104 lines (83 loc) · 3.02 KB
/
post-to-discord.mjs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env zx
// require installation of google/zx
import { argv, fetch, fs } from 'zx'
$.verbose = false
async function postToDiscord (webhookUrl, payload) {
// Split message into 2000 character chunks
const chunks = payload.match(/[\s\S]{1,2000}/g) || []
for (let chunk of chunks) {
const content = {
content: chunk,
username: 'Techlynx'
}
try {
const response = await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(content)
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
// Sleep for 1.5 second to avoid hitting rate limits
await new Promise(resolve => setTimeout(resolve, 1500))
} catch (error) {
console.error('An error occurred:', error)
}
}
console.log('Message posted successfully')
}
async function postToDiscordWithFile (webhookUrl, payload, filePath) {
const boundary = `----WebKitFormBoundary${Math.random()
.toString(36)
.substring(2, 15)}`
const fileData = fs.readFileSync(filePath)
const fileName = filePath.split('/').pop()
// Construct the multipart/form-data body
let body = `--${boundary}\r\nContent-Disposition: form-data; name="content"\r\n\r\n${payload}\r\n`
body += `--${boundary}\r\nContent-Disposition: form-data; name="username"\r\n\r\nTechlynx\r\n`
body += `--${boundary}\r\nContent-Disposition: form-data; name="file"; filename="${fileName}"\r\nContent-Type: application/octet-stream\r\n\r\n`
body += fileData
body += `\r\n--${boundary}--`
try {
const response = await fetch(webhookUrl, {
method: 'POST',
body: body,
headers: {
'Content-Type': `multipart/form-data; boundary=${boundary}`
}
})
if (!response.ok) {
const errorBody = await response.text()
console.log('response', errorBody)
throw new Error(`HTTP error! status: ${response.status}`)
}
console.log('Message posted successfully with file')
} catch (error) {
console.error('An error occurred:', error)
}
}
const WEBHOOK_URL = argv['webhook-url']
let changes
changes = (await $`./git-utils/changelog.mjs --since="1 week ago" > ./changes-past-week.md`).toString()
postToDiscordWithFile(WEBHOOK_URL, 'This is our changelog throughout our repos :fire:', './changes-past-week.md')
// changes = (await $`./git-utils/changelog.mjs --repositories api`).toString()
// postToDiscord(WEBHOOK_URL, changes)
// changes = (
// await $`./git-utils/changelog.mjs --repositories partner-app`
// ).toString()
// postToDiscord(WEBHOOK_URL, changes)
// changes = (
// await $`./git-utils/changelog.mjs --repositories client-app`
// ).toString()
// postToDiscord(WEBHOOK_URL, changes)
// changes = (
// await $`./git-utils/changelog.mjs --repositories templ8-ui`
// ).toString()
// postToDiscord(WEBHOOK_URL, changes)
// changes = (
// await $`./git-utils/changelog.mjs --repositories templ8-helpers`
// ).toString()
// postToDiscord(WEBHOOK_URL, changes)