Skip to content

Instantly share code, notes, and snippets.

@hellsan631
Last active April 28, 2016 02:41
Show Gist options
  • Save hellsan631/25b39c3fc112c95737aab6191a89043a to your computer and use it in GitHub Desktop.
Save hellsan631/25b39c3fc112c95737aab6191a89043a to your computer and use it in GitHub Desktop.
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();
};
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);
};
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);
});
};
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