Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
11 changed files
with
133 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.env | ||
.idea/ | ||
node_modules/ | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters