-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.js
More file actions
105 lines (85 loc) · 3.5 KB
/
Copy pathhelpers.js
File metadata and controls
105 lines (85 loc) · 3.5 KB
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
105
const https = require('https');
const makeDiscordMessage = async function(item) {
return new Promise((resolve) => {
console.log(item.title.toString());
var type = item.title.startsWith('/u/') ? 'Comment' : 'Post';
const link = `[${item.title}](<${item.link}>)`;
console.log(type);
if(type == 'Post'){
//gets content for message where link type posts
if(item.content.toString().length != 0){
//janky regex on html, pls no kill.
//https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
var contentLink = item.content.match(/<span><a (?!<\/a>$)(.*?)(\[link\]<\/a>)/g).toString();
var contentLinkTrimmed = contentLink.substring(15, contentLink.length - 12);
console.log(contentLinkTrimmed);
}
if(item.link != contentLinkTrimmed){
console.log('item.link != contentLinkTrimmed')
//link post OR comment
type = 'Link';
}
}
console.log('Submission of type ' + type + ' detected');
let message = type + ' - ' + link + '\n';
message += item.isoDate.replace(/T|.000Z/g,' ').trim() + '\n' + '```';
type.match('Link') ? message += 'Linked to: ' + contentLinkTrimmed + '\n' : message += 'Wrote the following: ' + '\n';
message += item.contentSnippet.replace(/\[([link]+)\]|\[([comments]+)\]| +/g,' ').trim().substring(0, (2000 - (message.length + 4)));
message += '```-';
resolve(message);
});
};
const sleep = async function(seconds) {
return new Promise((resolve) => {
setTimeout(
() => { resolve(true) },
seconds*1000
);
});
};
class DiscordSender {
constructor(discordWebhookUrl) {
this.url = new URL(discordWebhookUrl)
this.options = {
hostname: this.url.hostname,
path: this.url.pathname,
port: 443,
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
};
async send(message) {
return new Promise((resolve, reject) => {
const req = https.request(
this.options, (res) => {
const chunks = [ ];
res.on('data', (chunk) => {
chunks.push(chunk);
});
res.on('error', (err) => {
reject(`Discord response callout error: ${err}`);
});
res.on('end', () => {
console.log(`statuscode: ${res.statusCode}`)
if (res.statusCode > 200 && res.statusCode >= 300) {
reject(`Discord callout statuscode: ${res.statuscode}. body: ${Buffer.concat(chunks).toString()}`);
} else {
resolve('Discord message sent');
};
});
}
);
req.on('error', (err) => {
reject(`Discord request callout error: ${err}`);
});
req.write(JSON.stringify({content: message.substring(0, 2000)}));
req.end();
});
};
};
// exports
exports.sleep = sleep;
exports.makeDiscordMessage = makeDiscordMessage;
exports.DiscordSender = DiscordSender;