Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Codebase Refactoring
  • Loading branch information
Duncan Perkins authored and Duncan Perkins committed Jun 9, 2016
1 parent 7a0e85b commit 5944435
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 106 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.env
.idea/
node_modules/
npm-debug.log
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -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
Expand Down
Binary file modified img/nicememe.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions 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
};

23 changes: 0 additions & 23 deletions lib/history.js

This file was deleted.

45 changes: 15 additions & 30 deletions lib/parser.js
@@ -1,51 +1,36 @@
'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;
}

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]);
}
}

Expand Down
33 changes: 0 additions & 33 deletions lib/presence.js

This file was deleted.

13 changes: 0 additions & 13 deletions lib/randomRange.js

This file was deleted.

48 changes: 48 additions & 0 deletions 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
};
13 changes: 7 additions & 6 deletions 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);
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down

0 comments on commit 5944435

Please sign in to comment.