Last active
April 28, 2016 02:41
-
-
Save hellsan631/25b39c3fc112c95737aab6191a89043a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var Gamers = require('Gamers'); | |
var Games = require('Games'); | |
var discord = require('discord.js'); | |
var config = require('config'); | |
var Promise = require('bluebird'); | |
module.exports = Bot; | |
function Bot(app) { | |
var _this = this; | |
_this.app = app; | |
_this.client = new discord.Client(); | |
_this | |
.login() | |
//we don't need to use a function wrapper for this because promises (but maybe we might have to) | |
.then(_this.initModels); | |
} | |
Bot.prototype.login = function() { | |
return this.client.login(config.username, config.password); | |
}; | |
Bot.prototype.initModels = function() { | |
this.Gamers = new Gamers(self); | |
return Promise.resolve(); | |
}; |
This file contains hidden or 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
var Promise = require('bluebird'); | |
module.exports = Gamers; | |
function Gamers(bot) { | |
this.db = bot.app.models; | |
this.bot = bot.client; | |
//bootstrap our events | |
this.initEvents(); | |
} | |
Gamers.prototype.initEvents = function() { | |
var _this = this; | |
_this.bot.on('presence', function(old, data) { | |
return _this.rename(old, data); | |
}); | |
}; | |
Gamers.prototype.rename = function(old, data) { | |
if (old.username !== data.username) | |
return this.db.Gamer.update({id: data.id}, {username: data.username}); | |
return Promise.resolve(false); | |
}; |
This file contains hidden or 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
var Promise = require('bluebird'); | |
module.exports = Games; | |
function Games(bot) { | |
this.db = bot.app.models; | |
this.bot = bot.client; | |
//bootstrap our events | |
this.initEvents(); | |
} | |
Games.prototype.initEvents = function() { | |
var _this = this; | |
this.bot.on('presence', function(old, data){ | |
_this.logGameSession(old, data); | |
}); | |
}; | |
Games.prototype.logGameSession = function(old, data) { | |
if (old.game !== data.game && data.game) | |
return this.newSession(data); | |
return Promise.resolve(false); | |
}; | |
Games.prototype.newSession = function(session) { | |
var _this = this; | |
return new Promise(function(resolve, reject) { | |
//findings everything else | |
var lookups = []; | |
lookups.push(_this.db.Gamer.discordSync(session)); | |
lookups.push(_this.db.Game.discordSync(session)); | |
Promise | |
.all(lookups) | |
.spread(function(Gamer, Game) { | |
return _this.db | |
.Gamelog | |
.findOrCreate({ | |
gameId: Game.id, | |
gamerId: Gamer.id, | |
date: new Date() | |
}); | |
}) | |
.then(resolve) | |
.catch(reject); | |
}); | |
}; |
This file contains hidden or 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
var loopback = require('loopback'); | |
var boot = require('loopback-boot'); | |
var app = module.exports = loopback(); | |
var Bot = require('praxbot/Bot.js'); | |
app.start = function() { | |
// start the web server | |
return app.listen(function() { | |
app.emit('started'); | |
var baseUrl = app.get('url').replace(/\/$/, ''); | |
console.log('Web server listening at: %s', baseUrl); | |
if (app.get('loopback-component-explorer')) { | |
var explorerPath = app.get('loopback-component-explorer').mountPath; | |
console.log('Browse your REST API at %s%s', baseUrl, explorerPath); | |
} | |
console.log('Bot running'); | |
var praxBot = new Bot(app); | |
}); | |
}; | |
// Bootstrap the application, configure models, datasources and middleware. | |
// Sub-apps like REST API are mounted via boot scripts. | |
boot(app, __dirname, function(err) { | |
if (err) throw err; | |
// start the server if `$ node server.js` | |
if (require.main === module) | |
app.start(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment