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

Commit

Permalink
Add keyword logger
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRacingLion committed Mar 15, 2017
1 parent bf4e23d commit 3214a9e
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 13 deletions.
10 changes: 9 additions & 1 deletion config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
"mentionNotificator": {
"inConsole": true,
"inNotificationChannel": true,
"logBlockedUsers": false
"logBlockedUsers": false,
"ignoredServers": ["SERVER ID"]
},
"keywordNotificator": {
"inConsole": true,
"inNotificationChannel": true,
"logBlockedUsers": false,
"caseInsensitive": true,
"ignoredServers": ["SERVER ID"]
},
"eventNotificator": {
"inConsole": true,
Expand Down
18 changes: 9 additions & 9 deletions config/games.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[
"catch that man!",
"with you",
"with fire",
"@everyone",
"hide and seek",
"nothing",
"with bad code",
"...",
";D"
"catch that man!",
"with you",
"with fire",
"@everyone",
"hide and seek",
"nothing",
"with bad code",
"...",
";D"
]
5 changes: 5 additions & 0 deletions config/keywords.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"If someone says this, you will be notified!",
"This too.",
"Bananas"
]
5 changes: 4 additions & 1 deletion self.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ self.config = config
const counts = {
msgsGot: 0,
msgsSent: 0,
mentionsGot: 0
mentionsGot: 0,
keywordsGot: 0
}

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

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

require('./src/plugins/KeywordLogger.js')(self, log, config)

self.connect().catch(err => log.err(err, 'Login'))

