Skip to content

Commit

Permalink
Doesn't work so far
Browse files Browse the repository at this point in the history
  • Loading branch information
MeLlamoPablo committed Jan 6, 2017
1 parent a95b25e commit 5d1aa84
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 116 deletions.
137 changes: 23 additions & 114 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fs = require('fs')
, SummaryHandler = require('./modules/summaryhandler')
, db = require('./modules/dbhandler')
, cfg = require('../config.js')
, loadSteamBot = require('./modules/steambotshandler/loadbot')
, moment = require('moment')
, pkg = require('../package.json')
, Discord = require('discord.js')
Expand Down Expand Up @@ -195,127 +196,32 @@ startupPromises.push(
// Connect to Steam and log in
startupPromises.push(
new Promise((fulfill, reject) => {
let steamStartupPromises = [];

// Get Steam credentials
steamStartupPromises.push(db.config.steam.getCredentials());

// Ignore all users that have already linked their Steam.
steamStartupPromises.push(new Promise((fulfill, reject) => {
db.users.getAllLinked().then(users => {
fulfill(users.map(e => { return e.steam_id }));
}).catch(reject);
}));

// Read sentry file if it exists
steamStartupPromises.push(new Promise((fulfill, reject) => {
// Try to find the sentry in the database first
db.config.steam.getSentryFile().then(sentryFile => {
if (sentryFile !== null) {
console.log("[STEAM] Found sentry file on the database.");
fulfill(sentryFile);
} else {
// If it doesn't exist, search it on the schedulebot directory
console.log("[STEAM] Sentry file doesn't exist in database, searching it in" +
" the local machine.");

fs.readFile("./sentry", (err, sentryFile) => {
if (!err) {
console.log("[STEAM] Found sentry file on the local machine.");

// Attempt to save it to the database
db.config.steam.saveSentryFile(sentryFile).then(() => {
console.log("[STEAM] Saved sentry file to the database.");
}).catch(err => {
console.log("[STEAM] Couldn't save the sentry file to the" +
"database:");
consle.error(err);
});

fulfill(sentryFile);
} else {
if (err.code === "ENOENT") {
// No such file or directory error
console.log("[STEAM] Sentry file doesn't exist in the local" +
" machine.");
fulfill(null);
} else {
reject(err);
}
}
});
}
}).catch(reject);
}));

// Connect to the Steam network
steamStartupPromises.push(new Promise(fulfill => {
steamClient.on("connected", () => {
console.log("[STEAM] Connected to Steam");
fulfill()
});
db.steambots.getAll().then(bots => {

steamClient.connect();
}));
if (bots.length > 0) {

Promise.all(steamStartupPromises).then(values => {
let credentials = values[0];
let ignoredUsers = values[1];
let sentry = values[2];
let steamStartupPromises = [];

let logOnDetails = {};
// Load all Steam bots
steamStartupPromises.push(new Promise((fulfill2, reject2) => {
let loadBotsPromises = [];

logOnDetails.account_name = credentials.username;
logOnDetails.password = credentials.password;
for (let i = 0; i < bots.length; i++) {
let bot = bots[i];
loadBotsPromises.push(loadSteamBot(bot));
}

if (credentials.auth_code !== null) {
logOnDetails.auth_code = credentials.auth_code;
db.config.steam.deleteAuthCode().then(() => {
console.log("[STEAM] Steam Guard code was used, and therefore deleted from" +
" the database.");
}).catch(console.error);
}
Promise.all(loadBotsPromises).then(fulfill2).catch(reject2);
}));

if (sentry !== null) {
logOnDetails.sha_sentryfile = sentry;
Promise.all(steamStartupPromises)/*.then(() => process.exit())*/.catch(reject);

} else {
return reject(
new Error("No steam bots were found. Please run the setup-steam script.")
);
}

steamUser.logOn(logOnDetails);

steamClient.on("logOnResponse", response => {
if (response.eresult == Steam.EResult.OK) {
console.log("[STEAM] Successfully logged in!");
steamFriends.setPersonaState(Steam.EPersonaState.Online);
steamFriends.setPersonaName(cfg.steam.name);
steamUser.gamesPlayed([{
game_id: 570
}]); // Appear as playing Dota 2

verificator = new steamVerf.Verificator({
trigger: steamVerf.Trigger.FriendRequest,
triggerOptions: {
secondService: "Discord's " + cfg.name,
ignoredUsers: ignoredUsers
},
steamClient: steamClient,
steamUser: steamUser,
steamFriends: steamFriends
});

dotaClient.on("ready", () => {
console.log("[DOTA] Running!");
dotaHandler = new DotaHandler(dotaClient);

fulfill();
});

dotaClient.launch();
} else {
let err = new Error("[STEAM] Login failed");
err.steamResponse = response;
reject(err);
}
});
}).catch(reject);
})
);
Expand Down Expand Up @@ -376,4 +282,7 @@ Promise.all(startupPromises).then(values => {
console.log("[INFO] ScheduleBot finished loading.")
}).catch(console.error);

}).catch(console.error);
}).catch(err => {
console.error(err);
process.exit(1);
});
36 changes: 35 additions & 1 deletion lib/modules/dbhandler/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,43 @@ const config = {

};

