diff --git a/.gitignore b/.gitignore index 4ec18f2..2897b1b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .env .idea/ node_modules/ +npm-debug.log diff --git a/.travis.yml b/.travis.yml index 0d6120c..47c6959 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,13 +2,13 @@ language: node_js node_js: - '0.10' script: npm run jsHint -- --failTaskOnError +script: echo $BOT >> .env before_install: - export DISPLAY=:99.0 - sh -e /etc/init.d/xvfb start before_script: - npm install deploy: - before_deploy: echo $BOT >> .env provider: modulus api_key: $MODULUS_TOKEN project_name: miscord diff --git a/img/nicememe.jpg b/img/nicememe.jpg index f7f247a..78704a0 100644 Binary files a/img/nicememe.jpg and b/img/nicememe.jpg differ diff --git a/lib/eventLogging.js b/lib/eventLogging.js new file mode 100644 index 0000000..a2a6b55 --- /dev/null +++ b/lib/eventLogging.js @@ -0,0 +1,60 @@ +'use strict'; + +var dateFormat = require('dateformat'); + +/** + * Records the activities of users joining and leaving voice channels based on the given parameters. + * + * @param {String} event The given event + * @param {Object} bot The given bot Client + * @param {Object} channel the given channel + * @param {Object} user the given user + */ +function history(event, bot, channel, user) { + if (event === 'leave') { + bot.sendMessage(channel.server.channels.get('name', 'history'), '**' + user.username + '**' + ' left **' + channel.name + '** at **' + dateFormat(new Date(), 'mmmm dS, yyyy, h:MM:ss TT Z') + '**'); + } + + if (event === 'join') { + bot.sendMessage(channel.server.channels.get('name', 'history'), '**' + user.username + '**' + ' joined **' + channel.name + '** at **' + dateFormat(new Date(), 'mmmm dS, yyyy, h:MM:ss TT Z') + '**'); + } +} + +/** + * Records the activities of users starting and stopping a game based on the given parameters. + * + * @param {Object} oldUser The given old user state + * @param {Object} newUser The given new user state + * @param {Object} bot The given bot Client + */ +function presenceUpdated(oldUser, newUser, bot) { + for (var i = 0; i < bot.servers.length; i++) { + for (var j = 0; j < bot.servers[i].members.length; j++) { + if (bot.servers[i].members[j].id === newUser.id) { + var historyChannel = bot.servers[i].channels.get('name', 'history'); + if (oldUser.status !== newUser.status) { + var statusVerb; + if (newUser.status === 'offline' || newUser.status === 'idle') { + statusVerb = 'gone'; + } else { + statusVerb = 'come'; + } + bot.sendMessage(historyChannel, '**' + newUser.username + '**' + ' has ' + statusVerb + ' **' + newUser.status + '** at **' + dateFormat(new Date(), 'mmmm dS, yyyy, h:MM:ss TT Z') + '**'); + } + if (oldUser.game !== newUser.game) { + if (!newUser.game) { + bot.sendMessage(historyChannel, '**' + newUser.username + '**' + ' stopped playing **' + oldUser.game.name + '** at **' + dateFormat(new Date(), 'mmmm dS, yyyy, h:MM:ss TT Z') + '**'); + } else { + bot.sendMessage(historyChannel, '**' + newUser.username + '**' + ' started playing **' + newUser.game.name + '** at **' + dateFormat(new Date(), 'mmmm dS, yyyy, h:MM:ss TT Z') + '**'); + } + } + } + } + } +} + +module.exports = { + history: history, + presenceUpdated: presenceUpdated +}; + diff --git a/lib/history.js b/lib/history.js deleted file mode 100644 index 60789ad..0000000 --- a/lib/history.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -var dateFormat = require('dateformat'); - -/** - * Records the activities of users based on the given parameters. - * - * @param {String} event The given event - * @param {Object} bot The given bot Client - * @param {Object} channel the given channel - * @param {Object} user the given user - */ -function history(event, bot, channel, user) { - if (event === 'leave') { - bot.sendMessage(channel.server.channels.get('name', 'history'), '**' + user.username + '**' + ' left **' + channel.name + '** at **' + dateFormat(new Date(), 'mmmm dS, yyyy, h:MM:ss TT Z') + '**'); - } - - if (event === 'join') { - bot.sendMessage(channel.server.channels.get('name', 'history'), '**' + user.username + '**' + ' joined **' + channel.name + '** at **' + dateFormat(new Date(), 'mmmm dS, yyyy, h:MM:ss TT Z') + '**'); - } -} - -module.exports = history; diff --git a/lib/parser.js b/lib/parser.js index 5366784..841755c 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -1,22 +1,19 @@ 'use strict'; var playFile = require('./playFile'); -var randomRange = require('./randomRange'); - -var availableCommands = ['.help', '.bag', '.gg', '.random', '.lion', '.cheers']; -var random = ['./audio/duh1.mp3', './audio/duh2.mp3', './audio/duh3.mp3']; -var randomNumber = random.length; +var utils = require('./utils'); +var historyChannel = require('./eventLogging'); /** * Parses input from Discord based on the given parameters. - * + * * @param {Object} bot The given bot Client * @param {Object} msg The given message + * @param {FastMap} commandMap The given map */ -function parser(bot, msg) { +function parser(bot, msg, commandMap) { var voiceChannel = msg.author.voiceChannel; var message = msg.content; - if (!message.startsWith('.')) { return; } @@ -24,28 +21,16 @@ function parser(bot, msg) { if (voiceChannel.server.id !== msg.channel.server.id) { return; } - - switch (message) { - case availableCommands[0]: - bot.reply(msg, 'available commands are: ' + availableCommands.join(', ')); - break; - case availableCommands[1]: - playFile(bot, voiceChannel, '.125', 'https://hydra-media.cursecdn.com/dota2.gamepedia.com/e/ee/Wdoc_inthebag_01.mp3'); - break; - case availableCommands[2]: - playFile(bot, voiceChannel, '.25', './audio/gg.mp3'); - break; - case availableCommands[3]: - playFile(bot, voiceChannel, '1.2', random[randomRange(randomNumber)]); - break; - case availableCommands[4]: - playFile(bot, voiceChannel, '.125', 'http://hydra-media.cursecdn.com/dota2.gamepedia.com/4/4a/Lion_respawn_01.mp3'); - break; - case availableCommands[5]: - playFile(bot, voiceChannel, '.5', 'https://my.mixtape.moe/dcvdxs.wav'); - break; - default: - bot.reply(msg, 'available commands are: ' + availableCommands.join(', ')); + if (message === '.help') { + var mapKeys = ''; + for (var i = 0; i < commandMap.entries().length; i++) { + mapKeys += commandMap.entries()[i][0] + ' '; + } + bot.reply(msg, 'available commands are: ' + mapKeys); + utils.postMessage(historyChannel.historyChannel, msg.author + ' invoked command \x22.help\x22', bot); + } else { + utils.postMessage(historyChannel.historyChannel, msg.author + ' invoked command \x22' + message + '\x22.', bot); + playFile(bot, voiceChannel, commandMap.get(message)[1], commandMap.get(message)[0]); } } diff --git a/lib/presence.js b/lib/presence.js deleted file mode 100644 index 5d0cddc..0000000 --- a/lib/presence.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -var dateFormat = require('dateformat'); - -/** - * Records the activities of users based on the given parameters. - * - * @param {Object} oldUser The given old user - * @param {Object} newUser The given new user - * @param {Object} bot The given bot Client - */ -function presenceUpdated(oldUser, newUser, bot) { - for (var i = 0; i < bot.servers.length; i++) { - for (var j = 0; j < bot.servers[i].members.length; j++) { - if (bot.servers[i].members[j].id === newUser.id) { - var historyChannel = bot.servers[i].channels.get('name', 'history'); - if (oldUser.status !== newUser.status) { - bot.sendMessage(historyChannel, '**' + newUser.username + '**' + ' has gone **' + newUser.status + '** at **' + dateFormat(new Date(), 'mmmm dS, yyyy, h:MM:ss TT Z') + '**'); - } - if (oldUser.game !== newUser.game) { - if (!newUser.game) { - bot.sendMessage(historyChannel, '**' + newUser.username + '**' + ' stopped playing **' + oldUser.game.name + '** at **' + dateFormat(new Date(), 'mmmm dS, yyyy, h:MM:ss TT Z') + '**'); - } - else { - bot.sendMessage(historyChannel, '**' + newUser.username + '**' + ' started playing **' + newUser.game.name + '** at **' + dateFormat(new Date(), 'mmmm dS, yyyy, h:MM:ss TT Z') + '**'); - } - } - } - } - } -} - -module.exports = presenceUpdated; diff --git a/lib/randomRange.js b/lib/randomRange.js deleted file mode 100644 index e70b1e3..0000000 --- a/lib/randomRange.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -/** - * Gets a random integer between zero to the given parameter. - * - * @param {Number} range The given range - * @returns {number} The random integer - */ -function randomRange(range) { - return Math.floor(Math.random() * range); -} - -module.exports = randomRange; diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..ff8b535 --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,48 @@ +'use strict'; + +var random = ['./audio/duh1.mp3', './audio/duh2.mp3', './audio/duh3.mp3']; +var randomNumber = random.length; +var FastMap = require('collections/fast-map'); + +/** + * Gets a random integer between zero to the given parameter. + * + * @param {Number} range The given range + * @returns {number} The random integer + */ +function randomRange(range) { + return Math.floor(Math.random() * range); +} + +/** + * Gets a map. + * + * @returns {FastMap} + */ +function getMap() { + var localMap = new FastMap(); + localMap.set('.bag', ['https://hydra-media.cursecdn.com/dota2.gamepedia.com/e/ee/Wdoc_inthebag_01.mp3', '.125']); + localMap.set('.gg', ['./audio/gg.mp3', '.25']); + localMap.set('.dp', ['http://hydra-media.cursecdn.com/dota2.gamepedia.com/b/b4/Dpro_wailing_03.mp3', '.125']); + localMap.set('.lion', ['http://hydra-media.cursecdn.com/dota2.gamepedia.com/4/4a/Lion_respawn_01.mp3', '.125']); + localMap.set('.cheers', ['https://my.mixtape.moe/dcvdxs.wav', '.5']); + localMap.set('.random', [random[randomRange(randomNumber)], '1.2']); + return localMap; +} + +/** + * Posts a message to the server. + * + * @param {Object} message The given message + * @param {Object} channel Then given channel + * @param {Object} bot The given bot Client + */ +function postMessage(message, channel, bot) { + bot.sendMessage(channel, message); +} + +module.exports = { + getMap: getMap, + randomRange: randomRange, + postMessage: postMessage +}; diff --git a/main.js b/main.js index aad5b03..ec839f1 100644 --- a/main.js +++ b/main.js @@ -1,27 +1,28 @@ 'use strict'; require('dotenv').config(); +var eventLogging = require('./lib/eventLogging'); var parser = require('./lib/parser'); -var history = require('./lib/history'); -var presenceUpdated = require ('./lib/presence'); var Discord = require('discord.js'); +var utils = require('./lib/utils'); var bot = new Discord.Client(); +var commandMap = utils.getMap(); bot.on('voiceJoin', function (channel, user) { - history('join', bot, channel, user); + eventLogging.history('join', bot, channel, user); }); bot.on('voiceLeave', function (channel, user) { - history('leave', bot, channel, user); + eventLogging.history('leave', bot, channel, user); }); bot.on('presence', function (oldUser, newUser) { - presenceUpdated(oldUser, newUser, bot); + eventLogging.presenceUpdated(oldUser, newUser, bot); }); bot.on('message', function (msg) { - parser(bot, msg); + parser(bot, msg, commandMap); }); bot.loginWithToken(process.env.BOT_TOKEN); diff --git a/package.json b/package.json index 1f7360b..35ce76b 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ }, "homepage": "https://github.com/TomPed/miscord#readme", "dependencies": { + "collections": "^3.0.0", "dateformat": "^1.0.12", "discord.js": "^7.0.1", "dotenv": "^2.0.0",