Skip to content
This repository was archived by the owner on Jun 20, 2018. It is now read-only.

Commit 3214a9e

Browse files
committed
Add keyword logger
1 parent bf4e23d commit 3214a9e

File tree

8 files changed

+115
-13
lines changed

8 files changed

+115
-13
lines changed

config/config.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
"mentionNotificator": {
1212
"inConsole": true,
1313
"inNotificationChannel": true,
14-
"logBlockedUsers": false
14+
"logBlockedUsers": false,
15+
"ignoredServers": ["SERVER ID"]
16+
},
17+
"keywordNotificator": {
18+
"inConsole": true,
19+
"inNotificationChannel": true,
20+
"logBlockedUsers": false,
21+
"caseInsensitive": true,
22+
"ignoredServers": ["SERVER ID"]
1523
},
1624
"eventNotificator": {
1725
"inConsole": true,

config/games.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[
2-
"catch that man!",
3-
"with you",
4-
"with fire",
5-
"@everyone",
6-
"hide and seek",
7-
"nothing",
8-
"with bad code",
9-
"...",
10-
";D"
2+
"catch that man!",
3+
"with you",
4+
"with fire",
5+
"@everyone",
6+
"hide and seek",
7+
"nothing",
8+
"with bad code",
9+
"...",
10+
";D"
1111
]

config/keywords.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
"If someone says this, you will be notified!",
3+
"This too.",
4+
"Bananas"
5+
]

self.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ self.config = config
3131
const counts = {
3232
msgsGot: 0,
3333
msgsSent: 0,
34-
mentionsGot: 0
34+
mentionsGot: 0,
35+
keywordsGot: 0
3536
}
3637

