Skip to content

Commit

Permalink
Now saving the sentry file in the database
Browse files Browse the repository at this point in the history
Heroku's free plan may restart the app at any time, and delete the sentry file. Thus, the bot would crash because it wouldn't be able to log in to Steam.
  • Loading branch information
MeLlamoPablo committed Dec 14, 2016
1 parent 6fb5196 commit 5ffe987
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
3 changes: 2 additions & 1 deletion db_setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ CREATE TABLE public.config
bot_token TEXT,
steam_username TEXT,
steam_password TEXT,
steam_guard_code TEXT
steam_guard_code TEXT,
steam_sentry_file BYTEA
);

CREATE TABLE public.admins
Expand Down
50 changes: 35 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,17 @@ steamClient.on("error", console.error);
// If a sentry file is generated, update it.
steamUser.on('updateMachineAuth', function(sentry, callback) {
let hashedSentry = crypto.createHash('sha1').update(sentry.bytes).digest();
fs.writeFileSync('sentry', hashedSentry);
console.log("[STEAM] New sentry file saved");

callback({ sha_file: hashedSentry});
db.config.steam.saveSentryFile(hashedSentry).then(() => {
console.log("[STEAM] New sentry file saved to the database.");
callback({ sha_file: hashedSentry});
}).catch(err => {
console.log("[STEAM] Couldn't save the sentry file to the database:");
consle.error(err);

console.log("[STEAM] Saving it to the local machine instead");
fs.writeFileSync('sentry', hashedSentry);
callback({ sha_file: hashedSentry});
});
});

// Startup tasks
Expand Down Expand Up @@ -194,20 +201,33 @@ startupPromises.push(

// Read sentry file if it exists
steamStartupPromises.push(new Promise((fulfill, reject) => {
fs.readFile("./sentry", (err, sentryFile) => {
if (!err) {
console.log("[STEAM] Found sentry file");
// 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 (err.code === "ENOENT") {
// No such file or directory error
console.log("[STEAM] Sentry file doesn't exist");
fulfill(null);
} else {
reject(err);
}
// 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.");
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
Expand Down
16 changes: 16 additions & 0 deletions lib/modules/dbhandler/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,22 @@ const config = {
});
},

getSentryFile: function() {
return new Promise((fulfill, reject) => {
db("config").select("steam_sentry_file").then(rows => {
fulfill(rows[0] ? rows[0].steam_sentry_file : null);
}).catch(reject);
});
},

saveSentryFile: function(sentryFile) {
return new Promise((fulfill, reject) => {
db("config").update({
steam_sentry_file: sentryFile
}).then(fulfill).catch(reject);
});
},

deleteAuthCode: function() {
return new Promise((fulfill, reject) => {
db("config").update({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "schedulebot",
"version": "1.0.3",
"version": "1.0.4",
"description": "A Discord bot that makes scheduling easy",
"homepage": "https://github.com/mellamopablo/schedulebot#readme",
"author": "Pablo Rodríguez <pabloviolin8@gmail.com> (https://github.com/MeLlamoPablo)",
Expand Down

0 comments on commit 5ffe987

Please sign in to comment.