process.on('SIGINT', () => { self.disconnect({reconnect: false}); setTimeout(() => process.exit(0), 1000) })
Expand Down
62 changes: 62 additions & 0 deletions src/plugins/KeywordLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Created by TheRacingLion (https://github.com/TheRacingLion) [ 14 / 03 / 2017 ]
-*Read LICENSE to know more about permissions*-
Keyword Notifier. Tracks specific keywords set in config and notifies them in the notification channel or in console.
*/
const moment = require('moment')
const keywords = require('../../config/keywords.json')

module.exports = (self, log, config) => {
self.on('messageCreate', (msg) => {
if (!msg.author || !msg.channel.guild) return
if (~config.keywordNotificator.ignoredServers.indexOf(msg.channel.guild.id)) return
if (msg.author.id !== self.user.id && !msg.author.bot && keywords && keywords.length > 0) {
for (let i = 0; i < keywords.length; i++) {
let keyword = config.keywordNotificator.caseInsensitive ? ~msg.cleanContent.replace(/\n/g, ' ').toLowerCase().indexOf(keywords[i].toLowerCase()) : ~msg.content.indexOf(keywords[i])
if (keyword) {
self.counts.keywordsGot = self.counts.keywordsGot + 1
if (!config.keywordNotificator.logBlockedUsers && self.relationships.has(msg.author.id)) {
if (self.relationships.get(msg.author.id).type === 2) continue
}
if (config.keywordNotificator.inConsole) { log.keyword(msg, keywords[i]) }
if (config.keywordNotificator.inNotificationChannel) {
if (Object.keys(self.channelGuildMap).includes(config.notificationChannelID)) {
self.getMessages(msg.channel.id, 4, msg.id).then(msgs => {
self.createChannelWebhook(config.notificationChannelID, { name: msg.author.username }).then(w => {
self.counts.msgsSent = self.counts.msgsSent + 1
self.executeWebhook(w.id, w.token, {
username: msg.author.username,
avatarURL: msg.author.avatarURL,
content: `**I mentioned keyword \`${keywords[i]}\` in** ${msg.channel.mention}!`,
embeds: [{
title: moment(msg.timestamp).format('ddd, Do of MMM @ HH:mm:ss'),
author: {
name: `${msg.author.username}#${msg.author.discriminator} (${msg.author.id})`,
icon_url: msg.author.avatarURL
},
color: 16426522,
fields: [
{ name: msgs[2].author.username, value: msgs[2].cleanContent, inline: false },
{ name: msgs[1].author.username, value: msgs[1].cleanContent, inline: false },
{ name: msgs[0].author.username, value: msgs[0].cleanContent, inline: false },
{ name: msg.author.username, value: msg.cleanContent, inline: false },
{ name: 'Guild', value: `${msg.channel.guild.name}\n(${msg.channel.guild.id})`, inline: true },
{ name: 'Channel', value: `#${msg.channel.name}\n(${msg.channel.id})`, inline: true },
{ name: 'Msg', value: `(${msg.id})`, inline: true }
]
}]
}).then(self.deleteWebhook(w.id, w.token))
})
})
} else { log.err('Invalid config, "notificationChannelID". Must be a channel ID from a server you are in.', 'Mention Stalker'); process.exit() }
}
return
}
continue
}
return
}
return
})
}
6 changes: 6 additions & 0 deletions src/plugins/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ module.exports = {
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`)
}
},
keyword (msg, word = '') {
if (typeof msg === 'object') {
const cleanMsg = msg.cleanContent.replace(/\n/g, ' ')
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`)
}
},
ready (self, config) {
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 {
console.log(chalk.cyan([
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/MentionStalker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const moment = require('moment')

module.exports = (self, log, config) => {
self.on('messageCreate', (msg) => {
if (msg.author.id !== self.user.id && msg.channel.guild && ~msg.content.indexOf(self.user.id)) {
if (!msg.author || !msg.channel.guild) return
if (~config.mentionNotificator.ignoredServers.indexOf(msg.channel.guild.id)) return
if (msg.author.id !== self.user.id && ~msg.content.indexOf(self.user.id)) {
self.counts.mentionsGot = self.counts.mentionsGot + 1
if (!config.mentionNotificator.logBlockedUsers && self.relationships.has(msg.author.id)) {
if (self.relationships.get(msg.author.id).type === 2) return
Expand Down
18 changes: 17 additions & 1 deletion src/utils/ConfigValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,29 @@ module.exports.check = function (config, log) {
Err('mentionNotificator -> "inConsole"', 'bol')
} else if (typeof config.mentionNotificator.inNotificationChannel !== 'boolean') {
Err('mentionNotificator -> "inNotificationChannel"', 'bol')
} else if (typeof config.mentionNotificator.logBlockedUsers !== 'boolean') {
Err('mentionNotificator -> "logBlockedUsers"', 'bol')
} else if (!Array.isArray(config.mentionNotificator.ignoredServers)) {
Err('mentionNotificator -> "ignoredServers"', 'Must be an array')
} else if (typeof config.keywordNotificator !== 'object') {
Err('keywordNotificator', 'Must be an object with two options:\n\n"keywordNotificator": {\n "inConsole": true,\n "inNotificationChannel": true\n}')
} else if (typeof config.keywordNotificator.inConsole !== 'boolean') {
Err('keywordNotificator -> "inConsole"', 'bol')
} else if (typeof config.keywordNotificator.inNotificationChannel !== 'boolean') {
Err('keywordNotificator -> "inNotificationChannel"', 'bol')
} else if (typeof config.keywordNotificator.logBlockedUsers !== 'boolean') {
Err('keywordNotificator -> "logBlockedUsers"', 'bol')
} else if (typeof config.keywordNotificator.caseInsensitive !== 'boolean') {
Err('keywordNotificator -> "caseInsensitive"', 'bol')
} else if (!Array.isArray(config.keywordNotificator.ignoredServers)) {
Err('keywordNotificator -> "ignoredServers"', 'Must be an array')
} else if (typeof config.eventNotificator !== 'object') {
Err('eventNotificator', 'Must be an object with two options:\n\n"eventNotificator": {\n "inConsole": true,\n "inNotificationChannel": true\n}')
} else if (typeof config.eventNotificator.inConsole !== 'boolean') {
Err('eventNotificator -> "inConsole"', 'bol')
} else if (typeof config.eventNotificator.inNotificationChannel !== 'boolean') {
Err('eventNotificator -> "inNotificationChannel"', 'bol')
} else if ((config.mentionNotificator.inNotificationChannel || config.eventNotificator.inNotificationChannel) && !(/^\d{17,18}/.test(config.notificationChannelID))) {
} else if ((config.mentionNotificator.inNotificationChannel || config.eventNotificator.inNotificationChannel || config.keywordNotificator.inNotificationChannel) && !(/^\d{17,18}/.test(config.notificationChannelID))) {
Err('notificationChannelID', 'Must be a channel ID from a server you are in.')
} else if (!/#?([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})/i.test(config.defaultEmbedColor)) {
Err('defaultEmbedColor', 'Must be a valid hex color code.')
Expand Down

0 comments on commit 3214a9e

Please sign in to comment.