-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
160 lines (148 loc) · 6.15 KB
/
index.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const Discord = require('discord.js'); //Discord.js
const dcClient = new Discord.Client();
const fetch = require('node-fetch'); //Node-fetch
const puppeteer = require('puppeteer'); //Puppeteer
const url = 'https://www.rockstargames.com/newswire';
const MongoClient = require('mongodb').MongoClient; //MongoDB
const uri = `${process.env.MONGO_CONNECTION_STR}`;
let mongoDBClient = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
// ------------- Functions -------------
const getSentArticles = async () => {
let sentArticles;
try {
mongoDBClient = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
await mongoDBClient.connect().then(console.log("Connected to MongoDB"));
sentArticles = await (await mongoDBClient.db("discord-bot").collection("gta-online-news-lock").find({}).toArray()).map(x => x.articleID);
console.log(`Sent articles collection: ${sentArticles}`);
} catch (ex){
console.error(ex);
} finally {
await mongoDBClient.close();
console.log("MongoDB Connection closed")
}
return sentArticles;
}
const addSentArticle = async (title) => {
try {
mongoDBClient = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
await mongoDBClient.connect().then(console.log("Connected to MongoDB, adding new item to collection"));
await mongoDBClient.db("discord-bot").collection("gta-online-news-lock").insertOne({
"articleID": title
});
} catch (ex){
console.error(ex);
} finally {
await mongoDBClient.close();
console.log("MongoDB Connection closed")
}
}
const readArticles = async () => {
console.log(`Getting data from ${url}`);
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox','--disable-setuid-sandbox'],
ignoreDefaultArgs: ['--disable-extensions']
});
const page = await browser.newPage();
await page.goto(url);
await page.waitForTimeout(5000);
const results = await page.evaluate(() => {
let articles = document.querySelectorAll('._30cQN4q');
const dataset = [...articles].map(a => ({
innerText: a.innerText,
innerHtml: a.innerHTML,
href: a.href,
}));
return dataset;
});
await browser.close();
return results;
}
const readLastGtaArticle = async (articles) => {
const gtaArticles = articles.filter(a => a.innerText.includes("GTA Online"));
const latestArticle = gtaArticles[0];
const articleUrl = latestArticle.href;
const title = latestArticle.innerText.substring(latestArticle.innerText.indexOf("\n") + 1, latestArticle.innerText.lastIndexOf("\n"));
const imgUrl = latestArticle.innerHtml.substring(latestArticle.innerHtml.indexOf("https://media"), latestArticle.innerHtml.indexOf(".jpg") + 4)
const subtitles = await getArticleBody(latestArticle.href);
const descMain = subtitles[0];
subtitles.shift();
const subfields = [...subtitles.map(s => ` - ${s}`)]; //Remove first subtitle from the array and adds indentions to the remaining array items
console.log(`Latest article title from Rockstar Newswire: ${title}`);
return ({
title: title,
imgUrl: imgUrl,
subtitles: subtitles,
descMain: descMain,
subfields: subfields,
articleUrl: articleUrl
})
}
const sendMessage = (dcChannel, title, descMain, subfields, articleUrl, imgUrl) => {
let isSent = false;
try {
dcChannel.send({embed: {
color: 3447003,
author: {
name: dcClient.user.username,
icon_url: dcClient.user.avatarURL
},
title: title,
description: `${descMain}\n\nMain updates this week:\n${subfields.join("\n")}`,
url: articleUrl,
image: {
url: imgUrl
},
timestamp: new Date(),
footer: 'https://i.imgur.com/wSTFkRM.png'
}});
console.log("Message was sent successfully!");
isSent = true;
} catch (error) {
console.error(error)
}
return isSent;
}
//Gets GTA online articles from Rockstar Newswire, extracts data and send message in case the article wasn't yet sent to the channel.
const checkUpdates = async (dcChannel) => {
let sentArticles = await getSentArticles();
const articles = await readArticles();
if (articles){
const {title, imgUrl, subtitles, descMain, subfields, articleUrl} = await readLastGtaArticle(articles);
console.log(sentArticles.includes(title) ? "Article was already sent to the channel" : "Sending article to the Discord channel, and adding to memory.");
if (!sentArticles.includes(title)) {
let isSent = sendMessage(dcChannel, title, descMain, subfields, articleUrl, imgUrl); //Send Discord message
if (isSent) addSentArticle(title); //Adding to MongoDB
}
} else {
console.error('No data was exported from webpage');
}
};
//Extracts h3 elements from latest GTA online article
const getArticleBody = async (url) => {
console.log("Getting latest article subtitles");
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox','--disable-setuid-sandbox'],
ignoreDefaultArgs: ['--disable-extensions']
});
const page = await browser.newPage();
await page.goto(url);
await page.waitForTimeout(10000);
const res = await page.evaluate(() => {
let subtitleElements = [...document.querySelectorAll('h3')];
let subtitles = subtitleElements.map(s => s.innerHTML);
return subtitles;
});
await browser.close();
return res;
};
// ------------- Main -------------
console.log("Logging in to Discord");
dcClient.login(process.env.TOKEN);
dcClient.once('ready', () => {
let dcChannel = dcClient.channels.cache.get(process.env.DC_CHANNEL_ID);
console.log(`Setting up bot on channel ${dcChannel}`);
checkUpdates(dcChannel);
setInterval(() => checkUpdates(dcChannel), parseInt(process.env.INTERVAL_MS))
});