-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.ts
66 lines (51 loc) · 2.01 KB
/
bot.ts
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 { ETwitterStreamEvent } from "twitter-api-v2";
import { v1TwitterClient, v2TwitterClient } from "./config/config";
import { initializeBanList, checkIfBanned } from "./utils/banned/bannedUtils";
import http from "http";
const initializeBot = async () => {
//Health Check for Digital Ocean
http.createServer((req, res) => {
const data = {"status": "ok"};
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify(data));
}).listen(8080);
let banList = await initializeBanList();
// Get and delete old rules if needed
const rules = await v2TwitterClient.v2.streamRules();
if (rules.data?.length) await v2TwitterClient.v2.updateStreamRules({
delete: { ids: rules.data.map(rule => rule.id) },
});
// Add our rules
await v2TwitterClient.v2.updateStreamRules({
add: [{ value: "#TechIsHiring" }],
});
const stream = await v2TwitterClient.v2.searchStream({
'tweet.fields': ['referenced_tweets', 'author_id'],
expansions: ['referenced_tweets.id'],
});
// Enable auto reconnect
stream.autoReconnect = true;
stream.on(ETwitterStreamEvent.Data, async tweet => {
console.log(tweet);
console.log(tweet.includes);
const banned = checkIfBanned(tweet.data.author_id, banList) ||
tweet.includes?.tweets?.some(tweet => checkIfBanned(tweet.author_id, banList));
const permitted = !banned && tweet.data.text.toLowerCase().includes("#techishiring");
if(permitted){
try {
await v1TwitterClient.v1.post("favorites/create.json", { id: tweet.data.id });
await v1TwitterClient.v1.post(`statuses/retweet/${tweet.data.id}.json`);
return;
} catch (error) {
return console.log(error);
}
}
return console.log(`This tweet was not a permitted tweet: ${tweet.data.id}`);
});
setInterval(async () => {
const updatedBanList = await initializeBanList();
const isUpdated = !updatedBanList.has("");
if(isUpdated) banList = updatedBanList;
}, 1800000);
};
initializeBot();