const steambots = {
getAll: () => new Promise((fulfill, reject) => {
db("steam_bots").select().then(fulfill).catch(reject);
}),

/**
* Saves a sentry file to the specified bot
*
* @param {number} botId
* @param {Buffer} sentryFile
*/
saveSentryFile: (botId, sentryFile) => new Promise((fulfill, reject) => {
db("steam_bots").update({
sentry_file: sentryFile
}).where({
id: botId
}).then(fulfill).catch(reject);
}),

/**
* Deletes the Steam Guard code from the specified bot.
*
* @param {number} botId
*/
deleteSteamGuardCode: botId => new Promise((fulfill, reject) => {
db("steam_bots").update({
steam_guard_code: null
}).where({
id: botId
}).then(fulfill).catch(reject);
})
};

module.exports = {
events: events,
confirms: confirms,
users: users,
config: config
config: config,
steambots: steambots
};
3 changes: 2 additions & 1 deletion lib/modules/dbhandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@ module.exports = {

confirms: core.confirms,
users: core.users,
config: core.config
config: core.config,
steambots: core.steambots
};
34 changes: 34 additions & 0 deletions lib/modules/steambotshandler/loadbot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use strict";

const SteamBot = require("../../structures/SteamBot");

/**
* @typedef {object} BotObject
* @property {number} id
* @property {string} username
* @property {string} password
* @property {string} [steam_guard_code]
* @property {Buffer} [sentry_file]
*/

/**
* @param {BotObject} botObj
* @return {Promise<SteamBot>}
*/
module.exports = botObj => new Promise((fulfill, reject) => {
let bot = new SteamBot(botObj.id, function onSentry(sentry) {
console.log("got a sentry"); // TODO
});

let logInDetails = {
username: botObj.username,
password: botObj.password,
steam_guard_code: botObj.steam_guard_code,
sentry_file: botObj.sentry_file
};

bot.connectToSteam()
.then(() => bot.logIn(logInDetails))
.then(() => fulfill(bot))
.catch(reject);
});
114 changes: 114 additions & 0 deletions lib/structures/SteamBot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const cfg = require("../../config")
, crypto = require("crypto")
, db = require("../modules/dbhandler").steambots
, Dota2 = require("dota2")
, Steam = require("steam")
;

class SteamBot {

constructor(id) {
this.id = id;
this.prefix = `[STEAM] [BOT #${id}]`;

this.steam = {};
this.dota = {};

this.steam.client = new Steam.SteamClient();
this.steam.user = new Steam.SteamUser(this.steam.client);
this.steam.friends = new Steam.SteamFriends(this.steam.client);
this.dota.client = new Dota2.Dota2Client(this.steam.client, false, false);

this.steam.user.on("updateMachineAuth", (sentry, callback) => {
let hashedSentry = crypto.createHash('sha1').update(sentry.bytes).digest();

db.saveSentryFile(this.id, hashedSentry).then(() => {
this._log("Saved new sentry file to the database.");
}).catch(console.error);
callback({ sha_file: hashedSentry });
});

this.steam.client.on("error", console.error);
}

/**
* Connects the current client to the steam network.
* @return {Promise}
*/
connectToSteam() {
return new Promise((fulfill, reject) => {
this.steam.client.on("connected", err => {
if (!err) {
this._log("Connected to the Steam network.");
fulfill();
} else {
reject(err);
}
});

this.steam.client.connect();
});
}

/**
* Logs in Steam with the provided details.
* @param details
* @param {string} details.username The steam account's username
* @param {string} details.password The steam account's password
* @param {string} [details.steam_guard_code] If present, will log in with the Steam guard code.
* @param {Buffer} [details.sentry_file] If present, will log in with the sentry file.
* @return {Promise}
*/
logIn(details) {
return new Promise((fulfill, reject) => {
let logOnDetails = {};

logOnDetails.account_name = details.username;
logOnDetails.password = details.password;

if (details.sentry_file) {
logOnDetails.sha_sentryfile = details.sentry_file;
this._log("Logging in with the database's sentry file...");
}

if (details.steam_guard_code) {
logOnDetails.auth_code = details.steam_guard_code;
db.deleteSteamGuardCode(this.id).then(() => {
this._log("Steam Guard code was used, and therefore deleted from the database");
});
}

this.steam.user.logOn(logOnDetails);
console.log(logOnDetails)

this.steam.client.on("logOnResponse", response => {
if (response.eresult == Steam.EResult.OK) {
this._log("Successfully logged in!");

this.steam.friends.setPersonaState(Steam.EPersonaState.Online);
this.steam.friends.setPersonaName(cfg.steam.name + ` #${this.id}`);
this.steam.user.gamesPlayed([{
game_id: 570
}]); // Appear as playing Dota 2

fulfill();
} else {
let err = new Error(this.prefix + " Login failed. Re-check your credentials.");
err.steamResponse = response;
reject(err); // TODO login fails
}
});
});
}

/**
* @param {string} msg
* @private
*/
_log(msg) {
console.log(this.prefix + " " + msg);
}

}

module.exports = SteamBot;

0 comments on commit 5d1aa84

Please sign in to comment.