-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugger.js
161 lines (139 loc) · 5.42 KB
/
debugger.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
161
// Discord.js and friends
const Discord = require('discord.js');
const client = new Discord.Client();
const config = require('./config.js');
// Official MongoDB Driver
const MongoClient = require('mongodb').MongoClient
const assert = require('assert');
// Logging switches
const debug = true;
const verbose = false;
// MongoDB URL
const mongoUrl = 'mongodb://dispatch-mongo:27017/dispatch-notify-bot';
//let entries = [];
let database = null;
const collectionName = 'matchwords';
const mongoOpts = {
autoReconnect: true,
w: 1,
keepAlive: 1
};
const onlyUnique = function(value, index, self) { return value && self.indexOf(value) === index; };
const toLower = function(x){ return x.toLowerCase(); };
// Reconnect to the database
/*(function() {
debug && console.log(`${database === null ? 'C' : 'Rec'}onnecting to MongoDB at ${mongoUrl}...`);
return MongoClient.connect(mongoUrl, mongoOpts, function(err, db) {
assert.equal(null, err);
console.log(`${database === null ? 'C' : 'Rec'}onnected to MongoDB at ${mongoUrl}`);
assert.notEqual(null, database = db);
});
})();*/
// Workaround... see https://github.com/hydrabolt/discord.js/issues/1685#issuecomment-315620118
client.on('disconnect', function () {
clearTimeout(client.ws.connection.ratelimit.resetTimer);
});
let shutdown = function() {
console.log("Stopping Dispatch Notify Bot...");
client.destroy(function () {
console.log("Logged out of Discord");
});
/*database.close(false, function(err, result) {
if (err) {
console.log("Error encountered closing MongoDB connection", err);
} else {
console.log("MongoDB connection closed successfully");
}
});*/
console.log("Dispatch Notify Bot has been stopped");
}
process.on( 'SIGTERM', function () { shutdown(); });
process.on( 'SIGINT', function () { shutdown(); });
process.on('unhandledRejection', console.error);
// Log startup events
debug && console.log("Starting Dispatch Debugger...");
client.on('ready', () => {
debug && console.log(`Logged in as ${client.user.tag} for debugging!`);
});
let lastNotification = null;
let prettyPrint = function(message, forward) {
// TODO: What to do with other messages?
// DEBUG ONLY: this can get pretty noisy
let d = new Date(message.createdTimestamp || message.timestamp);
debug && console.log(`[${d.toLocaleDateString()} ${d.toLocaleTimeString()}] ${message.author}: ${message.content}`);
message.embeds.forEach(msgEmbed => {
//console.log(" Author:", msgEmbed.author);
console.log(" Title:", msgEmbed.title);
console.log(" Description:", msgEmbed.description);
// console.log(" Color:", msgEmbed.color); // cannot convert to string
// console.log(" Client:", msgEmbed.client); // super verbose
//console.log(" Created At:", msgEmbed.createdAt);
//console.log(" Created Timestamp:", msgEmbed.createdTimestamp);
// console.log(" HexColor:", msgEmbed.hexColor); // probably cannot convert to string
console.log(" Image:", msgEmbed.image.url);
//console.log(" Provider:", msgEmbed.provider);
console.log(" Thumbnail:", msgEmbed.thumbnail.url);
console.log(" Type:", msgEmbed.type);
console.log(" Url:", msgEmbed.url);
//console.log(" Video:", msgEmbed.video);
//console.log(" Footer", msgEmbed.footer);
for (let i = 0; i < msgEmbed.fields.length; i++) {
let field = msgEmbed.fields[i];
console.log(` Field ${i+1} name:`, field.name);
console.log(` Field ${i+1} value:`, field.value);
}
if (lastNotification === `${msgEmbed.title} - ${msgEmbed.description}`) {
return;
}
lastNotification = `${msgEmbed.title} - ${msgEmbed.description}`;
// FIXME: For some reason, Girafarig notifications from Monocle come through as blank
if (forward || msgEmbed.title.indexOf("Girafarig") !== -1) {
//let embed = msgEmbedToRich(msgEmbed);
let name = msgEmbed.title.split(" -")[0];
let thumbnail = msgEmbed.thumbnail.url;
let image = msgEmbed.image.url;
let url = msgEmbed.url;
let location = message.content.replace(/ <.*>/, "");
let embed = new Discord.RichEmbed()
.setTitle(msgEmbed.title)
.setDescription(msgEmbed.description)
.setColor("#42f450")
.setTimestamp()
.setThumbnail(thumbnail)
.setImage(image)
//.setAuthor(name, thumbnail, url)
.setURL(url)
//.addField("Matched Word", "girafarig test", true)
console.log("Forwarding embed:", embed);
client.fetchUser(config.adminUser).then(u => {
u.send(`${location}: ${name}`, { embed: embed })
.then(message => {
debug && console.log(`Sent message to admin user:`);
prettyPrint(message, false);
})
.catch(err => console.log(`Failed to send message to admin user: ${err}`))
});
}
});
console.log("");
console.log("");
};
/**
* Handler for all messages received by the bot.
*/
client.on('message', message => {
// Notify approriate users when a notification comes
// TODO: Allow user to choose which words in which channels?
let channels = config.watchedChannels;
for(let i = 0; i < channels.length; i++) {
let wChannel = channels[i];
if (message.channel == wChannel) {
//handleNotification(message);
prettyPrint(message, false);
return;
}
}
});
// Login with our bot's access token
// Do NOT commit this token to source control
client.login(config.token);