This repository has been archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
monitor.js
127 lines (97 loc) · 4.73 KB
/
monitor.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
const Discord = require("discord.js");
const axios = require("axios");
const sqlite3 = require("sqlite3").verbose();
const path = require("path");
const meta = {
Authorization: "Bearer " + process.env.CANVAS_TOKEN,
};
module.exports = (client, courseAnn) => {
const dbPath = path.resolve(__dirname, "./commands/db/memory.db");
//Api request to get announcements
axios.get(courseAnn, /*{ headers: meta }*/).then(function (response) {
const data = response.data;
const status = response.status;
//check if request is oke
if (status === 200) {
let db = new sqlite3.Database(dbPath, sqlite3.OPEN_READWRITE, (err) => {
if (err) {
console.error(err.message);
}
});
//select posted at and course id from courses table
db.serialize(() => {
//Get posted_at, course_id in a array
db.all(`SELECT posted_at, course_id FROM courses;`, (err, row) => {
if (err) {
console.error(err.message);
}
//Reverse JSON data order from API Canvas to show oldest posts first
var rev = data.reverse();
//Loop trough dtabase with course with all the API GET results to post on the right channel
rev.forEach((ann) => {
row.forEach((course) => {
if (ann.context_code.replace("course_", "") === JSON.stringify(course.course_id) && course.posted_at < ann.posted_at) {
//Replace default markup html tags of canvas by discord embed syntax
var message = ann.message
.replace(/<strong>/g, "**")
.replace(/<\/strong>/g, "**")
.replace(/<[^>]+>/g, "")
.replace(
/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/g,
"(LINK)"
)
.replace(/\(Links to an external site.\)/g, "");
//Build layout of embedded post
const exampleEmbed = new Discord.MessageEmbed()
.setColor("DARK_VIVID_PINK")
.setTitle(ann.title)
.setURL(ann.url)
.setAuthor(
ann.author.display_name,
ann.author.avatar_image_url,
ann.author.html_url
)
.setDescription(message.substring(0, 200))
.addFields({
name: "___________",
value: "[Read More](" + ann.url + ")",
})
.setThumbnail(
"https://scontent-bru2-1.xx.fbcdn.net/v/t1.0-9/46488296_2389319437749114_6505762562989096960_n.jpg?_nc_cat=111&ccb=2&_nc_sid=09cbfe&_nc_ohc=a58UoF9Yv_cAX-4H65n&_nc_ht=scontent-bru2-1.xx&oh=c1b0d140980bd3dc2a474ec029a01170&oe=5FF9B2D6"
)
.setTimestamp()
.setFooter(
"NxT Media Technology",
"https://play-lh.googleusercontent.com/2_M-EEPXb2xTMQSTZpSUefHR3TjgOCsawM3pjVG47jI-BrHoXGhKBpdEHeLElT95060B=s180"
);
db.all(
`SELECT course_id, channel_id FROM watchlist;`,
(err, row) => {
if (err) {
console.error(err.message);
}
row.forEach((record) => {
if (ann.context_code.replace("course_", "") === JSON.stringify(record.course_id)) {
/* channel = client.channels.cache.get(record.channel_id).then(
channel.send(exampleEmbed)
) */
client.channels.fetch(record.channel_id).then(channel => { channel.send(exampleEmbed) }).catch(error => { console.log(error) });;
}
});
}
);
//update DateTime so you have a reference when the last post on discord was posted, prevent posting doubles
db.run(`UPDATE courses SET posted_at=` + JSON.stringify(ann.posted_at) + ` WHERE course_id = ` + JSON.stringify(course.course_id)), (err, row) => {
if (err) {
console.error(err.message);
}
console.log("Your records had been pushed to the DB succesfully!");
};
}
});
});
});
});
}
});
};