3738
const commands = {
@@ -156,6 +157,8 @@ self.on('ready', () => {
156157

157158
require('./src/plugins/MentionStalker.js')(self, log, config)
158159

160+
require('./src/plugins/KeywordLogger.js')(self, log, config)
161+
159162
self.connect().catch(err => log.err(err, 'Login'))
160163

161164
process.on('SIGINT', () => { self.disconnect({reconnect: false}); setTimeout(() => process.exit(0), 1000) })

src/plugins/KeywordLogger.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Created by TheRacingLion (https://github.com/TheRacingLion) [ 14 / 03 / 2017 ]
3+
-*Read LICENSE to know more about permissions*-
4+
5+
Keyword Notifier. Tracks specific keywords set in config and notifies them in the notification channel or in console.
6+
*/
7+
const moment = require('moment')
8+
const keywords = require('../../config/keywords.json')
9+
10+
module.exports = (self, log, config) => {
11+
self.on('messageCreate', (msg) => {
12+
if (!msg.author || !msg.channel.guild) return
13+
if (~config.keywordNotificator.ignoredServers.indexOf(msg.channel.guild.id)) return
14+
if (msg.author.id !== self.user.id && !msg.author.bot && keywords && keywords.length > 0) {
15+
for (let i = 0; i < keywords.length; i++) {
16+
let keyword = config.keywordNotificator.caseInsensitive ? ~msg.cleanContent.replace(/\n/g, ' ').toLowerCase().indexOf(keywords[i].toLowerCase()) : ~msg.content.indexOf(keywords[i])
17+
if (keyword) {
18+
self.counts.keywordsGot = self.counts.keywordsGot + 1
19+
if (!config.keywordNotificator.logBlockedUsers && self.relationships.has(msg.author.id)) {
20+
if (self.relationships.get(msg.author.id).type === 2) continue
21+
}
22+
if (config.keywordNotificator.inConsole) { log.keyword(msg, keywords[i]) }
23+
if (config.keywordNotificator.inNotificationChannel) {
24+
if (Object.keys(self.channelGuildMap).includes(config.notificationChannelID)) {
25+
self.getMessages(msg.channel.id, 4, msg.id).then(msgs => {
26+
self.createChannelWebhook(config.notificationChannelID, { name: msg.author.username }).then(w => {
27+
self.counts.msgsSent = self.counts.msgsSent + 1
28+
self.executeWebhook(w.id, w.token, {
29+
username: msg.author.username,
30+
avatarURL: msg.author.avatarURL,
31+
content: `**I mentioned keyword \`${keywords[i]}\` in** ${msg.channel.mention}!`,
32+
embeds: [{
33+
title: moment(msg.timestamp).format('ddd, Do of MMM @ HH:mm:ss'),
34+
author: {
35+
name: `${msg.author.username}#${msg.author.discriminator} (${msg.author.id})`,
36+
icon_url: msg.author.avatarURL
37+
},
38+
color: 16426522,
39+
fields: [
40+
{ name: msgs[2].author.username, value: msgs[2].cleanContent, inline: false },
41+
{ name: msgs[1].author.username, value: msgs[1].cleanContent, inline: false },
42+
{ name: msgs[0].author.username, value: msgs[0].cleanContent, inline: false },
43+
{ name: msg.author.username, value: msg.cleanContent, inline: false },
44+
{ name: 'Guild', value: `${msg.channel.guild.name}\n(${msg.channel.guild.id})`, inline: true },
45+
{ name: 'Channel', value: `#${msg.channel.name}\n(${msg.channel.id})`, inline: true },
46+
{ name: 'Msg', value: `(${msg.id})`, inline: true }
47+
]
48+
}]
49+
}).then(self.deleteWebhook(w.id, w.token))
50+
})
51+
})
52+
} else { log.err('Invalid config, "notificationChannelID". Must be a channel ID from a server you are in.', 'Mention Stalker'); process.exit() }
53+
}
54+
return
55+
}
56+
continue
57+
}
58+
return
59+
}
60+
return
61+
})
62+
}

src/plugins/Logger.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ module.exports = {
3939
logger('magenta', 'Mention', `|> ${chalk.bgYellow.bold(msg.channel.guild.name)}|> #${chalk.bgYellow.bold(msg.channel.name)}|> ${msg.author.username} (${msg.author.id}):\n\n${cleanMsg}\n`)
4040
}
4141
},
42+
keyword (msg, word = '') {
43+
if (typeof msg === 'object') {
44+
const cleanMsg = msg.cleanContent.replace(/\n/g, ' ')
45+
logger('magenta', `Keyword Mention: "${word}"`, `|> ${chalk.bgYellow.bold(msg.channel.guild.name)}|> #${chalk.bgYellow.bold(msg.channel.name)}|> ${msg.author.username} (${msg.author.id}):\n\n${cleanMsg}\n`)
46+
}
47+
},
4248
ready (self, config) {
4349
if (self.user.id !== config.ownerID) { configErr('Invalid ownerID. This ID does not match the ID of your token. It must be YOUR discord ID.') } else {
4450
console.log(chalk.cyan([

src/plugins/MentionStalker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const moment = require('moment')
88

99
module.exports = (self, log, config) => {
1010
self.on('messageCreate', (msg) => {
11-
if (msg.author.id !== self.user.id && msg.channel.guild && ~msg.content.indexOf(self.user.id)) {
11+
if (!msg.author || !msg.channel.guild) return
12+
if (~config.mentionNotificator.ignoredServers.indexOf(msg.channel.guild.id)) return
13+
if (msg.author.id !== self.user.id && ~msg.content.indexOf(self.user.id)) {
1214
self.counts.mentionsGot = self.counts.mentionsGot + 1
1315
if (!config.mentionNotificator.logBlockedUsers && self.relationships.has(msg.author.id)) {
1416
if (self.relationships.get(msg.author.id).type === 2) return

src/utils/ConfigValidator.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,29 @@ module.exports.check = function (config, log) {
4545
Err('mentionNotificator -> "inConsole"', 'bol')
4646
} else if (typeof config.mentionNotificator.inNotificationChannel !== 'boolean') {
4747
Err('mentionNotificator -> "inNotificationChannel"', 'bol')
48+
} else if (typeof config.mentionNotificator.logBlockedUsers !== 'boolean') {
49+
Err('mentionNotificator -> "logBlockedUsers"', 'bol')
50+
} else if (!Array.isArray(config.mentionNotificator.ignoredServers)) {
51+
Err('mentionNotificator -> "ignoredServers"', 'Must be an array')
52+
} else if (typeof config.keywordNotificator !== 'object') {
53+
Err('keywordNotificator', 'Must be an object with two options:\n\n"keywordNotificator": {\n "inConsole": true,\n "inNotificationChannel": true\n}')
54+
} else if (typeof config.keywordNotificator.inConsole !== 'boolean') {
55+
Err('keywordNotificator -> "inConsole"', 'bol')
56+
} else if (typeof config.keywordNotificator.inNotificationChannel !== 'boolean') {
57+
Err('keywordNotificator -> "inNotificationChannel"', 'bol')
58+
} else if (typeof config.keywordNotificator.logBlockedUsers !== 'boolean') {
59+
Err('keywordNotificator -> "logBlockedUsers"', 'bol')
60+
} else if (typeof config.keywordNotificator.caseInsensitive !== 'boolean') {
61+
Err('keywordNotificator -> "caseInsensitive"', 'bol')
62+
} else if (!Array.isArray(config.keywordNotificator.ignoredServers)) {
63+
Err('keywordNotificator -> "ignoredServers"', 'Must be an array')
4864
} else if (typeof config.eventNotificator !== 'object') {
4965
Err('eventNotificator', 'Must be an object with two options:\n\n"eventNotificator": {\n "inConsole": true,\n "inNotificationChannel": true\n}')
5066
} else if (typeof config.eventNotificator.inConsole !== 'boolean') {
5167
Err('eventNotificator -> "inConsole"', 'bol')
5268
} else if (typeof config.eventNotificator.inNotificationChannel !== 'boolean') {
5369
Err('eventNotificator -> "inNotificationChannel"', 'bol')
54-
} else if ((config.mentionNotificator.inNotificationChannel || config.eventNotificator.inNotificationChannel) && !(/^\d{17,18}/.test(config.notificationChannelID))) {
70+
} else if ((config.mentionNotificator.inNotificationChannel || config.eventNotificator.inNotificationChannel || config.keywordNotificator.inNotificationChannel) && !(/^\d{17,18}/.test(config.notificationChannelID))) {
5571
Err('notificationChannelID', 'Must be a channel ID from a server you are in.')
5672
} else if (!/#?([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})/i.test(config.defaultEmbedColor)) {
5773
Err('defaultEmbedColor', 'Must be a valid hex color code.')

0 commit comments

Comments
 (0)